LivePredict__RAGBot / index_pinecone.py
AbhishekShrimali's picture
Upload 11 files
9f481e2 verified
raw
history blame contribute delete
1.37 kB
from pinecone import Pinecone
from sentence_transformers import SentenceTransformer
import uuid
# βœ… Load a 1024-dim model (must match your Pinecone index)
model = SentenceTransformer("sentence-transformers/all-roberta-large-v1")
# βœ… Initialize Pinecone
pc = Pinecone(api_key="pcsk_5CWCFt_9nPiRVa65nG4rs6ZC2cqnEthBSvcEhuQa53TK7FN6rwYZW1qQpacqoAvGUjNCFU")
index = pc.Index("aped-4627-b74a") # Use your actual index name
# βœ… Example data
texts = [
("This is a drama example", "drama"),
("A high-action thriller scene", "action"),
("Romantic movie with emotional plot", "drama"),
("Explosions and car chases", "action")
]
# βœ… Create embeddings and structure for upsert
vectors = []
for i, (text, genre) in enumerate(texts):
embedding = model.encode(text).tolist()
vectors.append({
"id": f"vec_{i}_{uuid.uuid4().hex[:8]}",
"values": embedding,
"metadata": {"genre": genre}
})
# βœ… Upsert into a namespace
index.upsert(
vectors=vectors,
namespace="ns1"
)
# βœ… Query example with metadata filter
query_vector = model.encode("Intense action movie").tolist()
response = index.query(
namespace="ns1",
vector=query_vector,
top_k=2,
include_values=True,
include_metadata=True,
filter={"genre": {"$eq": "action"}}
)
print(response)