File size: 1,370 Bytes
9f481e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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)
|