Deploy A Langchain Agent As An API Endpoint in 10 Minutes
Deploy A Langchain Agent As An API Endpoint in 10 Minutes
Get unlimited access to the best of Medium for less than $1/week. Become a member
So you coded a cool Langchain agent in Python. Congrats! In this tutorial, we will
walk you through the process of making it an OpenAPI endpoint, which can be
deployed and called as an API, allowing you to seamlessly integrate it into your
product or workflows. All that in only 10 minutes. Let’s get started!
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 1/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 2/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
Create an .env file and store your OpenAI API key in it. For extra security, you can
create a new OpenAI key for this project.
OPENAI_API_KEY=your-api-key
# main.py
Now, let’s create a basic chatbot using the OpenAI API. As you can imagine, the
process would be roughly the same for more complex agents.
# main.py
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 3/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
load_dotenv()
chat = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0.3)
messages = [
SystemMessage(content="You are a funny and helpful assistant."),
HumanMessage(content="Explain to me quantum physics."),
]
response=chat(messages)
print(response.content,end='\n')
Try it locally :
python main.py
Now, let’s package this logic into a function that we can use in our agent :
# main.py
# import schema for chat messages and ChatOpenAI in order to query chatmodels G
chat = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0.3)
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 4/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
def my_agent(input):
messages = [
SystemMessage(content="You are a funny and helpful assistant."),
HumanMessage(content=input),
]
Open in app
response=chat(messages)
returnSearch
response.content
To do so, we are going to package our langchain agent into a phospho agent.
phospho is an agnostic Plateform as a Service to deploy and manage autonomous
agents. They also provide an SDK to help developers go from prototype to
production with their agents.
Here, we only want to create a basic conversational agent. So we only need to create
a chat route.
# main.py
def my_agent(input):
messages = [
SystemMessage(content="You are a funny and helpful assistant."),
HumanMessage(content=input),
]
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 5/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
response=chat(messages)
return response.content
agent = Agent()
@agent.chat()
def my_chat(message):
# Use our agent logic to generate a response
response = my_agent(message.content)
# Return the response in a Message object
return Message(response)
That’s it!
With this code, we have created a new agent with a single route /chat that will call
our my_agent function and send the response back to the user. Note that you need to
send an object of type Message as a response (view the documentation for more
info).
# Ignore virtualenv
venv/
# Ignore .env
.env
# Ignore pycache
__pycache__/
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 6/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
if the name is already taken, choose a new one. Come on, be creative!
Select the github repository you want to link to your phospho project.
On the phospho webapp, go to your project page and click on the Manage Env
Variables tab. Then, click on the Environment variables tab and add a new variable
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 7/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
At the next restart of your agent, it will be able to use the OpenAI API (to force a
restart, you can trigger a new deployment).
After a one or two minutes, your agent should be deployed and ready to use. You
have the endpoint URL on your project page.
Unless you go viral, you should be able to use the free plan (no credit card required
to start).
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 8/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
Follow
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 9/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
X19
Pierre-Louis Biojout
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 10/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 11/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
217 3
Lists
New_Reading_List
174 stories · 170 saves
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 12/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
Diptiman Raichaudhuri
54
Dion
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 13/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
31 2
Guangya Liu
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 14/15
02/11/2023, 04:49 Deploy a Langchain agent as an API endpoint in 10 minutes | by Pierre-Louis Biojout | Sep, 2023 | Medium
Kelvin Lu
https://medium.com/@plbiojout/deploy-a-langchain-agent-as-an-api-endpoint-in-10-minutes-266959613a2c 15/15