Skip to content

Commit

Permalink
Merge pull request #83 from YangZhiBoGreenHand/yzb/feat/add-test
Browse files Browse the repository at this point in the history
feat: add test with action
  • Loading branch information
liuooo committed Sep 13, 2024
2 parents 22e3ba1 + 8ee1958 commit 2974110
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/schemas/tool/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def model_validator(cls, data: Any):
openapi_schema = data.get("openapi_schema")
validate_openapi_schema(openapi_schema)
authentication = data.get("authentication")
Authentication.model_validate(authentication).encrypt()
if authentication:
Authentication.model_validate(authentication).encrypt()
return data


Expand Down
1 change: 0 additions & 1 deletion app/services/run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ async def create_run(
# create run
db_run = Run.model_validate(body.model_dump(by_alias=True), update={"thread_id": thread_id, "file_ids": file_ids})
session.add(db_run)
session.refresh(db_run)
run_id = db_run.id
if body.additional_messages:
# create messages
Expand Down
58 changes: 58 additions & 0 deletions tests/run/run_with_assistant_extra_body_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import time

import openai


def test_run_with_assistant_extra_body():
client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
# 创建带有 action 的 assistant
assistant = client.beta.assistants.create(
name="Assistant Demo",
instructions="你是一个有用的助手",
model="gpt-3.5-turbo-1106",
extra_body={
"extra_body": {
"model_params": {
"frequency_penalty": 0,
"logit_bias": None,
"max_tokens": 1024,
"presence_penalty": 0.6,
"temperature": 1,
"presence_penalty": 0,
"top_p": 1,
}
}
},
)
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(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)
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)
139 changes: 139 additions & 0 deletions tests/run/run_with_auth_action_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
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 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"},
},
}
}
},
}
}


# 测试带有action的助手,run 的时候传递自己的auth信息
def test_run_with_action_auth(create_workspace_with_authentication):
body = ActionBulkCreateRequest(**create_workspace_with_authentication)
body.authentication = Authentication(type=AuthenticationType.none)
actions = ActionService.create_actions_sync(session=session, body=body)
[create_workspace_with_authentication] = 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": create_workspace_with_authentication.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="在组织63db49f7dcc8bf7b0990903c下,创建一个随机名字的工作空间",
)
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="",
extra_body={
"extra_body": {
"action_authentications": {
create_workspace_with_authentication.id: {
"type": "bearer",
"secret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJhdWQiOiI2M2RiNDlmN2RjYzhiZjdiMDk5MDkwM2MiLCJ1aWQiOiI2M2RiNDlhY2RjYzhiZjdiMDk5MDhmZDYiLCJpYXQiOjE3MTAxNDkxODcsImV4cCI6MTcxMDIzNTU4N30.h96cKhB8rPGKM2PEq6bg4k2j09gR82HCJHUws232Oe4",
}
}
}
},
)
print(run, end="\n\n")

while True:
# run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
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)
Loading

0 comments on commit 2974110

Please sign in to comment.