Skip to content

Commit 3d45190

Browse files
committed
Add semantic search
1 parent bee166c commit 3d45190

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

budgetkey_api/modules/search.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import elasticsearch
4+
from openai import OpenAI
45

56
from apies import apies_blueprint
67
from apies.query import Query
@@ -64,7 +65,35 @@ def text_rules(field):
6465

6566

6667
class BudgetkeyQuery(Query):
67-
...
68+
69+
def __init__(self, search_indexes):
70+
super().__init__(search_indexes)
71+
api_key = os.environ.get('OPENAI_API_KEY')
72+
if api_key:
73+
self.openai_client = OpenAI(api_key=api_key)
74+
else:
75+
self.openai_client = None
76+
77+
def apply_term(self, term, text_fields, multi_match_type='most_fields', multi_match_operator='and'):
78+
super().apply_term(term, text_fields, multi_match_type, multi_match_operator)
79+
if term and len(term) >= 5 and self.openai_client:
80+
embedding = self.openai_client.embeddings.create(
81+
model="text-embedding-3-small",
82+
input=term
83+
)
84+
embedding = embedding.data[0].embedding
85+
for type_name in self.types:
86+
chunks=dict(
87+
knn=dict(
88+
field="chunks.embeddings",
89+
query_vector=embedding,
90+
k=10,
91+
num_candidates=50,
92+
boost=0.5
93+
)
94+
)
95+
should = self.must(type_name)[-1]
96+
should['bool']['should'].append(chunks)
6897

6998

7099
def setup_search(app):

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ apies>=1.13.1
1313
dgp-oauth2>=1.3.4
1414
apisql>=0.7.2
1515
XlsxWriter
16-
setuptools==71.0.0
16+
setuptools==71.0.0
17+
openai

0 commit comments

Comments
 (0)