Skip to content

Commit a92cd6d

Browse files
committed
updated request interceptor.
1 parent f1b9c64 commit a92cd6d

File tree

6 files changed

+96
-46
lines changed

6 files changed

+96
-46
lines changed

docs/development/api/mas.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ oxygent/mas.py
5858
| `show_mas_info()` | No | `None` | Display MAS initialization information |
5959
| `show_org()` | No | `None` | Display organization structure |
6060
| `init_global_data()` | No | `None` | Initialize the in-memory global data store |
61-
| `get_global()` | No | `Any` | Get value from global data store |
62-
| `set_global()` | No | `None` | Set value in global data store |
6361
| `is_agent()` | No | `bool` | Check if an oxy name is an agent |
6462
| `init_master_agent_name()` | No | `None` | Initialize the master agent name |
6563
| `init_agent_organization()` | No | `None` | Build agent organization structure |

docs/docs_zh/3_2_set_global.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
OxyGent支持使用非常简单的方式设置和修改系统全局数据,这些数据类似于全局变量,能够在MAS中使用`OxyRequest`进行更改与访问。
44

55
支持的方法包括:
6-
+ `get_global`:使用`(key,default_value)`按键值访问全局数据
7-
+ `set_global`:使用`(key,value)`按键值修改全局数据
8-
+ `get_all_global`:获取所有的全局数据(返回`dict`
6+
+ `get_global_data`:使用`(key,default_value)`按键值访问全局数据
7+
+ `set_global_data`:使用`(key,value)`按键值修改全局数据
98

109
下面使用全局数据实现简单的计数器。
1110

1211
```python
1312
class CounterAgent(BaseAgent):
1413
async def execute(self, oxy_request: OxyRequest):
15-
cnt = oxy_request.get_global("counter", 0) + 1 # 获取计数
16-
oxy_request.set_global("counter", cnt) # 存储计数+1
14+
cnt = oxy_request.get_global_data("counter", 0) + 1 # 获取计数
15+
oxy_request.set_global_data("counter", cnt) # 存储计数+1
1716

1817
return OxyResponse(
1918
state=OxyState.COMPLETED,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Demo for using OxyGent with multiple LLMs and an agent."""
2+
3+
import asyncio
4+
import os
5+
6+
from oxygent import MAS, Config, OxyRequest, oxy
7+
8+
Config.set_es_schema_shared_data(
9+
{
10+
"properties": {
11+
"user_pin": {"type": "keyword"},
12+
"user_name": {"type": "keyword"},
13+
}
14+
}
15+
)
16+
17+
18+
def process_input(oxy_request: OxyRequest) -> OxyRequest:
19+
oxy_request.set_shared_data("user_pin", "123456")
20+
oxy_request.set_shared_data("user_name", "oxy")
21+
return oxy_request
22+
23+
24+
oxy_space = [
25+
oxy.HttpLLM(
26+
name="default_llm",
27+
api_key=os.getenv("DEFAULT_LLM_API_KEY"),
28+
base_url=os.getenv("DEFAULT_LLM_BASE_URL"),
29+
model_name=os.getenv("DEFAULT_LLM_MODEL_NAME"),
30+
),
31+
oxy.ReActAgent(
32+
name="master_agent",
33+
llm_model="default_llm",
34+
func_process_input=process_input,
35+
),
36+
]
37+
38+
39+
async def main():
40+
async with MAS(oxy_space=oxy_space) as mas:
41+
await mas.start_web_service(first_query="hello")
42+
43+
44+
if __name__ == "__main__":
45+
asyncio.run(main())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import asyncio
2+
import os
3+
4+
from oxygent import MAS, Config, oxy
5+
6+
Config.set_llm_config({}) # 重置LLM默认参数,以适配GPT-5模型
7+
8+
9+
oxy_space = [
10+
oxy.HttpLLM(
11+
name="default_llm",
12+
api_key=os.getenv("DEFAULT_LLM_API_KEY"),
13+
base_url=os.getenv("DEFAULT_LLM_BASE_URL"),
14+
model_name=os.getenv("DEFAULT_LLM_MODEL_NAME"),
15+
llm_params={"thinking": False, "stream": False},
16+
),
17+
]
18+
19+
20+
async def main():
21+
async with MAS(oxy_space=oxy_space) as mas:
22+
await mas.call(
23+
callee="default_llm",
24+
arguments={"messages": [{"role": "user", "content": "hello"}]},
25+
)
26+
27+
28+
if __name__ == "__main__":
29+
asyncio.run(main())

examples/llms/ollama_demo.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,16 @@
1010
name="default_llm",
1111
base_url="http://localhost:11434/api/chat",
1212
model_name=os.getenv("DEFAULT_OLLAMA_MODEL"),
13-
semaphore=1,
14-
),
15-
oxy.ChatAgent(
16-
name="master_agent",
17-
is_master=True,
18-
llm_model="default_llm",
1913
),
2014
]
2115

2216

2317
async def main():
2418
async with MAS(oxy_space=oxy_space) as mas:
25-
history = [{"role": "system", "content": "You are a helpful assistant."}]
26-
27-
while True:
28-
user_in = input("User: ").strip()
29-
if user_in.lower() in {"exit", "quit", "q"}:
30-
break
31-
32-
history.append({"role": "user", "content": user_in})
33-
result = await mas.call(
34-
callee="master_agent",
35-
arguments={"messages": history},
36-
)
37-
assistant_out = result
38-
print(f"Assistant: {assistant_out}\n")
39-
history.append({"role": "assistant", "content": assistant_out})
19+
await mas.call(
20+
callee="default_llm",
21+
arguments={"messages": [{"role": "user", "content": "hello"}]},
22+
)
4023

4124

4225
if __name__ == "__main__":

oxygent/oxy/base_oxy.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,30 +252,26 @@ async def _pre_log(self, oxy_request: OxyRequest):
252252
async def _request_interceptor(self, oxy_request: OxyRequest):
253253
if (
254254
oxy_request.reference_trace_id
255+
and oxy_request.restart_node_id
255256
and oxy_request.is_load_data_for_restart
256257
and self.mas
257258
and self.mas.es_client
258259
and self.category in ["llm", "tool"]
259260
):
260-
if oxy_request.restart_node_id:
261-
es_response = await self.mas.es_client.search(
262-
Config.get_app_name() + "_node",
263-
{
264-
"query": {
265-
"bool": {
266-
"must": [
267-
{
268-
"term": {
269-
"trace_id": oxy_request.reference_trace_id
270-
}
271-
},
272-
{"term": {"input_md5": oxy_request.input_md5}},
273-
]
274-
}
275-
},
276-
"size": 1,
261+
es_response = await self.mas.es_client.search(
262+
Config.get_app_name() + "_node",
263+
{
264+
"query": {
265+
"bool": {
266+
"must": [
267+
{"term": {"trace_id": oxy_request.reference_trace_id}},
268+
{"term": {"input_md5": oxy_request.input_md5}},
269+
]
270+
}
277271
},
278-
)
272+
"size": 1,
273+
},
274+
)
279275
logging.info(f"ES search returned {len(es_response['hits']['hits'])} hits")
280276
if es_response["hits"]["hits"]:
281277
current_node_order = es_response["hits"]["hits"][0]["_source"][

0 commit comments

Comments
 (0)