flag-checkeredYour First RAG Pipeline

This guide will walk you through setting up a basic RAG pipeline.

chevron-rightPrerequisiteshashtag

This example specifically requires you to complete all setup steps listed on the Prerequisites and Index Your Data with Vector Data Store page.

You should be familiar with these concepts and components:

githubView full project code on GitHub

Installation

# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" python-dotenv gllm-core gllm-generation gllm-inference gllm-pipeline gllm-retrieval gllm-datastore

How to Use this Guide

You can either:

  1. Download or copy the complete guide file(s) to get everything ready instantly by heading to 📂 Complete Guide Files section in the end of this page. You can refer to the guide whenever you need explanation or want to clarify how each part works.

  2. Follow along with each step to recreate the files yourself while learning about the components and how to integrate them.

Both options will work—choose based on whether you prefer speed or learning by doing!

Project Setup

1

Folder Structure

Start by organizing your files (if you have downloaded the Complete Guide Files, you can proceed to the next step). This is the minimal folder structure you can follow, yet you may adjust to your need.

circle-info

Don’t worry about creating all these files yourself — we’ll provide the content for each file throughout the tutorial.

2

Prepare your .env file:

Ensure you have a file named .env in your project directory with the following content:

EMBEDDING_MODEL="text-embedding-3-small"
LANGUAGE_MODEL="openai/gpt-5-nano"
OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>"
circle-info

This is an example .env file. You may adjust the variables according to your need.


1) Index Your Data

1

Download the database

For this guide, we provide a preset SQLite database that is loaded with chunks from imaginary_animals.csv . You can download them here.

2

Arrange the files

Arrange them in your project. You can follow the structure in Project Setup section.

You may use another knowledge base file and adjust accordingly. For more information about how to index data to data store, you can visit this page Index Your Data with Vector Data Store.

2) Build Core Components of Your Pipeline

Create the Retriever

The Retriever finds and pulls useful information from your ChromaDB database. Create modules/retriever.py:

Key Components Explained:

  1. Environment Loading: Load settings from your .env file

  2. Embedding Model: OpenAIEMInvoker converts text into vector embeddings for similarity search

  3. Data Store: ChromaVectorDataStore connects to your local ChromaDB with persistent storage

  4. Retriever: BasicVectorRetriever performs vector similarity search to find relevant documents

circle-info

The embedding model used here must match the one used when indexing the data for proper retrieval functionality..

Create the Response Synthesizer

The response synthesizerarrow-up-right generates the final answer using the retrieved context and user query.

Create modules/response_synthesizer.py:

Key Components:

  1. System Prompt: Instructs the model to use only provided context and handle insufficient information gracefully

  2. User Prompt: Templates the user's question for the model

  3. LM Request Processor: Built using the build_lm_request_processor() helper function for simplified setup

  4. Response Synthesizer: ResponseSynthesizer.static_list combines all given chunks into a single prompt for response generation

3) Build the Pipeline

We'll build the full process in your pipeline.py file using GL SDK's pipelinearrow-up-right. Open the file and follow these instructions to create steps and compose them:

circle-info

Note that in this guide, the states used as input state and output state are only user_query, chunks, context, state_variables, and response. The full state structure is defined in Default State: RAGState. For each step, you can pass state variables as parameters to their corresponding components. For example,Vector Retriever accepts query as parameter, thus you can pass the "query" state and takes value from another state (i.e. "user_query") to be mapped. You may also notice that some parameters are passed through configs (runtime_config_map). These are parameters passed to a component but not forwarded to other steps.

1

Import the helpers and components

2

Create the Retriever Step

This component step searches for relevant chunks based on the user's query.

Here, the query input takes its value from the user input (user_query). We also configure top_k to control how many results are retrieved.

3

Create the Response Synthesizer Step

Key Components:

  1. System Prompt: Instructs the model to use only provided context and handle insufficient information gracefully

  2. User Prompt: Templates the user's question for the model

  3. LM Request Processor: Built using the build_lm_request_processor() helper function for simplified setup

  4. Response Synthesizer: ResponseSynthesizer.stuff() combines all context into a single prompt for response generation.

Note:

  1. "chunks": "chunks" is the primary data flow in the state.

  2. "kwargs": "chunks" is for validation compatibility since kwargs should not be empty.

4

Connect Everything into a Pipeline

Finally, use the pipe operator (|) to chain all steps in order:

4) Run the Pipeline

circle-info

When running the pipeline, you may encounter an error like this:

[2025-08-26T14:36:10+0700.550 chromadb.telemetry.product.posthog ERROR] Failed to send telemetry event CollectionQueryEvent: capture() takes 1 positional argument but 3 were given

Don't worry about this, since we do not use this Chroma feature. Your Pipeline should still work.

1

Configure and invoke the pipeline

Configure the state and config for direct pipeline invocation:

2

Run pipeline.py file

3

Observe output

If you successfully run all the steps, you will see something like this:

Congratulations! You've successfully built your first RAG pipeline.

Last updated