RAG with ChromaDB + Llama Index + Ollama + CSV

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

*RAG with ChromaDB + Llama Index + Ollama + CSV *

curl https://ollama.ai/install.sh | sh
ollama serve

ollama run mixtral

pip install llama-index torch transformers chromadb

Section 1:

Import modules

from llama_index.llms import Ollama
from pathlib import Path
import chromadb
from llama_index import VectorStoreIndex, ServiceContext, download_loader
from llama_index.storage.storage_context import StorageContext
from llama_index.vector_stores.chroma import ChromaVectorStore

Load CSV data

SimpleCSVReader = download_loader("SimpleCSVReader")
loader = SimpleCSVReader(encoding="utf-8")
documents = loader.load_data(file=Path('./fine_food_reviews_1k.csv'))

Create Chroma DB client and store

client = chromadb.PersistentClient(path="./chroma_db_data")
chroma_collection = client.create_collection(name="reviews")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

Initialize Ollama and ServiceContext

llm = Ollama(model="mixtral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

Create VectorStoreIndex and query engine

index = VectorStoreIndex.from_documents(documents, service_context=service_context, storage_context=storage_context)
query_engine = index.as_query_engine()

Perform a query and print the response

response = query_engine.query("What are the thoughts on food quality?")
print(response)

Section 2:

Import modules

import chromadb
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.llms import Ollama
from llama_index.vector_stores.chroma import ChromaVectorStore

Create Chroma DB client and access the existing vector store

client = chromadb.PersistentClient(path="./chroma_db_data")
chroma_collection = client.get_collection(name="reviews")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

Initialize Ollama and ServiceContext

llm = Ollama(model="mixtral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

Create VectorStoreIndex and query engine with a similarity threshold of 20

index = VectorStoreIndex.from_vector_store(vector_store=vector_store, service_context=service_context, similarity_top_k=20)
query_engine = index.as_query_engine()

Perform a query and print the response

response = query_engine.query("What are the thoughts on food quality?")
print(response)

6bca48b1-fine_food_reviews

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .