-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
76 lines (65 loc) · 2.81 KB
/
api.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
71
72
73
74
75
76
import os
from flask import Flask, jsonify, request
from database import get_database_connection
app = Flask(__name__)
@app.route('/entries', methods=['GET'])
def get_all_entries():
with get_database_connection() as conn:
with conn.cursor() as cursor:
sentence = request.args.get('sentence')
print(f"sentence = {sentence}")
cursor.execute("""
SELECT entry_id, title, last_modified_date, NULL AS similarity FROM entry ORDER BY last_modified_date DESC LIMIT 10
""" if sentence is None else """
WITH request AS (SELECT pgml.embed('intfloat/multilingual-e5-large', %s)::vector(1024) AS embedding)
SELECT entry_id, title, last_modified_date, 1 - (embedding <=> (SELECT embedding FROM request)) AS similarity
FROM entry
ORDER BY similarity DESC
LIMIT 10
""", (sentence,))
results = cursor.fetchall()
entries = [
{
"entry_id": row[0],
"title": row[1],
"last_modified_date": row[2].isoformat(),
"similarity": row[3]
}
for row in results]
return jsonify(entries)
@app.route('/entries/<int:entry_id>', methods=['GET'])
def get_single_entry(entry_id):
with get_database_connection() as conn:
with conn.cursor() as cursor:
cursor.execute("""
SELECT entry_id, title, content, last_modified_date FROM entry WHERE entry_id = %s
""", (entry_id,))
row = cursor.fetchone()
if not row:
return jsonify({"error": "Entry not found"}), 404
entry = {
"entry_id": row[0],
"title": row[1],
"content": row[2],
"last_modified_date": row[3].isoformat()
}
return jsonify(entry)
@app.route('/entries/<int:entry_id>/similarities', methods=['GET'])
def get_entry_similarities(entry_id):
with get_database_connection() as conn:
with conn.cursor() as cursor:
cursor.execute("""
SELECT entry_id, title, last_modified_date, 1 - (embedding <=> (SELECT embedding FROM entry WHERE entry_id = %s)) AS similarity FROM entry WHERE entry_id <> %s ORDER BY similarity DESC LIMIT 5
""", (entry_id, entry_id))
results = cursor.fetchall()
entries = [
{
"entry_id": row[0],
"title": row[1],
"last_modified_date": row[2].isoformat(),
"similarity": row[3]
}
for row in results]
return jsonify(entries)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get("PORT", 4000)))