-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-openai.py
39 lines (29 loc) · 1021 Bytes
/
app-openai.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
from dotenv import find_dotenv, load_dotenv
import os
from langchain.embeddings import OpenAIEmbeddings
from numpy import dot
from numpy.linalg import norm
load_dotenv(find_dotenv())
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
embeddings_model = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
def get_embeddings():
embeddings = embeddings_model.embed_documents(
[
"Hi there! My name is Niharika. I am from Gurgaon",
"What's your name?",
"Niharika is not from Gurgaon.",
"Niharika is from India."
]
)
return embeddings
def embedding_query():
embedded_query = embeddings_model.embed_query("Niharika is from Gurgaon.")
return embedded_query
def calculate_similarity(X, Y):
cos_sim = dot(X, Y)/(norm(X)*norm(Y))
return cos_sim
embeddings = get_embeddings()
embeddingquery = embedding_query()
rows = len(embeddings)
columns = len(embeddings[0])
print("Cosine Similarity: ", calculate_similarity(embeddings, embeddingquery))