Skip to content

Commit

Permalink
使用异步方式调用tool,解决当tool为agent时的阻塞问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiabo0816 committed Jun 14, 2024
1 parent 406fa24 commit 63a3e31
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions erniebot-agent/src/erniebot_agent/tools/remote_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from copy import deepcopy
from typing import Any, Dict, List, Optional, Type

import requests
import httpx

from erniebot_agent.file import (
FileManager,
Expand Down Expand Up @@ -113,6 +113,9 @@ async def __call__(self, **tool_arguments: Dict[str, Any]) -> Any:
return await self.__post_process__(tool_response)

async def send_request(self, tool_arguments: Dict[str, Any]) -> dict:
# async http request
requests = httpx.AsyncClient(timeout=None)

url = "/".join([self.server_url.strip("/"), self.tool_view.uri.strip("/")])
url += "?version=" + self.version

Expand Down Expand Up @@ -147,13 +150,13 @@ async def send_request(self, tool_arguments: Dict[str, Any]) -> dict:
)

if self.tool_view.method == "get":
response = requests.get(url, **requests_inputs) # type: ignore
response = await requests.get(url, **requests_inputs) # type: ignore
elif self.tool_view.method == "post":
response = requests.post(url, **requests_inputs) # type: ignore
response = await requests.post(url, **requests_inputs) # type: ignore
elif self.tool_view.method == "put":
response = requests.put(url, **requests_inputs) # type: ignore
response = await requests.put(url, **requests_inputs) # type: ignore
elif self.tool_view.method == "delete":
response = requests.delete(url, **requests_inputs) # type: ignore
response = await requests.delete(url, **requests_inputs) # type: ignore
else:
raise RemoteToolError(f"method<{self.tool_view.method}> is invalid", stage="Executing")

Expand Down

0 comments on commit 63a3e31

Please sign in to comment.