This series dives into how to use LangChain, based on the LangChain Quickstart guide.
In this post, we’ll explore how to deploy LangChain agents as a REST API using LangServe.
In our last post, we created an agent that combines tools for answering LangChain-related queries with internet search capabilities.
Here’s a recap of the code we used:
import os
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.tools.retriever import create_retriever_tool
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Set up the API key as environment variable
with open(".openai") as f:
os.environ["OPENAI_API_KEY"] = f.read().strip()
# Load web page content
loader = WebBaseLoader("https://python.langchain.com/docs/get_started/introduction")
docs = loader.load()
# Load embeddings
embeddings = OpenAIEmbeddings()
# Split the documents
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
# Vectorize documents and create a vector store
vector = FAISS.from_documents(documents, embeddings)
# Create a retriever
retriever = vector.as_retriever()
# Search tool for document retrieval
retriever_tool = create_retriever_tool(
retriever,
"langchain_search",
"A search tool for LangChain-related queries. Use this for questions about LangChain!",
)
# Set Tavily API key
with open(".tavily") as f:
os.environ["TAVILY_API_KEY"] = f.read().strip()
# Internet search tool
search = TavilySearchResults()
# List of tools in use
tools = [retriever_tool, search]
# Get template from LangChain Hub
template = hub.pull("hwchase17/openai-functions-agent")
# Create an agent
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent = create_openai_functions_agent(llm, tools, template)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
First, install LangServe with the following command: