Create a RAG application using LangChain
Overviewโ
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)
Install the LangChain CLIโ
Run the following to install the LangChain CLI, which gives you access to a set of templates for building applications:
pip install -U langchain-cli
Generate an applicationโ
Run the following to create the necessary Python files and install dependencies:
langchain app new my-app
Apply the Aerospike RAG templateโ
Run the following in your application folder (e.g., my-app
) to apply the Aerospike RAG application template:
langchain app add --repo="aerospike/rag-aerospike/" --branch="main"
Add route details for your applicationโ
Make the following changes to app/server.py
to set the route for the RAG application:
Add the following to your import:
from rag_aerospike import chain as rag_aerospike_chain
Replace
add_routes(app, NotImplemented)
with the following:add_routes(app, rag_aerospike_chain, path="/rag-aerospike")
Set required environment variablesโ
Run the following 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.pdf
Set up LangSmith (optional)โ
Set your LangChain credentials if you want to enable full application monitoring โ this requires a paid account on langchain.com.
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
Serve the applicationโ
Run the following to start serving the application:
langchain serve
It will take some time to tokenize and embed your datasource. When 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.