Skip to main content
Learn more about Chains

Prerequisites

You need uv installed and a Baseten account with an API key. If you want to run this example in local debugging mode, you’ll also need to install chromadb:
The complete code used in this tutorial can also be found in the Chains examples repo.

Overview

Retrieval-augmented generation (RAG) is a multi-model pipeline for generating context-aware answers from LLMs. There are a number of ways to build a RAG system. This tutorial shows a minimum viable implementation with a basic vector store and retrieval function. It’s intended as a starting point to show how Chains helps you flexibly combine model inference and business logic. In this tutorial, we’ll build a simple RAG pipeline for a hypothetical alumni matching service for a university. The system:
  1. Takes a bio with information about a new graduate
  2. Uses a vector database to retrieve semantically similar bios of other alums
  3. Uses an LLM to explain why the new graduate should meet the selected alums
  4. Returns the writeup from the LLM

Build the Chain

Create a file rag.py in a new directory with:
Our RAG Chain is composed of three parts:
  • VectorStore, a Chainlet that implements a vector database with a retrieval function.
  • LLMClient, a Stub for connecting to a deployed LLM.
  • RAG, the entrypoint Chainlet that orchestrates the RAG pipeline and has VectorStore and LLMClient as dependencies.
We’ll examine these components one by one and then see how they all work together.

Vector store Chainlet

A real production RAG system would use a hosted vector database with a massive number of stored embeddings. For this example, we’re using a small local vector store built with chromadb to stand in for a more complex system. The Chainlet has three parts:
  • remote_config, which configures a Docker image on deployment with dependencies.
  • __init__(), which runs once when the Chainlet is spun up, and creates the vector database with ten sample bios.
  • run_remote(), which runs each time the Chainlet is called and is the sole public interface for the Chainlet.
rag/rag.py

LLM inference stub

Now that we can retrieve relevant bios from the vector database, we need to pass that information to an LLM to generate our final output. Chains can integrate previously deployed models using a Stub. Like Chainlets, Stubs implement run_remote(), but as a call to the deployed model. For our LLM, we’ll use Phi-3 Mini Instruct, a small-but-mighty open source LLM.

Deploy Phi-3 Mini Instruct 4k

One-click model deployment from Baseten’s model library.
While the model is deploying, be sure to note down the models’ invocation URL from the model dashboard for use in the next step. To use our deployed LLM in the RAG Chain, we define a Stub:
rag/rag.py

RAG entrypoint Chainlet

The entrypoint to a Chain is the Chainlet that specifies the public-facing input and output of the Chain and orchestrates calls to dependencies. The __init__ function in this Chainlet takes two new arguments:
  • Add dependencies to any Chainlet with chains.depends(). Only Chainlets, not Stubs, need to be added in this fashion.
  • Use chains.depends_context() to inject a context object at runtime. This context object is required to initialize the LLMClient stub.
  • Visit your baseten workspace to find your the URL of the previously deployed Phi-3 model and insert if as value for LLM_URL.
rag/rag.py

Test locally

Because our Chain uses a Stub for the LLM call, we can run the whole Chain locally without any GPU resources. Before running the Chainlet, make sure to set your Baseten API key as an environment variable BASETEN_API_KEY.
rag/rag.py
We can run our Chain locally:
After a few moments, we should get a recommendation for why Sam should meet the alumni selected from the database.

Deploy to production

Once we’re satisfied with our Chain’s local behavior, we can deploy it to Baseten. To deploy the Chain, run:
This deploys the Chain as a published deployment. Once it’s running, call it from its API endpoint. You can do this in the console with cURL:
Alternatively, you can also integrate this in a Python application:
call_chain.py
The published deployment has access to full autoscaling settings and will scale to zero when not in use. To iterate on the Chain during development, use truss chains push --watch rag.py to create a development deployment with live code patching.