run_with_auth_action_test.py 6.1 KB

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