How to expose as remote tool which can be called by remote Agent #77

Closed
opened 2026-02-16 00:18:39 -05:00 by yindo · 1 comment
Owner

Originally created by @weiminw on GitHub (Dec 23, 2023).

I use langserve develop a chain , and expose as remote tool. my friend wants to call my chain in his agent, how to do it?

Joke chain:

#!/usr/bin/env python
from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langserve import add_routes
llm = ChatOpenAI(
    openai_api_base=f"http://192.168.1.201:18001/v1",
    openai_api_key="EMPTY",
    model="gpt-3.5-turbo",
    temperature=0.5,
    top_p="0.3",
    default_headers={"x-heliumos-appId": "general-inference"},
    tiktoken_model_name="gpt-3.5-turbo",
    verbose=True,
)

app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="A simple api server using Langchain's Runnable interfaces",
)


prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
add_routes(
    app,
    prompt | llm,
    path="/joke",
)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)

Agent:

from langchain.agents import initialize_agent, AgentType

from langchain_community.chat_models import ChatOpenAI
from langserve import RemoteRunnable

from langchain.tools import Tool

llm = ChatOpenAI(
    openai_api_base=f"http://xxxx:xxx/v1",
    openai_api_key="EMPTY",
    model="gpt-3.5-turbo",
    temperature=0.5,
    top_p="0.3",
    tiktoken_model_name="gpt-3.5-turbo",
    verbose=True,
)

remote_tool = RemoteRunnable("http://xxx:xxx/joke/")
tools = [
    Tool.from_function(
        func=remote_tool.invoke,
        name="joke",
        description="用户要求讲笑话的时候使用该工具",
        # coroutine= ... <- you can specify an async method if desired as well
    ),
]
agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)

result = agent.run("讲一个关于坐出租车的笑话")
print(result)

agent always got error: because not there is not valid input to remote tool.

Originally created by @weiminw on GitHub (Dec 23, 2023). I use langserve develop a chain , and expose as remote tool. my friend wants to call my chain in his agent, how to do it? **Joke chain:** ``` #!/usr/bin/env python from fastapi import FastAPI from langchain.prompts import ChatPromptTemplate from langchain.chat_models import ChatAnthropic, ChatOpenAI from langserve import add_routes llm = ChatOpenAI( openai_api_base=f"http://192.168.1.201:18001/v1", openai_api_key="EMPTY", model="gpt-3.5-turbo", temperature=0.5, top_p="0.3", default_headers={"x-heliumos-appId": "general-inference"}, tiktoken_model_name="gpt-3.5-turbo", verbose=True, ) app = FastAPI( title="LangChain Server", version="1.0", description="A simple api server using Langchain's Runnable interfaces", ) prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}") add_routes( app, prompt | llm, path="/joke", ) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) ``` **Agent:** ``` from langchain.agents import initialize_agent, AgentType from langchain_community.chat_models import ChatOpenAI from langserve import RemoteRunnable from langchain.tools import Tool llm = ChatOpenAI( openai_api_base=f"http://xxxx:xxx/v1", openai_api_key="EMPTY", model="gpt-3.5-turbo", temperature=0.5, top_p="0.3", tiktoken_model_name="gpt-3.5-turbo", verbose=True, ) remote_tool = RemoteRunnable("http://xxx:xxx/joke/") tools = [ Tool.from_function( func=remote_tool.invoke, name="joke", description="用户要求讲笑话的时候使用该工具", # coroutine= ... <- you can specify an async method if desired as well ), ] agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) result = agent.run("讲一个关于坐出租车的笑话") print(result) ``` agent always got error: because not there is not valid input to remote tool.
yindo closed this issue 2026-02-16 00:18:40 -05:00
Author
Owner

@eyurtsev commented on GitHub (Dec 23, 2023):

The best way right now is to wrap it in another function. This allows you to populate the doc-string and specify the type signature appropriately.

from langchain.tools import tool

@tool
def tell_joke(topic: str) -> str:
   """A tool that can be used to tell a joke."""
   return remote_tool.invoke({'topic': topic})

tools = [
  tell_joke
] 
...
@eyurtsev commented on GitHub (Dec 23, 2023): The best way right now is to wrap it in another function. This allows you to populate the doc-string and specify the type signature appropriately. ```python from langchain.tools import tool @tool def tell_joke(topic: str) -> str: """A tool that can be used to tell a joke.""" return remote_tool.invoke({'topic': topic}) tools = [ tell_joke ] ... ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#77