-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_Create_Assitant.py
96 lines (84 loc) · 3.12 KB
/
01_Create_Assitant.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
# 导入环境变量
from dotenv import load_dotenv
load_dotenv()
# 导入OpenAI库,并创建OpenAI客户端
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
instructions="您是一个订单助手。请使用提供的函数来计算订单总价并回答问题。",
model="gpt-4-1106-preview",
tools=[{
"type": "function",
"function": {
"name": "calculate_order_total",
"description": "根据多个商品类型和数量计算订单总价",
"parameters": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item_type": {
"type": "string",
"description": "商品类型,例如:书籍,文具,电子产品"
},
"quantity": {
"type": "integer",
"description": "商品数量"
}
},
"required": [
"item_type",
"quantity"
]
}
}
},
"required": [
"items"
]
}
}
}]
)
print(assistant)
# 创建一个新的Thread
thread = client.beta.threads.create()
print(thread)
# 向Thread添加用户的消息
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="你好,我购买了一本书和一个电子产品,请帮我计算一下订单总价。"
)
print(message)
# 运行Assistant来处理Thread
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
print("读取function元数据信息之前Run的状态", run)
import time
# 定义一个轮询的函数
def poll_run_status(client, thread_id, run_id, interval=1):
while True:
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
print(f"Run的轮询信息:\n{run}\n")
if run.status in ['requires_action', 'completed']:
return run
time.sleep(interval) # 等待后再次检查
# 轮询以检查Run的状态
run = poll_run_status(client, thread.id, run.id)
# 读取function元数据信息
def get_function_details(run):
function_name = run.required_action.submit_tool_outputs.tool_calls[0].function.name
arguments = run.required_action.submit_tool_outputs.tool_calls[0].function.arguments
function_id = run.required_action.submit_tool_outputs.tool_calls[0].id
return function_name, arguments, function_id
# 读取并打印元数据信息
function_name, arguments, function_id = get_function_details(run)
print("function_name:", function_name)
print("arguments:", arguments)
print("function_id:", function_id)