Tool that takes in function or coroutine directly.
| 18 | tool_logo_md: str = "" |
| 19 | |
| 20 | class RequestTool(BaseTool): |
| 21 | """Tool that takes in function or coroutine directly.""" |
| 22 | |
| 23 | description: str = "" |
| 24 | func: Callable[[str], str] |
| 25 | afunc: Callable[[str], str] |
| 26 | coroutine: Optional[Callable[[str], Awaitable[str]]] = None |
| 27 | max_output_len = 4000 |
| 28 | tool_logo_md: str = "" |
| 29 | |
| 30 | def _run(self, tool_input: str) -> str: |
| 31 | """Use the tool.""" |
| 32 | return self.func(tool_input) |
| 33 | |
| 34 | async def _arun(self, tool_input: str) -> str: |
| 35 | """Use the tool asynchronously.""" |
| 36 | ret = await self.afunc(tool_input) |
| 37 | return ret |
| 38 | |
| 39 | def convert_prompt(self,params): |
| 40 | lines = "Your input should be a json (args json schema): {{" |
| 41 | for p in params: |
| 42 | logger.debug(p) |
| 43 | optional = not p['required'] |
| 44 | description = p.get('description', '') |
| 45 | if len(description) > 0: |
| 46 | description = "("+description+")" |
| 47 | |
| 48 | lines += '"{name}" : {type}{desc}, '.format(name=p['name'], |
| 49 | type= p['schema']['type'], |
| 50 | optional=optional, |
| 51 | desc=description) |
| 52 | |
| 53 | lines += "}}" |
| 54 | return lines |
| 55 | |
| 56 | |
| 57 | |
| 58 | def __init__(self, root_url, func_url, method, request_info, **kwargs): |
| 59 | """ Store the function, description, and tool_name in a class to store the information |
| 60 | """ |
| 61 | url = root_url + func_url |
| 62 | |
| 63 | def func(json_args): |
| 64 | if isinstance(json_args, str): |
| 65 | try: |
| 66 | json_args = json.loads(json_args) |
| 67 | except: |
| 68 | return "Your input can not be parsed as json, please use thought." |
| 69 | if "tool_input" in json_args: |
| 70 | json_args = json_args["tool_input"] |
| 71 | |
| 72 | # if it's post put patch, then we do json |
| 73 | if method.lower() in ['post', 'put', 'patch']: |
| 74 | response = getattr(requests, method.lower())(url, json=json_args) |
| 75 | else: |
| 76 | # for other methods, we use get, and use json_args as query params |
| 77 | response = requests.get(url, params=json_args) |