forked from wombyz/HormoziGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
49 lines (43 loc) · 1.55 KB
/
utils.py
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
import os
import openai
import requests
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
api_key_pinecone = os.getenv("PINECONE_API_KEY")
pinecone_environment = os.getenv("PINECONE_ENVIRONMENT")
pinecone_endpoint = os.getenv("PINECONE_ENDPOINT")
# Get embeddings for a given string
def get_embeddings_openai(text):
response = openai.Embedding.create(
input=text,
model="text-embedding-ada-002"
)
response = response['data']
# extract embeddings from responses0
return [x["embedding"] for x in response]
# Search Pinecone for similar documents
def semantic_search(query, **kwargs):
# Embed the query into a vector
xq = get_embeddings_openai(query)
# Call Pinecone's REST API
url = pinecone_endpoint
headers = {
"Api-Key": api_key_pinecone,
"Content-Type": "application/json"
}
body = {
"vector": xq[0],
"topK": str(kwargs["top_k"]) if "top_k" in kwargs else "1",
"includeMetadata": "false" if "include_metadata" in kwargs and not kwargs["include_metadata"] else True
}
try:
res = requests.post(url, json=body, headers=headers)
res.raise_for_status() # Raise an exception if the HTTP request returns an error
res = res.json()
titles = [r["metadata"]["title"] for r in res["matches"]]
transcripts = [r["metadata"]["transcript"] for r in res["matches"]]
return list(zip(titles, transcripts))
except Exception as e:
print(f"Error in semantic search: {e}")
raise