Skip to content

Commit

Permalink
增加单测用例
Browse files Browse the repository at this point in the history
  • Loading branch information
ide-rea committed Mar 20, 2024
1 parent 47d4b79 commit b708a12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
11 changes: 9 additions & 2 deletions appbuilder/core/console/agent_builder/agent_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def upload_local_file(self, conversation_id, local_file_path: str) -> str:
返回:
response (str: ): 唯一文件ID
"""

if len(conversation_id) == 0:
raise ValueError("conversation_id is empty")
multipart_form_data = {
'file': (os.path.basename(local_file_path), open(local_file_path, 'rb')),
'app_id': (None, self.app_id),
Expand Down Expand Up @@ -109,6 +112,10 @@ def run(self, conversation_id: str,
stream (bool, 可选): 为True时,流式返回,需要将message.content.answer拼接起来才是完整的回答;为False时,对应非流式返回
返回: message (obj: `Message`): 对话结果.
"""

if len(conversation_id) == 0:
raise ValueError("conversation_id is empty")

req = HTTPRequest(
app_id=self.app_id,
conversation_id=conversation_id,
Expand All @@ -120,7 +127,7 @@ def run(self, conversation_id: str,
headers = self.http_client.auth_header()
headers["Content-Type"] = "application/json"
url = self.http_client.service_url("/v1/ai_engine/agi_platform/v1/instance/integrated", '/api')
response = self.http_client.session.post(url, headers=headers, json=req.dict(), timeout=None, stream=True)
response = self.http_client.session.post(url, headers=headers, json=req.model_dump(), timeout=None, stream=True)
self.http_client.check_response_header(response)
request_id = self.http_client.response_request_id(response)
if stream:
Expand Down Expand Up @@ -167,4 +174,4 @@ def _transform(inp: HTTPResponse, out: AgentBuilderAnswer):
for ev in inp.result.content:
out.events.append(Event(code=ev.event_code, message=ev.event_message,
status=ev.event_status, event_type=ev.event_type,
content_type=ev.content_type, detail=ev.outputs))
content_type=ev.content_type, detail=ev.outputs))
10 changes: 0 additions & 10 deletions appbuilder/core/console/agent_builder/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ class CodeDetail(BaseModel):
files: list[str] = []


class ImageDetail(BaseModel):
"""content_type=image,详情内容
属性:
text(str): 文本详情
url(str):资源下载地址
"""
text: str = ""
url: str = ""


class RAGDetail(BaseModel):
"""content_type=image,详情内容
属性:
Expand Down
45 changes: 45 additions & 0 deletions appbuilder/tests/test_agent_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
import appbuilder
import requests
import tempfile
import os


class TestAgentRuntime(unittest.TestCase):
def setUp(self):
"""
设置环境变量。
Args:
无参数,默认值为空。
Returns:
无返回值,方法中执行了环境变量的赋值操作。
"""
os.environ["APPBUILDER_TOKEN"] = ""
self.app_id = ""

def test_agent_builder_run(self):
# 如果app_id为空,则跳过单测执行, 避免单测因配置无效而失败
if len(self.app_id) == 0:
self.skipTest("self.app_id is empty")

agent_builder = appbuilder.AgentBuilder(self.app_id)
conversation_id = agent_builder.create_conversation()

with tempfile.NamedTemporaryFile(suffix=".png") as fp:
# 上传植物图片
img_url = ("https://bj.bcebos.com/v1/appbuilder/test_agent_builder_tr"
"ee.png?authorization=bce-auth-v1%2FALTAKGa8m4qCUasgoljdEDAzL"
"m%2F2024-03-20T08%3A03%3A16Z%2F-1%2Fhost%2F8227f2bb97928b1957a9a6"
"c14c4e307ef195d18ec68b22764158690cecbd9fc7")
raw_image = requests.get(img_url).content
fp.write(raw_image)
file_id = agent_builder.upload_local_file(conversation_id, fp.name)
msg = agent_builder.run(conversation_id, "请识别图中的植物类别", file_ids=[file_id])
print("助理回答内容:", msg.content.answer)
fp.close()


if __name__ == '__main__':
unittest.main()

0 comments on commit b708a12

Please sign in to comment.