Skip to main content
Loading

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:

Create a LangChain application

  1. 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
  2. Run langchain app new APP_NAME to create the necessary Python files and install dependencies. In this example, the name is my-app.

    langchain app new my-app && cd ./my-app
  3. 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"
  4. 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
  5. Then replace add_routes(app, NotImplemented) with your import and path to your repo:

    add_routes(app, rag_aerospike_chain, path="/rag-aerospike")
  6. Save and close app/server.py.

  7. 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.pdf

    If 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"
  8. 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.

image