Use Preset RAG Pipeline
Instead of building everything from scratch, this guide walks you through using a preset RAG pipeline with just a few lines of code — perfect for fast prototyping or onboarding new projects.
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/ "gllm-rag"
Inserting Documents into the Data Store
Before running the RAG pipeline, you need to insert your documents into the data store. In this example, we use Elasticsearch. Make sure to create an index and insert documents with embeddings.
Create a script called insert_data.py
:
import os
import asyncio
from gllm_core.schema import Chunk
from gllm_rag.preset.initializer import build_data_store, build_em_invoker
async def main():
em_invoker = build_em_invoker(
model_id="openai/text-embedding-3-small",
credentials=os.getenv("OPENAI_API_KEY"),
)
store = build_data_store(
store_type="elasticsearch",
index_name="my_index_name",
embedding=em_invoker,
config={
"url": "https://my-elasticsearch-endpoint",
"api_key": os.getenv("ELASTICSEARCH_API_KEY")
}
)
documents = [
"Mount Bromo in Bromo Tengger Semeru National Park is famous for its sea of sand and active volcano.",
"Komodo National Park is home to the Komodo dragon and offers world-class diving spots with rich coral reefs.",
"Ujung Kulon National Park protects the endangered Javan rhinoceros and features tropical rainforests and coastal ecosystems."
]
chunks = [Chunk(content=doc) for doc in documents]
ids = await store.add_chunks(chunks)
print(len(ids))
if __name__ == "__main__":
asyncio.run(main())
Run the script:
python insert_data.py
The program outputs the number of inserted documents, e.g:
3
Running the Pipeline
Create a script called simple_rag.py
:
import asyncio
import os
from gllm_rag.preset import SimpleRAG
async def main():
rag = SimpleRAG(
language_model_id="openai/gpt-4o-mini",
language_model_credentials=os.getenv("OPENAI_API_KEY"),
embedding_model_id="openai/text-embedding-3-small",
embedding_model_credentials=os.getenv("OPENAI_API_KEY"),
data_store_type="elasticsearch",
data_store_index="my_index_name",
data_store_config={
"url": "https://my-elasticsearch-endpoint",
"api_key": os.getenv("ELASTICSEARCH_API_KEY")
}
)
await rag("Which national park in Indonesia has Komodo dragons?")
if __name__ == "__main__":
asyncio.run(main())
Run the script:
python simple_rag.py
The language model will generate a response for the given query, based on the documents retrieved from the data store, e.g:
The national park in Indonesia that has Komodo dragons is Komodo National Park.
Congratulations! You have successfully run your first RAG pipeline!
Switching Between Models and Data Stores
You can easily switch between different language models, embedding models, or data store by updating the corresponding configuration values in your pipeline.
See all available language and embedding models in Supported Models page.
See all supported vector data stores and their setup details in Supported Vector Data Store page.
Last updated