From 1bd8d3bbe9e382bc6496739f17e073553903386e Mon Sep 17 00:00:00 2001 From: userpj Date: Fri, 20 Dec 2024 13:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=82=E6=AD=A5toolcall?= =?UTF-8?q?=E5=8D=95=E6=B5=8B=E5=B9=B6=E5=8F=91interrupt=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/core/_client.py | 6 +++--- .../appbuilder_client/async_appbuilder_client.py | 8 ++++---- .../appbuilder_client/async_event_handler.py | 6 +++--- .../tests/test_async_appbuilder_client_toolcall.py | 13 +++++++------ 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/python/core/_client.py b/python/core/_client.py index 83a2c524..2f9b87dc 100644 --- a/python/core/_client.py +++ b/python/core/_client.py @@ -243,7 +243,7 @@ def __init__(self, secret_key=None, gateway="", gateway_v2=""): self.session = AsyncInnerSession() @staticmethod - def check_response_header(response: ClientResponse): + async def check_response_header(response: ClientResponse): r"""check_response_header is a helper method for check head status . :param response: requests.Response. :rtype: @@ -252,7 +252,7 @@ def check_response_header(response: ClientResponse): if status_code == requests.codes.ok: return message = "request_id={} , http status code is {}, body is {}".format( - __class__.response_request_id(response), status_code, response.text + await __class__.response_request_id(response), status_code, await response.text() ) if status_code == requests.codes.bad_request: raise BadRequestException(message) @@ -268,7 +268,7 @@ def check_response_header(response: ClientResponse): raise BaseRPCException(message) @staticmethod - def response_request_id(response: ClientResponse): + async def response_request_id(response: ClientResponse): r"""response_request_id is a helper method to get the unique request id""" return response.headers.get("X-Appbuilder-Request-Id", "") diff --git a/python/core/console/appbuilder_client/async_appbuilder_client.py b/python/core/console/appbuilder_client/async_appbuilder_client.py index f6f010f2..e116ac6c 100644 --- a/python/core/console/appbuilder_client/async_appbuilder_client.py +++ b/python/core/console/appbuilder_client/async_appbuilder_client.py @@ -51,7 +51,7 @@ async def create_conversation(self) -> str: response = await self.http_client.session.post( url, headers=headers, json={"app_id": self.app_id}, timeout=None ) - self.http_client.check_response_header(response) + await self.http_client.check_response_header(response) data = await response.json() resp = data_class.CreateConversationResponse(**data) return resp.conversation_id @@ -116,8 +116,8 @@ async def run( response = await self.http_client.session.post( url, headers=headers, json=req.model_dump(), timeout=None ) - self.http_client.check_response_header(response) - request_id = self.http_client.response_request_id(response) + await self.http_client.check_response_header(response) + request_id = await self.http_client.response_request_id(response) if stream: client = AsyncSSEClient(response) return Message(content=self._iterate_events(request_id, client.events())) @@ -164,7 +164,7 @@ async def upload_local_file(self, conversation_id, local_file_path: str) -> str: response = await self.http_client.session.post( url, data=multipart_form_data, headers=headers ) - self.http_client.check_response_header(response) + await self.http_client.check_response_header(response) data = await response.json() resp = data_class.FileUploadResponse(**data) return resp.id diff --git a/python/core/console/appbuilder_client/async_event_handler.py b/python/core/console/appbuilder_client/async_event_handler.py index afa93eef..992b3833 100644 --- a/python/core/console/appbuilder_client/async_event_handler.py +++ b/python/core/console/appbuilder_client/async_event_handler.py @@ -123,13 +123,13 @@ async def __async_run_process__(self): while not self._is_complete: if not self._need_tool_call: res = await self._run() - self.__event_process__(res) + await self.__event_process__(res) else: res = await self._submit_tool_output() - self.__event_process__(res) + await self.__event_process__(res) yield res if self._need_tool_call and self._is_complete: - self.reset_state() + await self.reset_state() async def __event_process__(self, run_response): """ diff --git a/python/tests/test_async_appbuilder_client_toolcall.py b/python/tests/test_async_appbuilder_client_toolcall.py index 00322bf3..1e03f834 100644 --- a/python/tests/test_async_appbuilder_client_toolcall.py +++ b/python/tests/test_async_appbuilder_client_toolcall.py @@ -35,7 +35,8 @@ async def interrupt(self, run_context, run_response): tool_call_id = tool_call.id tool_res = self.get_current_weather(**tool_call.function.arguments) # 蓝色打印 - print("\033[1;34m", "-> 本地ToolCall结果: ", tool_res, "\033[0m\n") + print("\033[1;34m", "-> 本地ToolCallId: ", tool_call_id, "\033[0m") + print("\033[1;34m", "-> ToolCall结果: ", tool_res, "\033[0m\n") tool_output.append( {"tool_call_id": tool_call_id, "output": tool_res}) return tool_output @@ -92,9 +93,10 @@ def test_appbuilder_client_tool_call(self): } ] - appbuilder.logger.setLoglevel("ERROR") + appbuilder.logger.setLoglevel("DEBUG") - async def agent_run(client, conversation_id, query): + async def agent_run(client, query): + conversation_id = await client.create_conversation() with await client.run_with_handler( conversation_id=conversation_id, query=query, @@ -105,11 +107,10 @@ async def agent_run(client, conversation_id, query): async def agent_handle(): client = appbuilder.AsyncAppBuilderClient(self.app_id) - conversation_id = await client.create_conversation() task1 = asyncio.create_task( - agent_run(client, conversation_id, "北京的天气怎么样")) + agent_run(client, "北京的天气怎么样")) task2 = asyncio.create_task( - agent_run(client, conversation_id, "上海的天气怎么样")) + agent_run(client, "上海的天气怎么样")) await asyncio.gather(task1, task2) await client.http_client.session.close()