-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask.py
More file actions
20 lines (15 loc) · 661 Bytes
/
Copy pathask.py
File metadata and controls
20 lines (15 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from openai import OpenAI
import numpy as np, json, os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
MODEL = "text-embedding-3-small"
faqs = json.load(open("faqs_with_embeddings.json"))
matrix = np.load("faq_matrix.npy")
while True:
query = input("\n❓ Ask me something (or type 'exit'): ")
if query.lower() == "exit":
break
q_vec = np.array(client.embeddings.create(model=MODEL, input=query).data[0].embedding)
sims = matrix @ q_vec / (np.linalg.norm(matrix, axis=1) * np.linalg.norm(q_vec))
best_i = int(np.argmax(sims))
best = faqs[best_i]
print(f"\n📄 {best['answer']} (similarity: {sims[best_i]:.2f})")