-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
301 lines (250 loc) · 9.52 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# agent.py
import asyncio
from dataclasses import dataclass, field
import uuid
from typing import Any, Dict, Optional
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
# Import enhancement systems
from python.systems.pattern_recognition import PatternEngine
from python.systems.resource_optimization import ResourceManager
from python.systems.learning_integration import LearningEngine
from python.systems.multi_agent_cooperation import CoordinationEngine
from python.systems.system_evolution import EvolutionEngine
from python.systems.analytics_system import AnalyticsEngine
from python.systems.interface_enhancement import InterfaceEngine
@dataclass
class ModelConfig:
provider: str
name: str
ctx_length: int
limit_requests: int
limit_input: int
limit_output: int
kwargs: Dict[str, Any] = field(default_factory=dict)
@dataclass
class AgentConfig:
# Core configurations
chat_model: ModelConfig
utility_model: ModelConfig
embeddings_model: ModelConfig
prompts_subdir: str = ""
memory_subdir: str = ""
# Enhancement systems configurations
pattern_recognition: dict = field(default_factory=lambda: {
'similarity_threshold': 0.75,
'min_examples': 5,
'learning_rate': 0.1
})
resource_optimization: dict = field(default_factory=lambda: {
'optimization_threshold': 0.8,
'allocation_strategy': 'dynamic'
})
learning_integration: dict = field(default_factory=lambda: {
'experience_threshold': 0.7,
'learning_rate': 0.1
})
multi_agent_cooperation: dict = field(default_factory=lambda: {
'coordination_threshold': 0.8,
'team_size': 3
})
system_evolution: dict = field(default_factory=lambda: {
'evolution_rate': 0.2,
'adaptation_threshold': 0.7
})
analytics_system: dict = field(default_factory=lambda: {
'analysis_window': 1000,
'confidence_threshold': 0.8
})
interface_system: dict = field(default_factory=lambda: {
'response_format': 'markdown',
'style_preferences': {}
})
@dataclass
class AgentContext:
_contexts: Dict[str, "AgentContext"] = field(default_factory=dict)
_counter: int = 0
id: str = field(default_factory=lambda: str(uuid.uuid4()))
name: Optional[str] = None
config: AgentConfig = field(default_factory=lambda: AgentConfig(**create_default_config()))
log: Any = field(default_factory=lambda: Log())
agent0: Optional["Agent"] = None
paused: bool = False
streaming_agent: Optional["Agent"] = None
process: Any = None
no: int = field(init=False)
def __post_init__(self):
if self.agent0 is None:
self.agent0 = Agent(0, self.config, self)
AgentContext._counter += 1
self.no = AgentContext._counter
# Store context
existing = self._contexts.get(self.id)
if existing:
AgentContext.remove(self.id)
self._contexts[self.id] = self
@classmethod
def get(cls, id: str) -> Optional["AgentContext"]:
return cls._contexts.get(id)
@classmethod
def remove(cls, id: str):
if id in cls._contexts:
del cls._contexts[id]
class Agent:
def __init__(self, number: int, config: AgentConfig, context: AgentContext | None = None):
# Core initialization
self.number = number
self.config = config
self.context = context or AgentContext(config)
self.agent_name = f"Agent {self.number}"
# Enhancement systems
self.pattern_engine = PatternEngine(self.config.pattern_recognition)
self.resource_manager = ResourceManager(self.config.resource_optimization)
self.learning_engine = LearningEngine(self.config.learning_integration)
self.cooperation_engine = CoordinationEngine(self.config.multi_agent_cooperation)
self.evolution_engine = EvolutionEngine(self.config.system_evolution)
self.analytics_engine = AnalyticsEngine(self.config.analytics_system)
self.interface_engine = InterfaceEngine(self.config.interface_system)
# State management
self.history = []
self.last_user_message = None
self.intervention = None
self.data = {}
async def monologue(self):
while True:
try:
# Initialize loop
self.loop_data = self.initialize_loop()
# Process through enhancement systems
await self.process_through_systems()
# Main conversation loop
while True:
try:
# Prepare prompt
prompt = await self.prepare_prompt()
# Generate response
response = await self.generate_response(prompt)
# Process response
result = await self.process_response(response)
if result:
return result
except Exception as e:
await self.handle_exception(e)
except Exception as e:
self.handle_critical_exception(e)
async def process_through_systems(self):
# Pattern recognition
patterns = await self.pattern_engine.process_patterns(self.loop_data)
# Resource optimization
resources = await self.resource_manager.optimize_resources()
# Learning integration
learning = await self.learning_engine.process_learning()
# Multi-agent cooperation
cooperation = await self.cooperation_engine.coordinate_agents()
# System evolution
evolution = await self.evolution_engine.evolve_system()
# Analytics processing
analytics = await self.analytics_engine.analyze_system()
# Interface enhancement
interface = await self.interface_engine.process_interaction()
# Integrate results
self.integrate_system_results(
patterns, resources, learning, cooperation,
evolution, analytics, interface
)
async def prepare_prompt(self):
# Get system prompt
system_prompt = await self.get_system_prompt()
# Get enhancement data
system_data = await self.get_system_enhancements()
# Combine prompts
prompt = ChatPromptTemplate.from_messages([
SystemMessage(content="\n\n".join(system_prompt + system_data)),
*self.format_history()
])
return prompt
async def generate_response(self, prompt):
# Process through interface
formatted_prompt = await self.interface_engine.format_prompt(prompt)
# Generate response
response = await self.call_model(formatted_prompt)
# Analyze response
await self.analytics_engine.analyze_response(response)
return response
async def process_response(self, response):
# Process through interface
formatted_response = await self.interface_engine.format_response(response)
# Extract and execute tools
result = await self.process_tools(formatted_response)
# Learn from execution
await self.learning_engine.learn_from_execution(result)
return result
# Additional helper methods...
class Log:
def __init__(self):
self.entries = []
def add(self, entry):
self.entries.append(entry)
def create_default_config():
return {
'chat_model': {
'provider': 'openai',
'name': 'gpt-4',
'ctx_length': 2048,
'limit_requests': 100,
'limit_input': 2048,
'limit_output': 2048,
'kwargs': {
'temperature': 0.7
}
},
'utility_model': {
'provider': 'openai',
'name': 'gpt-3.5-turbo',
'ctx_length': 2048,
'limit_requests': 100,
'limit_input': 2048,
'limit_output': 2048,
'kwargs': {
'temperature': 0.3
}
},
'embeddings_model': {
'provider': 'openai',
'name': 'text-embedding-ada-002',
'ctx_length': 2048,
'limit_requests': 100,
'limit_input': 2048,
'limit_output': 2048,
'kwargs': {}
},
'pattern_recognition': {
'similarity_threshold': 0.75,
'min_examples': 5,
'learning_rate': 0.1
},
'resource_optimization': {
'optimization_threshold': 0.8,
'allocation_strategy': 'dynamic'
},
'learning_integration': {
'experience_threshold': 0.7,
'learning_rate': 0.1
},
'multi_agent_cooperation': {
'coordination_threshold': 0.8,
'team_size': 3
},
'system_evolution': {
'evolution_rate': 0.2,
'adaptation_threshold': 0.7
},
'analytics_system': {
'analysis_window': 1000,
'confidence_threshold': 0.8
},
'interface_system': {
'response_format': 'markdown',
'style_preferences': {}
}
}