Create R.A.G with germini
Step 1: Create txt file with name facts.txt
and content below:
facts.txt
and content below:Severe weather can trigger a mass migration known as a “grand passage.”
In 1995, a blizzard in the Prairie Pothole Region spurred a migration of millions of ducks
and geese that jammed radar systems and grounded flights in Nebraska and Missouri.
The fastest duck recorded was a red-breasted merganser, which achieved an airspeed of 100 mph.
According to research, ducks may show a preference for colors in the green or blue spectrum.
A duck’s bill varies by species and relates to its function.
Flat bills are used to consume plant materials,
while pointed bills are used to catch and eat fish.
City ducks have a different accent than country ducks – typically louder!
Mallards often have clutches that include eggs fertilized by different males,
which biologists believe ensures successful fertilization and greater genetic variation.
All ducks can dive, but some species are better at diving than others.
The best is the long-tailed duck, also known as an oldsquaw, which can dive 240 feet.
Ducklings can understand the relationship between objects,
which demonstrates abstract thought capabilities.
Ducks, like other waterfowl, have as many as 12,000 separate muscles to control their feathers.
They can use these to lift or compress feathers to dive underwater, show emotion, or regulate body heat.
Ducks migrate at an altitude of 200 to 4,000 feet but are capable of reaching heights of 21,000 feet.
Step 2: Using the code be low to build a simple RAG with doc and germini
from llama_index.core import Settings, SimpleDirectoryReader,StorageContext,VectorStoreIndex
from llama_index.core.node_parser import TokenTextSplitter
from llama_index.vector_stores.duckdb import DuckDBVectorStore
from langchain_google_genai import ChatGoogleGenerativeAI
import os
import getpass
# models
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ")
Settings.llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
# load documents into a vector store(DuckDB)
documents = SimpleDirectoryReader(input_files=["facts.txt"]).load_data(show_progress=True)
splitter = TokenTextSplitter(separator="\n",chunk_size=64,chunk_overlap=0)
vector_store = DuckDBVectorStore()
store_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex(splitter.get_nodes_from_documents(documents),
store_context=store_context,
show_progress=True)
query_engine = index.as_query_engine()
try:
while True:
query = input(">>")
respone = query_engine.query(query)
print(respone)
except KeyboardInterrupt:
exit()
Last updated