Retrieval Augmented Generation
Give additional context to the LLM by providing trusted content
Inserts a information retrieval step to find relevant information from a knowledge store
Retrieved information is then provided to the model’s context for LLM response
Can help with hallucinations
Not always predictable which queries will retrieve which knowledge stores
…
But can you add structure to it?
from llama_index.core import StorageContext, load_index_from_storage
# Load the knowledge store (index) from disk
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
def retrieve_trusted_content(query):
retriever = index.as_retriever(similarity_top_k=5)
nodes = retriever.retrieve(query)
return [f"<excerpt>{x.text}</excerpt>" for x in nodes]…
Create a function
from chatlas import ChatOpenAI
from llama_index.core import StorageContext, load_index_from_storage
# Load the knowledge store (index) from disk
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
def retrieve_trusted_content(query: str, top_k: int = 5):
"""
Retrieve relevant content from the knowledge store.
Parameters
----------
query
The query used to semantically search the knowledge store.
top_k
The number of results to retrieve from the knowledge store.
"""
retriever = index.as_retriever(similarity_top_k=top_k)
nodes = retriever.retrieve(query)
return [f"<excerpt>{x.text}</excerpt>" for x in nodes]https://github.com/chendaniely/nydsaic2025-llm/tree/main/code/06-rag
The New York Data Science & AI Conference. 2025. https://github.com/chendaniely/nydsaic2025-llm