pycityagent 2.0.0a49__cp39-cp39-macosx_11_0_arm64.whl → 2.0.0a51__cp39-cp39-macosx_11_0_arm64.whl
Sign up to get free protection for your applications and to get access to all the features.
- pycityagent/__init__.py +12 -3
- pycityagent/agent/__init__.py +9 -0
- pycityagent/agent/agent.py +324 -0
- pycityagent/{agent.py → agent/agent_base.py} +41 -345
- pycityagent/cityagent/bankagent.py +28 -16
- pycityagent/cityagent/firmagent.py +63 -25
- pycityagent/cityagent/governmentagent.py +35 -19
- pycityagent/cityagent/initial.py +38 -28
- pycityagent/cityagent/memory_config.py +240 -128
- pycityagent/cityagent/nbsagent.py +82 -36
- pycityagent/cityagent/societyagent.py +155 -72
- pycityagent/simulation/agentgroup.py +2 -2
- pycityagent/simulation/simulation.py +94 -55
- pycityagent/tools/__init__.py +11 -0
- pycityagent/{workflow → tools}/tool.py +3 -1
- pycityagent/workflow/__init__.py +0 -5
- pycityagent/workflow/block.py +12 -10
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/METADATA +1 -2
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/RECORD +23 -20
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/LICENSE +0 -0
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/WHEEL +0 -0
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/entry_points.txt +0 -0
- {pycityagent-2.0.0a49.dist-info → pycityagent-2.0.0a51.dist-info}/top_level.txt +0 -0
@@ -1,30 +1,41 @@
|
|
1
1
|
import asyncio
|
2
|
+
import logging
|
2
3
|
from typing import Optional
|
3
4
|
|
4
5
|
import numpy as np
|
5
|
-
|
6
|
-
from pycityagent
|
6
|
+
|
7
|
+
from pycityagent import InstitutionAgent, Simulator
|
7
8
|
from pycityagent.economy import EconomyClient
|
8
|
-
from pycityagent.
|
9
|
+
from pycityagent.llm.llm import LLM
|
9
10
|
from pycityagent.memory import Memory
|
10
|
-
from pycityagent.
|
11
|
-
import
|
11
|
+
from pycityagent.message import Messager
|
12
|
+
from pycityagent.tools import ExportMlflowMetrics
|
12
13
|
|
13
14
|
logger = logging.getLogger("pycityagent")
|
14
15
|
|
16
|
+
|
15
17
|
class NBSAgent(InstitutionAgent):
|
16
18
|
export_metrics = ExportMlflowMetrics(log_batch_size=3)
|
17
|
-
|
18
|
-
def __init__(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
|
20
|
+
def __init__(
|
21
|
+
self,
|
22
|
+
name: str,
|
23
|
+
llm_client: Optional[LLM] = None,
|
24
|
+
simulator: Optional[Simulator] = None,
|
25
|
+
memory: Optional[Memory] = None,
|
26
|
+
economy_client: Optional[EconomyClient] = None,
|
27
|
+
messager: Optional[Messager] = None,
|
28
|
+
avro_file: Optional[dict] = None,
|
29
|
+
) -> None:
|
30
|
+
super().__init__(
|
31
|
+
name=name,
|
32
|
+
llm_client=llm_client,
|
33
|
+
simulator=simulator,
|
34
|
+
memory=memory,
|
35
|
+
economy_client=economy_client,
|
36
|
+
messager=messager,
|
37
|
+
avro_file=avro_file,
|
38
|
+
)
|
28
39
|
self.initailzed = False
|
29
40
|
self.last_time_trigger = None
|
30
41
|
self.time_diff = 30 * 24 * 60 * 60
|
@@ -32,6 +43,7 @@ class NBSAgent(InstitutionAgent):
|
|
32
43
|
self.num_labor_hours = 168
|
33
44
|
self.productivity_per_labor = 1
|
34
45
|
self.price = 1
|
46
|
+
|
35
47
|
async def month_trigger(self):
|
36
48
|
now_time = await self.simulator.get_time()
|
37
49
|
if self.last_time_trigger is None:
|
@@ -41,46 +53,80 @@ class NBSAgent(InstitutionAgent):
|
|
41
53
|
self.last_time_trigger = now_time
|
42
54
|
return True
|
43
55
|
return False
|
44
|
-
|
56
|
+
|
45
57
|
async def gather_messages(self, agent_ids, content):
|
46
58
|
infos = await super().gather_messages(agent_ids, content)
|
47
|
-
return [info[
|
59
|
+
return [info["content"] for info in infos]
|
48
60
|
|
49
61
|
async def forward(self):
|
50
62
|
if await self.month_trigger():
|
51
63
|
citizens = await self.memory.get("citizens")
|
52
64
|
while True:
|
53
|
-
agents_forward = await self.gather_messages(citizens,
|
65
|
+
agents_forward = await self.gather_messages(citizens, "forward")
|
54
66
|
if np.all(np.array(agents_forward) > self.forward_times):
|
55
67
|
break
|
56
68
|
await asyncio.sleep(1)
|
57
69
|
work_propensity = await self.gather_messages(citizens, "work_propensity")
|
58
|
-
working_hours = np.mean(work_propensity)*self.num_labor_hours
|
70
|
+
working_hours = np.mean(work_propensity) * self.num_labor_hours
|
59
71
|
firm_id = await self.memory.get("firm_id")
|
60
72
|
price = await self.economy_client.get(firm_id, "price")
|
61
73
|
prices = await self.economy_client.get(self._agent_id, "prices")
|
62
74
|
initial_price = prices[0]
|
63
|
-
nominal_gdp =
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
75
|
+
nominal_gdp = (
|
76
|
+
working_hours * len(citizens) * self.productivity_per_labor * price
|
77
|
+
)
|
78
|
+
real_gdp = (
|
79
|
+
working_hours
|
80
|
+
* len(citizens)
|
81
|
+
* self.productivity_per_labor
|
82
|
+
* initial_price
|
83
|
+
)
|
84
|
+
await self.economy_client.update(
|
85
|
+
self._agent_id, "nominal_gdp", [nominal_gdp], mode="merge"
|
86
|
+
)
|
87
|
+
await self.economy_client.update(
|
88
|
+
self._agent_id, "real_gdp", [real_gdp], mode="merge"
|
89
|
+
)
|
90
|
+
await self.economy_client.update(
|
91
|
+
self._agent_id, "working_hours", [working_hours], mode="merge"
|
92
|
+
)
|
93
|
+
await self.economy_client.update(
|
94
|
+
self._agent_id, "prices", [price], mode="merge"
|
95
|
+
)
|
96
|
+
depression = await self.gather_messages(citizens, "depression")
|
70
97
|
depression = np.mean(depression)
|
71
|
-
await self.economy_client.update(
|
72
|
-
|
98
|
+
await self.economy_client.update(
|
99
|
+
self._agent_id, "depression", [depression], mode="merge"
|
100
|
+
)
|
101
|
+
consumption_currency = await self.gather_messages(
|
102
|
+
citizens, "consumption_currency"
|
103
|
+
)
|
73
104
|
consumption_currency = np.mean(consumption_currency)
|
74
|
-
await self.economy_client.update(
|
75
|
-
|
105
|
+
await self.economy_client.update(
|
106
|
+
self._agent_id,
|
107
|
+
"consumption_currency",
|
108
|
+
[consumption_currency],
|
109
|
+
mode="merge",
|
110
|
+
)
|
111
|
+
income_currency = await self.gather_messages(citizens, "income_currency")
|
76
112
|
income_currency = np.mean(income_currency)
|
77
|
-
await self.economy_client.update(
|
113
|
+
await self.economy_client.update(
|
114
|
+
self._agent_id, "income_currency", [income_currency], mode="merge"
|
115
|
+
)
|
78
116
|
self.forward_times += 1
|
79
117
|
for uuid in citizens:
|
80
|
-
await self.send_message_to_agent(
|
118
|
+
await self.send_message_to_agent(
|
119
|
+
uuid, f"nbs_forward@{self.forward_times}"
|
120
|
+
)
|
81
121
|
|
82
|
-
metrics = {
|
83
|
-
|
122
|
+
metrics = {
|
123
|
+
"nominal_gdp": nominal_gdp,
|
124
|
+
"working_hours": working_hours,
|
125
|
+
"price": price,
|
126
|
+
"depression": depression,
|
127
|
+
"consumption": consumption_currency,
|
128
|
+
"income": income_currency,
|
129
|
+
}
|
84
130
|
for k, v in metrics.items():
|
85
131
|
await self.export_metrics(
|
86
132
|
metric={
|
@@ -89,4 +135,4 @@ class NBSAgent(InstitutionAgent):
|
|
89
135
|
"step": self.forward_times,
|
90
136
|
},
|
91
137
|
clear_cache=True,
|
92
|
-
)
|
138
|
+
)
|
@@ -1,22 +1,27 @@
|
|
1
1
|
import asyncio
|
2
2
|
import json
|
3
|
+
import logging
|
3
4
|
from typing import Optional
|
4
|
-
|
5
|
+
|
6
|
+
from pycityagent import CitizenAgent, Simulator
|
5
7
|
from pycityagent.agent import Agent
|
6
|
-
from pycityagent.llm.llm import LLM
|
7
8
|
from pycityagent.economy import EconomyClient
|
8
|
-
from pycityagent.
|
9
|
+
from pycityagent.llm.llm import LLM
|
9
10
|
from pycityagent.memory import Memory
|
10
|
-
from pycityagent.
|
11
|
+
from pycityagent.message import Messager
|
11
12
|
from pycityagent.workflow import Block
|
12
|
-
from .
|
13
|
+
from pycityagent.tools import UpdateWithSimulator
|
14
|
+
|
15
|
+
from .blocks import (CognitionBlock, EconomyBlock, MobilityBlock, NeedsBlock,
|
16
|
+
OtherBlock, PlanBlock, SocialBlock)
|
13
17
|
from .blocks.economy_block import MonthPlanBlock
|
14
|
-
import logging
|
15
18
|
|
16
19
|
logger = logging.getLogger("pycityagent")
|
17
20
|
|
21
|
+
|
18
22
|
class PlanAndActionBlock(Block):
|
19
23
|
"""主动工作流"""
|
24
|
+
|
20
25
|
longTermDecisionBlock: MonthPlanBlock
|
21
26
|
needsBlock: NeedsBlock
|
22
27
|
planBlock: PlanBlock
|
@@ -25,48 +30,77 @@ class PlanAndActionBlock(Block):
|
|
25
30
|
economyBlock: EconomyBlock
|
26
31
|
otherBlock: OtherBlock
|
27
32
|
|
28
|
-
def __init__(
|
29
|
-
|
33
|
+
def __init__(
|
34
|
+
self,
|
35
|
+
agent: Agent,
|
36
|
+
llm: LLM,
|
37
|
+
memory: Memory,
|
38
|
+
simulator: Simulator,
|
39
|
+
economy_client: EconomyClient,
|
40
|
+
):
|
41
|
+
super().__init__(
|
42
|
+
name="plan_and_action_block", llm=llm, memory=memory, simulator=simulator
|
43
|
+
)
|
30
44
|
self._agent = agent
|
31
|
-
self.longTermDecisionBlock = MonthPlanBlock(
|
45
|
+
self.longTermDecisionBlock = MonthPlanBlock(
|
46
|
+
llm=llm, memory=memory, simulator=simulator, economy_client=economy_client
|
47
|
+
)
|
32
48
|
self.needsBlock = NeedsBlock(llm=llm, memory=memory, simulator=simulator)
|
33
49
|
self.planBlock = PlanBlock(llm=llm, memory=memory, simulator=simulator)
|
34
50
|
self.mobilityBlock = MobilityBlock(llm=llm, memory=memory, simulator=simulator)
|
35
|
-
self.socialBlock = SocialBlock(
|
36
|
-
|
51
|
+
self.socialBlock = SocialBlock(
|
52
|
+
agent=self, llm=llm, memory=memory, simulator=simulator
|
53
|
+
)
|
54
|
+
self.economyBlock = EconomyBlock(
|
55
|
+
llm=llm, memory=memory, simulator=simulator, economy_client=economy_client
|
56
|
+
)
|
37
57
|
self.otherBlock = OtherBlock(llm=llm, memory=memory)
|
38
58
|
|
39
59
|
async def check_and_update_step(self):
|
40
|
-
status = await self.memory.get(
|
60
|
+
status = await self.memory.get("status")
|
41
61
|
if status == 2:
|
42
62
|
# 正在运动
|
43
63
|
logger.info("Agent is moving")
|
44
64
|
await asyncio.sleep(1)
|
45
65
|
return False
|
46
|
-
|
66
|
+
|
47
67
|
# 获取上一步信息
|
48
68
|
current_step = await self.memory.get("current_step")
|
49
|
-
if current_step[
|
69
|
+
if current_step["intention"] == "" or current_step["type"] == "":
|
50
70
|
# 没有上一步,直接返回
|
51
71
|
return True
|
52
72
|
time_now = int(await self.simulator.get_time())
|
53
|
-
step_start_time = current_step[
|
54
|
-
step_consumed_time = current_step[
|
55
|
-
time_end_plan = step_start_time + int(step_consumed_time)*60
|
73
|
+
step_start_time = current_step["start_time"]
|
74
|
+
step_consumed_time = current_step["evaluation"]["consumed_time"]
|
75
|
+
time_end_plan = step_start_time + int(step_consumed_time) * 60
|
56
76
|
if time_now >= time_end_plan:
|
57
77
|
# 上一步执行完成
|
58
78
|
current_plan = await self.memory.get("current_plan")
|
59
|
-
current_step[
|
60
|
-
|
79
|
+
current_step["evaluation"]["consumed_time"] = (
|
80
|
+
time_now - step_start_time
|
81
|
+
) / 60
|
82
|
+
current_step_index = next(
|
83
|
+
(
|
84
|
+
i
|
85
|
+
for i, step in enumerate(current_plan["steps"])
|
86
|
+
if step["intention"] == current_step["intention"]
|
87
|
+
and step["type"] == current_step["type"]
|
88
|
+
),
|
89
|
+
None,
|
90
|
+
)
|
61
91
|
current_plan["steps"][current_step_index] = current_step
|
62
92
|
await self.memory.update("current_plan", current_plan)
|
63
|
-
if current_step_index is not None and current_step_index + 1 < len(
|
93
|
+
if current_step_index is not None and current_step_index + 1 < len(
|
94
|
+
current_plan["steps"]
|
95
|
+
):
|
64
96
|
next_step = current_plan["steps"][current_step_index + 1]
|
65
97
|
await self.memory.update("current_step", next_step)
|
66
98
|
else:
|
67
99
|
# 标记计划完成
|
68
100
|
current_plan["completed"] = True
|
69
|
-
current_plan["end_time"] = await self.simulator.get_time(
|
101
|
+
current_plan["end_time"] = await self.simulator.get_time(
|
102
|
+
format_time=True
|
103
|
+
)
|
70
104
|
await self.memory.update("current_plan", current_plan)
|
71
105
|
await self.memory.update("current_step", {"intention": "", "type": ""})
|
72
106
|
logger.info("Current plan execution completed.\n")
|
@@ -80,7 +114,7 @@ class PlanAndActionBlock(Block):
|
|
80
114
|
# 检测上一步是否执行完成
|
81
115
|
if not await self.check_and_update_step():
|
82
116
|
return
|
83
|
-
|
117
|
+
|
84
118
|
# 长期决策
|
85
119
|
await self.longTermDecisionBlock.forward()
|
86
120
|
|
@@ -102,58 +136,92 @@ class PlanAndActionBlock(Block):
|
|
102
136
|
if current_step and current_step.get("type") and current_step.get("intention"):
|
103
137
|
step_type = current_step.get("type")
|
104
138
|
position = await self.memory.get("position")
|
105
|
-
if
|
106
|
-
current_step[
|
107
|
-
current_step[
|
108
|
-
logger.info(
|
139
|
+
if "aoi_position" in position:
|
140
|
+
current_step["position"] = position["aoi_position"]["aoi_id"]
|
141
|
+
current_step["start_time"] = int(await self.simulator.get_time())
|
142
|
+
logger.info(
|
143
|
+
f"Executing step: {current_step['intention']} - Type: {step_type}"
|
144
|
+
)
|
109
145
|
result = None
|
110
146
|
if step_type == "mobility":
|
111
|
-
result = await self.mobilityBlock.forward(
|
147
|
+
result = await self.mobilityBlock.forward(
|
148
|
+
current_step, execution_context
|
149
|
+
)
|
112
150
|
elif step_type == "social":
|
113
151
|
result = await self.socialBlock.forward(current_step, execution_context)
|
114
152
|
elif step_type == "economy":
|
115
|
-
result = await self.economyBlock.forward(
|
153
|
+
result = await self.economyBlock.forward(
|
154
|
+
current_step, execution_context
|
155
|
+
)
|
116
156
|
elif step_type == "other":
|
117
157
|
result = await self.otherBlock.forward(current_step, execution_context)
|
118
158
|
if result != None:
|
119
159
|
logger.info(f"Execution result: {result}")
|
120
|
-
current_step[
|
121
|
-
|
160
|
+
current_step["evaluation"] = result
|
161
|
+
|
122
162
|
# 更新current_step信息,plan信息以及execution_context信息
|
123
|
-
current_step_index = next(
|
163
|
+
current_step_index = next(
|
164
|
+
(
|
165
|
+
i
|
166
|
+
for i, step in enumerate(current_plan["steps"])
|
167
|
+
if step["intention"] == current_step["intention"]
|
168
|
+
and step["type"] == current_step["type"]
|
169
|
+
),
|
170
|
+
None,
|
171
|
+
)
|
124
172
|
current_plan["steps"][current_step_index] = current_step
|
125
173
|
await self.memory.update("current_step", current_step)
|
126
174
|
await self.memory.update("current_plan", current_plan)
|
127
175
|
await self.memory.update("execution_context", execution_context)
|
128
176
|
|
177
|
+
|
129
178
|
class MindBlock(Block):
|
130
179
|
"""认知工作流"""
|
180
|
+
|
131
181
|
cognitionBlock: CognitionBlock
|
132
182
|
|
133
183
|
def __init__(self, llm: LLM, memory: Memory, simulator: Simulator):
|
134
184
|
super().__init__(name="mind_block", llm=llm, memory=memory, simulator=simulator)
|
135
|
-
self.cognitionBlock = CognitionBlock(
|
185
|
+
self.cognitionBlock = CognitionBlock(
|
186
|
+
llm=llm, memory=memory, simulator=simulator
|
187
|
+
)
|
136
188
|
|
137
189
|
async def forward(self):
|
138
190
|
await self.cognitionBlock.forward()
|
139
191
|
|
192
|
+
|
140
193
|
class SocietyAgent(CitizenAgent):
|
141
194
|
mindBlock: MindBlock
|
142
195
|
planAndActionBlock: PlanAndActionBlock
|
143
196
|
update_with_sim = UpdateWithSimulator()
|
144
197
|
|
145
|
-
def __init__(
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
198
|
+
def __init__(
|
199
|
+
self,
|
200
|
+
name: str,
|
201
|
+
llm_client: Optional[LLM] = None,
|
202
|
+
simulator: Optional[Simulator] = None,
|
203
|
+
memory: Optional[Memory] = None,
|
204
|
+
economy_client: Optional[EconomyClient] = None,
|
205
|
+
) -> None:
|
206
|
+
super().__init__(
|
207
|
+
name=name,
|
208
|
+
llm_client=llm_client,
|
209
|
+
simulator=simulator,
|
210
|
+
memory=memory,
|
211
|
+
economy_client=economy_client,
|
212
|
+
)
|
213
|
+
self.mindBlock = MindBlock(
|
214
|
+
llm=self._llm_client, memory=self._memory, simulator=self._simulator
|
215
|
+
)
|
216
|
+
self.planAndActionBlock = PlanAndActionBlock(
|
217
|
+
agent=self,
|
218
|
+
llm=self._llm_client,
|
219
|
+
memory=self._memory,
|
220
|
+
simulator=self._simulator,
|
221
|
+
economy_client=self._economy_client,
|
222
|
+
)
|
223
|
+
self.step_count = -1
|
224
|
+
|
157
225
|
# Main workflow
|
158
226
|
async def forward(self):
|
159
227
|
logger.info(f"Agent {self._uuid} forward")
|
@@ -166,7 +234,7 @@ class SocietyAgent(CitizenAgent):
|
|
166
234
|
await asyncio.gather(*task_list)
|
167
235
|
|
168
236
|
async def process_agent_chat_response(self, payload: dict) -> str:
|
169
|
-
if payload[
|
237
|
+
if payload["type"] == "social":
|
170
238
|
resp = f"Agent {self._uuid} received agent chat response: {payload}"
|
171
239
|
logger.info(resp)
|
172
240
|
try:
|
@@ -174,9 +242,9 @@ class SocietyAgent(CitizenAgent):
|
|
174
242
|
sender_id = payload.get("from")
|
175
243
|
if not sender_id:
|
176
244
|
return ""
|
177
|
-
|
245
|
+
|
178
246
|
raw_content = payload.get("content", "")
|
179
|
-
|
247
|
+
|
180
248
|
# Parse message content
|
181
249
|
try:
|
182
250
|
message_data = json.loads(raw_content)
|
@@ -185,32 +253,34 @@ class SocietyAgent(CitizenAgent):
|
|
185
253
|
except (json.JSONDecodeError, TypeError, KeyError):
|
186
254
|
content = raw_content
|
187
255
|
propagation_count = 1
|
188
|
-
|
256
|
+
|
189
257
|
if not content:
|
190
258
|
return ""
|
191
|
-
|
259
|
+
|
192
260
|
# Get chat histories and ensure proper format
|
193
261
|
chat_histories = await self._memory.get("chat_histories") or {}
|
194
262
|
if not isinstance(chat_histories, dict):
|
195
263
|
chat_histories = {}
|
196
|
-
|
264
|
+
|
197
265
|
# Update chat history with received message
|
198
266
|
if sender_id not in chat_histories:
|
199
267
|
chat_histories[sender_id] = ""
|
200
268
|
if chat_histories[sender_id]:
|
201
269
|
chat_histories[sender_id] += ","
|
202
270
|
chat_histories[sender_id] += f"them: {content}"
|
203
|
-
|
271
|
+
|
204
272
|
# Check propagation limit
|
205
273
|
if propagation_count > 5:
|
206
274
|
await self._memory.update("chat_histories", chat_histories)
|
207
|
-
logger.info(
|
275
|
+
logger.info(
|
276
|
+
f"Message propagation limit reached ({propagation_count} > 5), stopping propagation"
|
277
|
+
)
|
208
278
|
return ""
|
209
|
-
|
279
|
+
|
210
280
|
# Get relationship score
|
211
281
|
relationships = await self._memory.get("relationships") or {}
|
212
282
|
relationship_score = relationships.get(sender_id, 50)
|
213
|
-
|
283
|
+
|
214
284
|
# Decision prompt
|
215
285
|
should_respond_prompt = f"""Based on:
|
216
286
|
- Received message: "{content}"
|
@@ -230,10 +300,15 @@ class SocietyAgent(CitizenAgent):
|
|
230
300
|
|
231
301
|
Answer only YES or NO."""
|
232
302
|
|
233
|
-
should_respond = await self._llm_client.atext_request(
|
234
|
-
|
235
|
-
|
236
|
-
|
303
|
+
should_respond = await self._llm_client.atext_request(
|
304
|
+
[
|
305
|
+
{
|
306
|
+
"role": "system",
|
307
|
+
"content": "You are helping decide whether to respond to a message.",
|
308
|
+
},
|
309
|
+
{"role": "user", "content": should_respond_prompt},
|
310
|
+
]
|
311
|
+
)
|
237
312
|
|
238
313
|
if should_respond.strip().upper() != "YES":
|
239
314
|
await self._memory.update("chat_histories", chat_histories)
|
@@ -258,34 +333,42 @@ class SocietyAgent(CitizenAgent):
|
|
258
333
|
|
259
334
|
Response should be ONLY the message text, no explanations."""
|
260
335
|
|
261
|
-
response = await self._llm_client.atext_request(
|
262
|
-
|
263
|
-
|
264
|
-
|
336
|
+
response = await self._llm_client.atext_request(
|
337
|
+
[
|
338
|
+
{
|
339
|
+
"role": "system",
|
340
|
+
"content": "You are helping generate a chat response.",
|
341
|
+
},
|
342
|
+
{"role": "user", "content": response_prompt},
|
343
|
+
]
|
344
|
+
)
|
265
345
|
|
266
346
|
if response:
|
267
347
|
# Update chat history with response
|
268
348
|
chat_histories[sender_id] += f",me: {response}"
|
269
349
|
await self._memory.update("chat_histories", chat_histories)
|
270
|
-
|
350
|
+
|
271
351
|
# Send response
|
272
|
-
serialized_response = json.dumps(
|
273
|
-
|
274
|
-
|
275
|
-
|
352
|
+
serialized_response = json.dumps(
|
353
|
+
{
|
354
|
+
"content": response,
|
355
|
+
"propagation_count": propagation_count + 1,
|
356
|
+
},
|
357
|
+
ensure_ascii=False,
|
358
|
+
)
|
276
359
|
await self.send_message_to_agent(sender_id, serialized_response)
|
277
|
-
logger.info(
|
278
|
-
logger.info(
|
360
|
+
logger.info("sender_id", sender_id)
|
361
|
+
logger.info("message", serialized_response)
|
279
362
|
return response
|
280
363
|
|
281
364
|
except Exception as e:
|
282
365
|
logger.warning(f"Error in process_agent_chat_response: {str(e)}")
|
283
366
|
return ""
|
284
367
|
else:
|
285
|
-
content = payload[
|
368
|
+
content = payload["content"]
|
286
369
|
key, value = content.split("@")
|
287
|
-
if
|
370
|
+
if "." in value:
|
288
371
|
value = float(value)
|
289
372
|
else:
|
290
373
|
value = int(value)
|
291
|
-
await self.memory.update(key, value)
|
374
|
+
await self.memory.update(key, value)
|
@@ -48,7 +48,7 @@ class AgentGroup:
|
|
48
48
|
mlflow_run_id: str,
|
49
49
|
embedding_model: Embeddings,
|
50
50
|
logging_level: int,
|
51
|
-
agent_config_file: Union[str, list[str]] = None,
|
51
|
+
agent_config_file: Optional[Union[str, list[str]]] = None,
|
52
52
|
):
|
53
53
|
logger.setLevel(logging_level)
|
54
54
|
self._uuid = str(uuid.uuid4())
|
@@ -163,7 +163,7 @@ class AgentGroup:
|
|
163
163
|
agent.memory.set_faiss_query(self.faiss_query)
|
164
164
|
if self.embedding_model is not None:
|
165
165
|
agent.memory.set_embedding_model(self.embedding_model)
|
166
|
-
if self.agent_config_file[i]:
|
166
|
+
if self.agent_config_file is not None and self.agent_config_file[i]:
|
167
167
|
agent.load_from_file(self.agent_config_file[i])
|
168
168
|
self.agents.append(agent)
|
169
169
|
self.id2agent[agent._uuid] = agent
|