-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_use_agent_demo.py
56 lines (49 loc) · 1.5 KB
/
tool_use_agent_demo.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
import os
from autogen import ConversableAgent, register_function
from typing import Annotated, Literal
llm_config = {
"config_list": [
{
"model": "gpt-4",
"api_type": "azure",
"api_key": os.environ['AZURE_OPENAI_API_KEY'],
"base_url": os.environ['AZURE_OPENAI_BASE_URL'],
"api_version": os.environ['AZURE_OPENAI_API_VERSION'],
"max_tokens": 4096,
"temperature": 0.5,
}
]
}
Operator = Literal["+", "-", "*", "/"]
def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:
if operator == "+":
return a + b
elif operator == "-":
return a - b
elif operator == "*":
return a * b
elif operator == "/":
return int(a / b)
else:
raise ValueError("Invalid operator")
assistant = ConversableAgent(
name = "Assistant",
system_message="你是一个有用的ai助手。你可以帮助进行简单的计算。任务完成后返回“TERMINATE”。",
llm_config=llm_config,
)
user_proxy = ConversableAgent(
name = "User",
llm_config=False,
human_input_mode="NEVER",
is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
)
# 同时向两个代理注册工具
register_function(
calculator,
caller=assistant,
executor=user_proxy,
name="calculator",
description="一个简单的计算器",
)
chat_result = user_proxy.initiate_chat(assistant, message="(44232 + 13312 / (232 - 32)) * 5的结果是多少?")
print(chat_result.summary)