-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
70 lines (57 loc) · 2.65 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import psycopg2
from embedding_util import generate_embeddings
def run():
# Establish a connection to the PostgreSQL database
conn = psycopg2.connect(
user="myuser",
password="mypassword",
host="localhost",
port=5555, # The port you exposed in docker-compose.yml
database="mydb"
)
# Create a cursor to execute SQL commands
cur = conn.cursor()
try:
sentences = [
"A group of vibrant parrots chatter loudly, sharing stories of their tropical adventures.",
"The mathematician found solace in numbers, deciphering the hidden patterns of the universe.",
"The robot, with its intricate circuitry and precise movements, assembles the devices swiftly.",
"The chef, with a sprinkle of spices and a dash of love, creates culinary masterpieces.",
"The ancient tree, with its gnarled branches and deep roots, whispers secrets of the past.",
"The detective, with keen observation and logical reasoning, unravels the intricate web of clues.",
"The sunset paints the sky with shades of orange, pink, and purple, reflecting on the calm sea.",
"In the dense forest, the howl of a lone wolf echoes, blending with the symphony of the night.",
"The dancer, with graceful moves and expressive gestures, tells a story without uttering a word.",
"In the quantum realm, particles flicker in and out of existence, dancing to the tunes of probability.",
]
# Insert sentences into the items table
for sentence in sentences:
embedding = generate_embeddings(sentence)
cur.execute(
"INSERT INTO items (content, embedding) VALUES (%s, %s)",
(sentence, embedding)
)
# Example query
query = "Give me some content about the ocean"
query_embedding = generate_embeddings(query)
# Perform a cosine similarity search
cur.execute(
"""SELECT id, content, 1 - (embedding <=> %s) AS cosine_similarity
FROM items
ORDER BY cosine_similarity DESC LIMIT 5""",
(query_embedding,)
)
# Fetch and print the result
print("Query:", query)
print("Most similar sentences:")
for row in cur.fetchall():
print(
f"ID: {row[0]}, CONTENT: {row[1]}, Cosine Similarity: {row[2]}")
except Exception as e:
print("Error executing query", str(e))
finally:
# Close communication with the PostgreSQL database server
cur.close()
conn.close()
if __name__ == "__main__":
run()