-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinecone_query.py
More file actions
61 lines (45 loc) · 1.65 KB
/
Copy pathpinecone_query.py
File metadata and controls
61 lines (45 loc) · 1.65 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""
Pinecone query. Stratos Memory Stack, layer 4.
Usage:
python3 pinecone_query.py "your question" --top-k 3
python3 pinecone_query.py "what does the playbook say about pricing"
Requires:
pip install pinecone-client openai python-dotenv
.env file with PINECONE_API_KEY and OPENAI_API_KEY
Output:
Top-k matching chunks with source path and similarity score.
"""
import os
import sys
import argparse
from dotenv import load_dotenv
from pinecone import Pinecone
from openai import OpenAI
load_dotenv()
INDEX_NAME = "your-vault"
EMBEDDING_MODEL = "text-embedding-3-small"
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
index = pc.Index(INDEX_NAME)
def embed(text: str):
response = openai_client.embeddings.create(model=EMBEDDING_MODEL, input=text)
return response.data[0].embedding
def main():
parser = argparse.ArgumentParser()
parser.add_argument("query", help="Question or topic to search")
parser.add_argument("--top-k", type=int, default=3, help="Number of matches to return")
args = parser.parse_args()
vector = embed(args.query)
results = index.query(vector=vector, top_k=args.top_k, include_metadata=True)
print(f"\nQuery: {args.query}\n")
print(f"Top {args.top_k} matches:\n")
for i, match in enumerate(results["matches"], 1):
meta = match["metadata"]
print(f"--- Match {i} (score: {match['score']:.3f}) ---")
print(f"Source: {meta['source']}")
print(f"Chunk: {meta['chunk_index']}")
print(f"Preview: {meta['text']}")
print()
if __name__ == "__main__":
main()