-
Notifications
You must be signed in to change notification settings - Fork 0
/
mop_system.py
140 lines (117 loc) · 4.61 KB
/
mop_system.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
# mop_system.py
import asyncio
import aiohttp
from typing import Dict, List, Any
from dataclasses import dataclass
@dataclass
class AgentConfig:
endpoint: str
model_name: str
role: str
temperature: float
max_tokens: int
class BaseAgent:
def __init__(self, config: AgentConfig):
self.config = config
self.conversation_history: List[Dict] = []
async def generate_response(self, prompt: str, context: Dict = None) -> str:
async with aiohttp.ClientSession() as session:
payload = {
"model": self.config.model_name,
"messages": [
{"role": "system", "content": self.config.role},
{"role": "user", "content": prompt}
],
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens
}
if context:
payload["messages"].insert(1, {
"role": "assistant",
"content": str(context)
})
async with session.post(self.config.endpoint, json=payload) as response:
result = await response.json()
return result["choices"][0]["message"]["content"]
class MemoryAgent(BaseAgent):
def __init__(self, config: AgentConfig):
super().__init__(config)
self.memory_store = sqlite3.connect('memory.sqlite')
async def process_memory(self, input_data: Dict) -> Dict:
prompt = f"""
Process this memory:
Input: {input_data}
Tasks:
1. Analyze importance (0-1)
2. Extract key concepts
3. Identify relationships with existing memories
4. Determine storage location (STM/LTM)
Provide structured response.
"""
response = await self.generate_response(prompt)
return self.parse_memory_decision(response)
class EvolutionAgent(BaseAgent):
async def evaluate_fitness(self, memory_item: Dict) -> float:
prompt = f"""
Evaluate this memory's fitness:
Memory: {memory_item}
Consider:
1. Relevance to current knowledge
2. Usage patterns
3. Information density
4. Connection strength
Provide numerical score (0-1) with reasoning.
"""
response = await self.generate_response(prompt)
return self.parse_fitness_score(response)
class SupervisorAgent(BaseAgent):
async def coordinate_agents(self, agent_states: Dict) -> Dict:
prompt = f"""
Current agent states:
{agent_states}
Tasks:
1. Evaluate system health
2. Identify optimization opportunities
3. Suggest resource allocation
4. Determine priority actions
Provide coordination instructions.
"""
response = await self.generate_response(prompt)
return self.parse_supervision_decision(response)
class MOPOrchestrator:
def __init__(self):
self.agents = {
"memory": MemoryAgent(AgentConfig(
endpoint="http://localhost:1234/v1/chat/completions",
model_name="phi-2",
role="You are a memory management specialist...",
temperature=0.3,
max_tokens=512
)),
"evolution": EvolutionAgent(AgentConfig(
endpoint="http://localhost:1235/v1/chat/completions",
model_name="mistral-7b",
role="You are an evolution and optimization specialist...",
temperature=0.7,
max_tokens=1024
)),
"supervisor": SupervisorAgent(AgentConfig(
endpoint="http://localhost:1236/v1/chat/completions",
model_name="llama2",
role="You are a system supervisor and coordinator...",
temperature=0.5,
max_tokens=2048
))
}
self.message_queue = asyncio.Queue()
async def process_input(self, input_data: Dict):
# 1. Submit to Memory Agent
memory_result = await self.agents["memory"].process_memory(input_data)
# 2. Evolution Agent evaluates
fitness_score = await self.agents["evolution"].evaluate_fitness(memory_result)
# 3. Supervisor coordinates
coordination = await self.agents["supervisor"].coordinate_agents({
"memory_state": memory_result,
"fitness_score": fitness_score
})
return self.compile_response(memory_result, fitness_score, coordination)