Skip to content

Commit 2974110

Browse files
authored
Merge pull request #83 from YangZhiBoGreenHand/yzb/feat/add-test
feat: add test with action
2 parents 22e3ba1 + 8ee1958 commit 2974110

File tree

5 files changed

+450
-2
lines changed

5 files changed

+450
-2
lines changed

app/schemas/tool/action.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def model_validator(cls, data: Any):
8888
openapi_schema = data.get("openapi_schema")
8989
validate_openapi_schema(openapi_schema)
9090
authentication = data.get("authentication")
91-
Authentication.model_validate(authentication).encrypt()
91+
if authentication:
92+
Authentication.model_validate(authentication).encrypt()
9293
return data
9394

9495

app/services/run/run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ async def create_run(
6161
# create run
6262
db_run = Run.model_validate(body.model_dump(by_alias=True), update={"thread_id": thread_id, "file_ids": file_ids})
6363
session.add(db_run)
64-
session.refresh(db_run)
6564
run_id = db_run.id
6665
if body.additional_messages:
6766
# create messages
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
3+
import openai
4+
5+
6+
def test_run_with_assistant_extra_body():
7+
client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
8+
# 创建带有 action 的 assistant
9+
assistant = client.beta.assistants.create(
10+
name="Assistant Demo",
11+
instructions="你是一个有用的助手",
12+
model="gpt-3.5-turbo-1106",
13+
extra_body={
14+
"extra_body": {
15+
"model_params": {
16+
"frequency_penalty": 0,
17+
"logit_bias": None,
18+
"max_tokens": 1024,
19+
"presence_penalty": 0.6,
20+
"temperature": 1,
21+
"presence_penalty": 0,
22+
"top_p": 1,
23+
}
24+
}
25+
},
26+
)
27+
print(assistant, end="\n\n")
28+
29+
thread = client.beta.threads.create()
30+
print(thread, end="\n\n")
31+
32+
message = client.beta.threads.messages.create(
33+
thread_id=thread.id,
34+
role="user",
35+
content="你好,介绍一下你自己",
36+
)
37+
print(message, end="\n\n")
38+
39+
run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id, instructions="")
40+
print(run, end="\n\n")
41+
42+
while True:
43+
# run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
44+
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
45+
if run.status == "completed":
46+
print("done!", end="\n\n")
47+
messages = client.beta.threads.messages.list(thread_id=thread.id)
48+
49+
print("messages: ")
50+
for message in messages:
51+
assert message.content[0].type == "text"
52+
print(messages)
53+
print({"role": message.role, "message": message.content[0].text.value})
54+
55+
break
56+
else:
57+
print("\nin progress...")
58+
time.sleep(1)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import time
2+
3+
import openai
4+
import pytest
5+
6+
from app.providers.database import session
7+
from app.schemas.tool.action import ActionBulkCreateRequest
8+
from app.schemas.tool.authentication import Authentication, AuthenticationType
9+
from app.services.tool.action import ActionService
10+
11+
12+
@pytest.fixture
13+
def api_url():
14+
return "http://127.0.0.1:8086/api/v1/actions"
15+
16+
17+
@pytest.fixture
18+
def create_workspace_with_authentication():
19+
return {
20+
"openapi_schema": {
21+
"openapi": "3.0.0",
22+
"info": {"title": "Create New Workspace", "version": "1.0.0"},
23+
"servers": [{"url": "https://tx.c.csvfx.com/api"}],
24+
"paths": {
25+
"/tx/v1/workspaces": {
26+
"post": {
27+
"summary": "Create a new workspace",
28+
"description": "This endpoint creates a new workspace with the provided data.",
29+
"operationId": "createWorkspace",
30+
"requestBody": {
31+
"required": True,
32+
"content": {
33+
"application/json": {
34+
"schema": {
35+
"type": "object",
36+
"properties": {
37+
"name": {"type": "string", "description": "The name of the workspace"},
38+
"description": {
39+
"type": "string",
40+
"description": "The description of the workspace",
41+
},
42+
"ui_settings": {
43+
"type": "object",
44+
"properties": {
45+
"color": {
46+
"type": "string",
47+
"description": "The color of the workspace UI",
48+
},
49+
"icon": {
50+
"type": "string",
51+
"description": "The icon of the workspace UI",
52+
},
53+
},
54+
},
55+
"tenant_id": {"type": "string", "description": "The tenant ID"},
56+
},
57+
}
58+
}
59+
},
60+
},
61+
"responses": {
62+
"200": {
63+
"description": "Workspace created successfully",
64+
"content": {"application/json": {"schema": {"type": "object", "properties": {}}}},
65+
},
66+
"401": {"description": "Unauthorized - Authentication credentials are missing or invalid"},
67+
"403": {"description": "Forbidden - The authenticated user does not have permission to perform this action"},
68+
"500": {"description": "Internal Server Error - Something went wrong on the server side"},
69+
},
70+
}
71+
}
72+
},
73+
}
74+
}
75+
76+
77+
# 测试带有action的助手,run 的时候传递自己的auth信息
78+
def test_run_with_action_auth(create_workspace_with_authentication):
79+
body = ActionBulkCreateRequest(**create_workspace_with_authentication)
80+
body.authentication = Authentication(type=AuthenticationType.none)
81+
actions = ActionService.create_actions_sync(session=session, body=body)
82+
[create_workspace_with_authentication] = actions
83+
84+
client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
85+
86+
# 创建带有 action 的 assistant
87+
assistant = client.beta.assistants.create(
88+
name="Assistant Demo",
89+
instructions="你是一个有用的助手",
90+
tools=[{"type": "action", "id": create_workspace_with_authentication.id}],
91+
model="gpt-3.5-turbo-1106",
92+
)
93+
print(assistant, end="\n\n")
94+
95+
thread = client.beta.threads.create()
96+
print(thread, end="\n\n")
97+
98+
message = client.beta.threads.messages.create(
99+
thread_id=thread.id,
100+
role="user",
101+
content="在组织63db49f7dcc8bf7b0990903c下,创建一个随机名字的工作空间",
102+
)
103+
print(message, end="\n\n")
104+
105+
run = client.beta.threads.runs.create(
106+
# model="gpt-3.5-turbo-1106",
107+
thread_id=thread.id,
108+
assistant_id=assistant.id,
109+
instructions="",
110+
extra_body={
111+
"extra_body": {
112+
"action_authentications": {
113+
create_workspace_with_authentication.id: {
114+
"type": "bearer",
115+
"secret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJhdWQiOiI2M2RiNDlmN2RjYzhiZjdiMDk5MDkwM2MiLCJ1aWQiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJpYXQiOjE3MTAxNDkxODcsImV4cCI6MTcxMDIzNTU4N30.h96cKhB8rPGKM2PEq6bg4k2j09gR82HCJHUws232Oe4",
116+
}
117+
}
118+
}
119+
},
120+
)
121+
print(run, end="\n\n")
122+
123+
while True:
124+
# run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
125+
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
126+
if run.status == "completed":
127+
print("done!", end="\n\n")
128+
messages = client.beta.threads.messages.list(thread_id=thread.id)
129+
130+
print("messages: ")
131+
for message in messages:
132+
assert message.content[0].type == "text"
133+
print(messages)
134+
print({"role": message.role, "message": message.content[0].text.value})
135+
136+
break
137+
else:
138+
print("\nin progress...")
139+
time.sleep(1)

0 commit comments

Comments
 (0)