Create a RAG application using LangChain
This guide describes how to create an example Retrieval Augmented Generation (RAG) application using LangChain templates.
Prerequisites
You need the following components to set up this application:
- OpenAI API key (requires a paid account)
- Python 3.9 or later
- pip 9.0.1 or later
- A running AVS deployment (see Install AVS)
Create a LangChain application
Install the LangChain CLI, which gives you access to a set of templates for building applications, with the following command.
pip install -U langchain-cli
Run
langchain app new APP_NAME
to create the necessary Python files and install dependencies. In this example, the name ismy-app
.langchain app new my-app && cd ./my-app
While still in your
my-app
application folder, run the following command to apply the Aerospike RAG application template:langchain app add --repo="aerospike/rag-aerospike/" --branch="main"
Edit
app/server.py
in two places to set the route for the RAG application. First, add the following to your import statements:from rag_aerospike import chain as rag_aerospike_chain
Then replace
add_routes(app, NotImplemented)
with your import and path to your repo:add_routes(app, rag_aerospike_chain, path="/rag-aerospike")
Save and close
app/server.py
.Run the following commands to export your OpenAI API key and any other relevant environment variables:
export OPENAI_API_KEY=<your-OpenAI-api-key>
export AVS_HOST=<your-AVS-host> #default localhost
export AVS_PORT=<your-AVS-port> #default 5000
export AVS_NAMESPACE=<your-AVS-namespace> #default test
export DATASOURCE=<your-datasource-url> #default https://aerospike.com/files/ebooks/aerospike-up-and-running-early-release3.pdfIf you have a paid LangSmith account, you can set your LangChain credentials as environment variables to enable application monitoring. These are not required for this example application.
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"Serve your LangChain application with the following command:
langchain serve
It takes some time to tokenize and embed your datasource. When this is finished, you should see the following log line:
INFO: Application startup complete.
Test your application
When your application is serving, navigate to http://localhost:8000/rag-aerospike/playground/ and ask a context-relevant question.