-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv2.py
122 lines (106 loc) · 3.61 KB
/
v2.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
from openai import OpenAI
import json
client = OpenAI(
api_key="65b0348119a6b858ee16dda43ede5a8c.DFnYvPKSZDO5ciKL",
base_url="https://open.bigmodel.cn/api/paas/v4/"
)
function_description = [
{
"type": "function",
"function": {
"name": "get_flight_info",
"description": "get flight info between two airports",
"parameters": {
"type": "object",
"properties": {
"loc_origin": {
"type": "string",
"description": ""
},
"loc_destination": {
"type": "string",
"description": ""
}
},
"required": ["loc_origin", "loc_destination"]
},
}
}
]
user_prompt = "what's the next flight from london to new york"
def get_flight_info(loc_origin, loc_destination):
print(f"出发地是{loc_origin}")
print(f"终点是{loc_destination}")
return "早上七点"
completion = client.chat.completions.create(
model="glm-4-flash",
messages=[
{
"role": "user",
"content": user_prompt
}
],
tools=function_description
)
"""(v1.py)
这个create函数,可以实现参数抽取,给出函数名
"""
# test2.py 修改FuncSet为ToolSet
class ToolSet:
def __init__(self, callable_func):
self.funcs = callable_func # Store as instance variable
def __getitem__(self, name): # Add subscript support
return self.funcs[name]
tool_set = ToolSet({
"get_flight_info": get_flight_info
})
"""(v2.py)
分析执行一个agent,普遍的框架应该是怎么样的。需要有prompt,llm,tool,memory,
目前的执行过程是,
1,调用openai的create函数,根据prompt,返回tool所需要的参数。
2,执行tool,返回结果
我期望的调用过程:
1,构建一个对象
2,调用对象的run函数。
因此,这时需要一个agent类。
"""
# test2.py 试着写一个Agent类
class Agent:
def __init__(self, prompt, llm, tool, model):
self.prompt = prompt
self.llm = llm # llm是一个client
self.tool = tool
self.model = model
def run(self):
completion = client.chat.completions.create(
model=self.model,
messages=[
{
"role": "user",
"content": self.prompt
}
],
tools=function_description
)
response = completion.choices[0].message
args = json.loads(response.tool_calls[0].function.arguments)
res = tool_set[response.tool_calls[0].function.name](args["loc_origin"], args["loc_destination"])
print(f"出发时间是{res}")
# v2.py 试着生成一个Agent类对象
agent = Agent(
model="glm-4-flash",
prompt=user_prompt,
llm=client,
tool=tool_set
)
agent.run()
response = completion.choices[0].message
args = json.loads(response.tool_calls[0].function.arguments)
if response.tool_calls[0].function.name == "get_flight_info":
res = tool_set[response.tool_calls[0].function.name](args["loc_origin"], args["loc_destination"])
print(f"出发时间是{res}")
"""(v1.py)
之所以要以对象的形式,注册函数,因为每次执行函数,都需要if-else判断,代码不灵活。
之前无法管理函数,只能用if-else的形式去判断执行哪个函数,现在可以结合openai的function calling管理函数
这个函数可以看做一个工具,tool
"""