-
Notifications
You must be signed in to change notification settings - Fork 68
/
assistant_action_test.py
251 lines (231 loc) · 10.4 KB
/
assistant_action_test.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import time
import openai
import pytest
from app.providers.database import session
from app.schemas.tool.action import ActionBulkCreateRequest
from app.schemas.tool.authentication import Authentication, AuthenticationType
from app.services.tool.action import ActionService
@pytest.fixture
def api_url():
return "http://127.0.0.1:8086/api/v1/actions"
@pytest.fixture
def get_weather_data_valid_payload():
return {
"openapi_schema": {
"openapi": "3.0.0",
"info": {
"title": "OpenWeatherMap One Call API",
"description": "API for accessing comprehensive weather data from OpenWeatherMap.",
"version": "1.0.0",
},
"servers": [
{
"url": "https://api.openweathermap.org/data/3.0",
"description": "OpenWeatherMap One Call API server",
}
],
"paths": {
"/onecall": {
"get": {
"summary": "Get Comprehensive Weather Data",
"description": "Retrieves weather data for a specific latitude and longitude.",
"operationId": "get_weather_data",
"parameters": [
{
"in": "query",
"name": "lat",
"schema": {"type": "number", "format": "float", "minimum": -90, "maximum": 90},
"required": True,
"description": "Latitude, decimal (-90 to 90).",
},
{
"in": "query",
"name": "lon",
"schema": {"type": "number", "format": "float", "minimum": -180, "maximum": 180},
"required": True,
"description": "Longitude, decimal (-180 to 180).",
},
{
"in": "query",
"name": "exclude",
"schema": {"type": "string"},
"required": False,
"description": "Exclude some parts of the weather data(current, minutely, hourly, daily, alerts).",
},
{
"in": "query",
"name": "appid",
"schema": {"type": "string", "enum": ["101f41d3ff4095824722d57a513cb80a"]},
"required": True,
"description": "Your unique API key.",
},
],
"responses": {
"200": {
"description": "Successful response with comprehensive weather data.",
"content": {"application/json": {"schema": {"type": "object", "properties": {}}}},
}
},
}
}
},
}
}
@pytest.fixture
def get_number_fact_valid_payload():
return {
"openapi_schema": {
"openapi": "3.0.0",
"info": {
"title": "Numbers API",
"version": "1.0.0",
"description": "API for fetching interesting number facts",
},
"servers": [{"url": "http://numbersapi.com"}],
"paths": {
"/{number}": {
"get": {
"description": "Get fact about a number",
"operationId": "get_number_fact",
"parameters": [
{
"name": "number",
"in": "path",
"required": True,
"description": "The number to get the fact for",
"schema": {"type": "integer"},
}
],
"responses": {
"200": {
"description": "A fact about the number",
"content": {"text/plain": {"schema": {"type": "string"}}},
}
},
}
}
},
}
}
@pytest.fixture
def create_workspace_with_authentication():
return {
"openapi_schema": {
"openapi": "3.0.0",
"info": {"title": "Create New Workspace", "version": "1.0.0"},
"servers": [{"url": "https://tx.c.csvfx.com/api"}],
"paths": {
"/tx/v1/workspaces": {
"post": {
"summary": "Create a new workspace",
"description": "This endpoint creates a new workspace with the provided data.",
"operationId": "createWorkspace",
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "The name of the workspace"},
"description": {
"type": "string",
"description": "The description of the workspace",
},
"ui_settings": {
"type": "object",
"properties": {
"color": {
"type": "string",
"description": "The color of the workspace UI",
},
"icon": {
"type": "string",
"description": "The icon of the workspace UI",
},
},
},
"tenant_id": {"type": "string", "description": "The tenant ID"},
},
}
}
},
},
"responses": {
"200": {
"description": "Workspace created successfully",
"content": {"application/json": {"schema": {"type": "object", "properties": {}}}},
},
"401": {"description": "Unauthorized - Authentication credentials are missing or invalid"},
"403": {
"description": "Forbidden - The authenticated user does not have permission to perform this action"
},
"500": {"description": "Internal Server Error - Something went wrong on the server side"},
},
}
}
},
},
"authentication": {
"type": "bearer",
"secret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJhdWQiOiI2M2RiNDlmN2RjYzhiZjdiMDk5MDkwM2MiLCJ1aWQiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJpYXQiOjE3MDg5MTQ0MDcsImV4cCI6MTcwOTAwMDgwN30.vK6cH1qxPqjgeSdDym4b4fzwTwhW66s_L1suCvh3W98",
},
}
# 测试带有action的助手
def test_assistant_with_action_tools(
get_weather_data_valid_payload, get_number_fact_valid_payload, create_workspace_with_authentication
):
body = ActionBulkCreateRequest(**get_weather_data_valid_payload)
body.authentication = Authentication(
type=AuthenticationType.none,
)
actions = ActionService.create_actions_sync(session=session, body=body)
[get_weather_data] = actions
# get_number_fact
body = ActionBulkCreateRequest(**get_number_fact_valid_payload)
body.authentication = Authentication(
type=AuthenticationType.none,
)
actions = ActionService.create_actions_sync(session=session, body=body)
[get_number_fact] = actions
client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
# 创建带有 action 的 assistant
assistant = client.beta.assistants.create(
name="Assistant Demo",
instructions="你是一个有用的助手",
tools=[
{"type": "action", "id": get_weather_data.id},
{"type": "action", "id": get_number_fact.id},
],
model="gpt-3.5-turbo-1106",
)
print(assistant, end="\n\n")
thread = client.beta.threads.create()
print(thread, end="\n\n")
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="武汉的天气",
)
print(message, end="\n\n")
run = client.beta.threads.runs.create(
# model="gpt-3.5-turbo-1106",
thread_id=thread.id,
assistant_id=assistant.id,
instructions="",
)
print(run, end="\n\n")
while True:
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
if run.status == "completed":
print("done!", end="\n\n")
messages = client.beta.threads.messages.list(thread_id=thread.id)
print("messages: ")
for message in messages:
assert message.content[0].type == "text"
print(messages)
print({"role": message.role, "message": message.content[0].text.value})
break
else:
print("\nin progress...")
time.sleep(1)