RAG with Ollama + Mistral + Llama Index

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

RAG with Ollama + Mistral + Llama Index

pip install llama-index qdrant_client torch transformers

Import modules

from llama_index.llms import Ollama
from pathlib import Path
import qdrant_client
from llama_index import VectorStoreIndex, ServiceContext, download_loader
from llama_index.storage.storage_context import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore

Load JSON data

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

Create Qdrant client and store

client = qdrant_client.QdrantClient(path="./qdrant_data")
vector_store = QdrantVectorStore(client=client, collection_name="tweets")
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 qdrant_client
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.llms import Ollama
from llama_index.vector_stores.qdrant import QdrantVectorStore

Create Qdrant client and vector store

client = qdrant_client.QdrantClient(path="./qdrant_data")
vector_store = QdrantVectorStore(client=client, collection_name="tweets")

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)
query_engine = index.as_query_engine(similarity_top_k=20)

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