RAG with ChromaDB + Llama Index + Ollama

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

*RAG with ChromaDB + Llama Index + Ollama
*

pip install llama-index torch transformers chromadb

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 JSON data

JSONReader = download_loader("JSONReader")
loader = JSONReader()
documents = loader.load_data(Path('tinytweets.json'))

Create Chroma DB client and store

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

Initialize Ollama and ServiceContext

llm = Ollama(model="mistral")
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 does the author think about Star Trek? In One line")
print(response)

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="tweets")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

Initialize Ollama and ServiceContext

llm = Ollama(model="mistral")
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 does the author think about Star Trek? In One line")
print(response)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player