|
1 | 1 | import time
|
| 2 | +import logging |
| 3 | +import requests |
| 4 | +import json |
2 | 5 |
|
3 |
| -import openai |
4 |
| -import pytest |
| 6 | +from app.exceptions.exception import BadRequestError |
| 7 | +from examples.prerun import client |
| 8 | +from examples.prerun import base_url |
| 9 | +from examples.prerun import api_key |
5 | 10 |
|
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 | +# To test the localhost, you can listen to a port using the shell command 'echo -e "HTTP/1.1 200 OK\r\n\r\n Success" | nc -l 9999'. |
| 13 | +# Make sure to change the URL to match your API server. |
| 14 | +auth_server_url = "http://localhost:9999/api/v1" |
11 | 15 |
|
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 { |
| 16 | +def create_worksapce_action(): |
| 17 | + """ |
| 18 | + create action with actions api |
| 19 | + """ |
| 20 | + openapi_schema = { |
20 | 21 | "openapi_schema": {
|
21 | 22 | "openapi": "3.0.0",
|
22 | 23 | "info": {"title": "Create New Workspace", "version": "1.0.0"},
|
23 |
| - "servers": [{"url": "https://tx.c.csvfx.com/api"}], |
| 24 | + "servers": [{"url": f"{auth_server_url}"}], |
24 | 25 | "paths": {
|
25 | 26 | "/tx/v1/workspaces": {
|
26 | 27 | "post": {
|
@@ -74,68 +75,73 @@ def create_workspace_with_authentication():
|
74 | 75 | },
|
75 | 76 | }
|
76 | 77 | }
|
| 78 | + openapi_schema["authentication"] = {"type": "none"} |
| 79 | + actions_url = f"{base_url}/actions" |
| 80 | + headers = { |
| 81 | + 'Content-Type': 'application/json', |
| 82 | + 'Authorization': f'Bearer {api_key}' |
| 83 | + } |
| 84 | + response = requests.request("POST", actions_url, headers=headers, data=json.dumps(openapi_schema), timeout=1000) |
| 85 | + if response.status_code != 200: |
| 86 | + raise BadRequestError(f"Failed to create action: {response.text}") |
| 87 | + return response.json() |
77 | 88 |
|
78 | 89 |
|
79 |
| -# 测试带有action的助手,run 的时候传递自己的auth信息 |
80 |
| -def test_run_with_action_auth(create_workspace_with_authentication): |
81 |
| - body = ActionBulkCreateRequest(**create_workspace_with_authentication) |
82 |
| - body.authentication = Authentication(type=AuthenticationType.none) |
83 |
| - actions = ActionService.create_actions_sync(session=session, body=body) |
84 |
| - [create_workspace_with_authentication] = actions |
85 |
| - |
86 |
| - client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx") |
| 90 | +if __name__ == "__main__": |
| 91 | + [create_workspace_with_authentication] = create_worksapce_action() |
| 92 | + logging.info("=====> action: %s\n", create_workspace_with_authentication) |
87 | 93 |
|
88 |
| - # 创建带有 action 的 assistant |
| 94 | + # create a assistant with action |
89 | 95 | assistant = client.beta.assistants.create(
|
90 | 96 | name="Assistant Demo",
|
91 |
| - instructions="你是一个有用的助手", |
92 |
| - tools=[{"type": "action", "id": create_workspace_with_authentication.id}], |
| 97 | + instructions="you are a personal assistant", |
| 98 | + tools=[{"type": "action", "id": create_workspace_with_authentication["id"]}], |
93 | 99 | model="gpt-3.5-turbo-1106",
|
94 | 100 | )
|
95 |
| - print(assistant, end="\n\n") |
| 101 | + logging.info("=====> : %s\n", assistant) |
96 | 102 |
|
97 | 103 | thread = client.beta.threads.create()
|
98 |
| - print(thread, end="\n\n") |
| 104 | + logging.info("=====> : %s\n", thread) |
99 | 105 |
|
100 | 106 | message = client.beta.threads.messages.create(
|
101 | 107 | thread_id=thread.id,
|
102 | 108 | role="user",
|
103 |
| - content="在组织63db49f7dcc8bf7b0990903c下,创建一个随机名字的工作空间", |
| 109 | + content="在组织 63db49f7dcc8bf7b0990903c 下, 创建一个随机名字的工作空间", |
104 | 110 | )
|
105 |
| - print(message, end="\n\n") |
| 111 | + logging.info("=====> : %s\n", message) |
106 | 112 |
|
| 113 | + # create a run with auth info |
107 | 114 | run = client.beta.threads.runs.create(
|
108 |
| - # model="gpt-3.5-turbo-1106", |
109 | 115 | thread_id=thread.id,
|
110 | 116 | assistant_id=assistant.id,
|
111 | 117 | instructions="",
|
112 | 118 | extra_body={
|
113 | 119 | "extra_body": {
|
114 | 120 | "action_authentications": {
|
115 |
| - create_workspace_with_authentication.id: { |
| 121 | + create_workspace_with_authentication["id"]: { |
| 122 | + # auth info, change as needed |
116 | 123 | "type": "bearer",
|
117 |
| - "secret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJhdWQiOiI2M2RiNDlmN2RjYzhiZjdiMDk5MDkwM2MiLCJ1aWQiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJpYXQiOjE3MTAxNDkxODcsImV4cCI6MTcxMDIzNTU4N30.h96cKhB8rPGKM2PEq6bg4k2j09gR82HCJHUws232Oe4", |
| 124 | + "secret": "xxx", |
118 | 125 | }
|
119 | 126 | }
|
120 | 127 | }
|
121 | 128 | },
|
122 | 129 | )
|
123 |
| - print(run, end="\n\n") |
| 130 | + logging.info("=====> : %s\n", run) |
124 | 131 |
|
125 | 132 | while True:
|
126 |
| - # run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id) |
127 | 133 | run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
|
128 | 134 | if run.status == "completed":
|
129 |
| - print("done!", end="\n\n") |
130 | 135 | messages = client.beta.threads.messages.list(thread_id=thread.id)
|
131 | 136 |
|
132 |
| - print("messages: ") |
| 137 | + logging.info("=====> messages:") |
133 | 138 | for message in messages:
|
134 | 139 | assert message.content[0].type == "text"
|
135 |
| - print(messages) |
136 |
| - print({"role": message.role, "message": message.content[0].text.value}) |
137 |
| - |
| 140 | + logging.info("%s", {"role": message.role, "message": message.content[0].text.value}) |
| 141 | + break |
| 142 | + elif run.status == "failed": |
| 143 | + logging.error("run failed %s\n", run.last_error) |
138 | 144 | break
|
139 | 145 | else:
|
140 |
| - print("\nin progress...") |
141 |
| - time.sleep(1) |
| 146 | + logging.info("in progress...\n") |
| 147 | + time.sleep(5) |
0 commit comments