From b708a12f949fad037ff7ee70eeae9d17c0f849b8 Mon Sep 17 00:00:00 2001 From: ideoutrea Date: Wed, 20 Mar 2024 16:29:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E6=B5=8B=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../console/agent_builder/agent_builder.py | 11 ++++- .../core/console/agent_builder/model.py | 10 ----- appbuilder/tests/test_agent_builder.py | 45 +++++++++++++++++++ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 appbuilder/tests/test_agent_builder.py diff --git a/appbuilder/core/console/agent_builder/agent_builder.py b/appbuilder/core/console/agent_builder/agent_builder.py index 0179ccaa2..63d669935 100644 --- a/appbuilder/core/console/agent_builder/agent_builder.py +++ b/appbuilder/core/console/agent_builder/agent_builder.py @@ -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), @@ -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, @@ -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: @@ -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)) \ No newline at end of file + content_type=ev.content_type, detail=ev.outputs)) diff --git a/appbuilder/core/console/agent_builder/model.py b/appbuilder/core/console/agent_builder/model.py index 7b7e6df32..2a02cbe5f 100644 --- a/appbuilder/core/console/agent_builder/model.py +++ b/appbuilder/core/console/agent_builder/model.py @@ -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,详情内容 属性: diff --git a/appbuilder/tests/test_agent_builder.py b/appbuilder/tests/test_agent_builder.py new file mode 100644 index 000000000..4c477dfb7 --- /dev/null +++ b/appbuilder/tests/test_agent_builder.py @@ -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()