-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
59 lines (48 loc) · 1.75 KB
/
search.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
# search.py
import warnings
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*")
import psycopg2
from insert_embedding import get_embedding # Reuse the get_embedding function
from dotenv import load_dotenv
import os
load_dotenv()
# Function to search for similar documents based on a query
def search_similar_documents(query, top_k=5):
# Generate the embedding for the query
query_embedding = get_embedding(query)
# Convert the embedding to a string format suitable for PostgreSQL
query_embedding_str = '[' + ','.join(map(str, query_embedding.tolist())) + ']'
# Connect to the PostgreSQL database
conn = psycopg2.connect(
dbname=os.getenv("dbname"),
user=os.getenv("user"),
password=os.getenv("password"),
host=os.getenv("host")
)
cur = conn.cursor()
# SQL query to find the top-k most similar documents using cosine distance
cur.execute(f"""
SELECT id, text, embedding <=> '{query_embedding_str}'::vector AS distance
FROM documents
ORDER BY embedding <=> '{query_embedding_str}'::vector
LIMIT %s
""", (top_k,))
# Fetch the results
results = cur.fetchall()
# Close the cursor and connection
cur.close()
conn.close()
# Check if results were found
if len(results) == 0:
print("No similar documents found.")
else:
# Return the results if found
return results
# Example usage
if __name__ == "__main__":
query_text = "How is AI used in healthcare?"
results = search_similar_documents(query_text)
# Print the results or notify if no documents were found
if results:
for result in results:
print(f"Document ID: {result[0]}, Text: {result[1]}, Distance: {result[2]}")