langchain-ai / opengpts

MIT License
6.48k stars 861 forks source link

How can I add custom tools to my assistant? #82

Open donatienthorez opened 11 months ago

donatienthorez commented 11 months ago

Hey everyone,

TLDR; I am trying to add my own tool to an OpenGPT. How can I achieve that?

Details explanation I tried adding it to main.py in gizmo-agent:

ConfigurableAgent(
        agent=GizmoAgentType.GPT_35_TURBO,
        tools=[AvailableTools.MY_TOOL],
        system_message=DEFAULT_SYSTEM_MESSAGE,
        assistant_id=None,
    )

and declare it in tools.py:

MY_TOOL = "My Tool"
class MyTool(BaseTool):
    name = "My tool"
    description = "Useful when the user wants to write an essay"

    def _run(self, query: str) -> str:
        """Use the tool."""
        return "Once upon a time, ..."

    async def _arun(
        self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
    ) -> str:
        """Use the tool asynchronously."""
        raise NotImplementedError("My tool does not support async")

I created the assistant with adding "My tool" in the array:

POST http://127.0.0.1:8100/assistants

{
    "name": "Chatbot Name",
    "config": {
        "configurable": {
            "agent_type": "GPT 3.5 Turbo",
            "system_message": "You are a helpful assistant,
            "tools": ["My tool"]
        }
    },
    "public": true
}

This is the erorr log I get on the backend

opengpts-backend-1   | Traceback (most recent call last):
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
opengpts-backend-1   |     result = await app(  # type: ignore[func-returns-value]
opengpts-backend-1   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
opengpts-backend-1   |     return await self.app(scope, receive, send)
opengpts-backend-1   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 292, in __call__
opengpts-backend-1   |     await super().__call__(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
opengpts-backend-1   |     await self.middleware_stack(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
opengpts-backend-1   |     raise exc
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
opengpts-backend-1   |     await self.app(scope, receive, _send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
opengpts-backend-1   |     raise exc
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
opengpts-backend-1   |     await self.app(scope, receive, sender)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
opengpts-backend-1   |     raise e
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
opengpts-backend-1   |     await self.app(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
opengpts-backend-1   |     await route.handle(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
opengpts-backend-1   |     await self.app(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 69, in app
opengpts-backend-1   |     await response(scope, receive, send)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/responses.py", line 174, in __call__
opengpts-backend-1   |     await self.background()
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/background.py", line 43, in __call__
opengpts-backend-1   |     await task()
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/starlette/background.py", line 26, in __call__
opengpts-backend-1   |     await self.func(*self.args, **self.kwargs)
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/langchain/schema/runnable/base.py", line 2798, in ainvoke
opengpts-backend-1   |     return await self.bound.ainvoke(
opengpts-backend-1   |            ^^^^^^^^^^^^^^^^^^^^^^^^^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/langchain/schema/runnable/configurable.py", line 89, in ainvoke
opengpts-backend-1   |     return await self._prepare(config).ainvoke(input, config, **kwargs)
opengpts-backend-1   |                  ^^^^^^^^^^^^^^^^^^^^^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/langchain/schema/runnable/configurable.py", line 253, in _prepare
opengpts-backend-1   |     configurable_multi_options = {
opengpts-backend-1   |                                  ^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/langchain/schema/runnable/configurable.py", line 254, in <dictcomp>
opengpts-backend-1   |     k: [
opengpts-backend-1   |        ^
opengpts-backend-1   |   File "/usr/local/lib/python3.11/site-packages/langchain/schema/runnable/configurable.py", line 255, in <listcomp>
opengpts-backend-1   |     v.options[o]
opengpts-backend-1   |     ~~~~~~~~~^^^
opengpts-backend-1   | KeyError: 'My tool'

Any ideas?

ATaylorAerospace commented 11 months ago

I am sure you already checked but make sure you do not have a naming mismatch for ""My tool" in main .py and tool.py. That is the issue it appears (naming mismatch) in the error log...

CakeCrusher commented 10 months ago

@donatienthorez did you end up getting it to work