18
18
19
19
from server import redis_client
20
20
from server .config import VECTOR_DIMENSION , RedisDocument
21
+ from server .utils import custom_log
21
22
22
23
assert redis_client is not None
23
24
@@ -35,7 +36,7 @@ def load_corpus(corpus: list[RedisDocument]):
35
36
Raises:
36
37
exception: if failed to load corpus into redis
37
38
"""
38
- print ("loading corpus..." )
39
+ custom_log ("loading corpus..." )
39
40
40
41
pipeline = redis_client .pipeline ()
41
42
for i , doc in enumerate (corpus , start = 1 ):
@@ -45,7 +46,7 @@ def load_corpus(corpus: list[RedisDocument]):
45
46
46
47
if not all (res ):
47
48
raise Exception ("failed to load some documents" )
48
- print ("successfully loaded all documents" )
49
+ custom_log ("successfully loaded all documents" )
49
50
50
51
51
52
def compute_openai_embeddings (texts ):
@@ -69,7 +70,7 @@ def compute_openai_embeddings(texts):
69
70
70
71
def compute_embeddings ():
71
72
"""Compute embeddings from redis documents."""
72
- print ("computing embeddings..." )
73
+ custom_log ("computing embeddings..." )
73
74
74
75
# get keys, questions, content
75
76
keys = sorted (redis_client .keys ("documents:*" )) # type: ignore
@@ -84,7 +85,7 @@ def compute_embeddings():
84
85
85
86
embeddings = compute_openai_embeddings (question_and_content )
86
87
87
- print ("successfully computed embeddings" )
88
+ custom_log ("successfully computed embeddings" )
88
89
return embeddings
89
90
90
91
@@ -98,7 +99,7 @@ def load_embeddings(embeddings: list[list[float]]):
98
99
Raises:
99
100
exception: if failed to load embeddings into redis
100
101
"""
101
- print ("loading embeddings into redis..." )
102
+ custom_log ("loading embeddings into redis..." )
102
103
103
104
# load embeddings into redis
104
105
pipeline = redis_client .pipeline ()
@@ -110,7 +111,7 @@ def load_embeddings(embeddings: list[list[float]]):
110
111
if not all (res ):
111
112
raise Exception ("failed to load embeddings" )
112
113
113
- print ("successfully loaded all embeddings" )
114
+ custom_log ("successfully loaded all embeddings" )
114
115
115
116
116
117
def create_index (corpus_len : int ):
@@ -125,7 +126,7 @@ def create_index(corpus_len: int):
125
126
Raises:
126
127
exception: if failed to create index
127
128
"""
128
- print ("creating index..." )
129
+ custom_log ("creating index..." )
129
130
130
131
schema = (
131
132
TextField ("$.source" , no_stem = True , as_name = "source" ),
@@ -157,7 +158,7 @@ def create_index(corpus_len: int):
157
158
info = redis_client .ft ("idx:documents_vss" ).info ()
158
159
num_docs = info ["num_docs" ]
159
160
indexing_failures = info ["hash_indexing_failures" ]
160
- print ("num_docs" , num_docs , "indexing_failures" , indexing_failures )
161
+ custom_log ("num_docs" , num_docs , "indexing_failures" , indexing_failures )
161
162
return
162
163
if time .time () - start >= 60 :
163
164
raise Exception ("time out" )
@@ -191,7 +192,7 @@ def queries(query, queries: list[str]) -> list[dict]:
191
192
Returns:
192
193
list of dictionaries containing query and result
193
194
"""
194
- print ("running queries..." )
195
+ custom_log ("running queries..." )
195
196
196
197
# encode queries
197
198
encoded_queries = compute_openai_embeddings (queries )
@@ -221,7 +222,7 @@ def queries(query, queries: list[str]) -> list[dict]:
221
222
)
222
223
results_list .append ({"query" : queries [i ], "result" : query_result })
223
224
224
- print ("done running query" )
225
+ custom_log ("done running query" )
225
226
return results_list
226
227
227
228
@@ -249,9 +250,9 @@ def embed_corpus(corpus: list[RedisDocument]):
249
250
exception: if failed to load corpus
250
251
"""
251
252
# flush database
252
- print ("cleaning database..." )
253
+ custom_log ("cleaning database..." )
253
254
redis_client .flushdb ()
254
- print ("done cleaning database" )
255
+ custom_log ("done cleaning database" )
255
256
256
257
# embed corpus
257
258
if not corpus :
0 commit comments