|
| 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