-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (33 loc) · 1.09 KB
/
utils.py
File metadata and controls
43 lines (33 loc) · 1.09 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
import os
import numpy as np
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
def get_embedding(text, model='text-embedding-3-small'):
client = OpenAI(api_key=OPENAI_API_KEY)
response = client.embeddings.create(
input=text,
model=model
)
return response.data[0].embedding
def get_embeddings(text, model='text-embedding-3-small'):
client = OpenAI(api_key=OPENAI_API_KEY)
response = client.embeddings.create(
input=text,
model=model
)
output = []
for i in range(len(response.data)):
output.append(response.data[i].embedding)
return output
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def call_openai(prompt, temperature=0.0, model='gpt-3.5-turbo-0125'):
client = OpenAI(api_key=OPENAI_API_KEY)
completion = client.chat.completions.create(
model=model,
messages=[{'role': 'user', 'content': prompt}],
temperature=temperature
)
return completion.choices[0].message.content