26
26
27
27
28
28
class StorageEngine :
29
- def get_client (self , data_path ):
29
+ __storage_engine = None
30
+
31
+ def __new__ (cls , * args , ** kwargs ):
32
+ if cls .__storage_engine is None :
33
+ cls .__storage_engine = super ().__new__ (cls )
34
+ return cls .__storage_engine
35
+
36
+ # This function is needed only for the unit testing for the
37
+ # mocks to work.
38
+ @classmethod
39
+ def recreate_instance (cls , * args , ** kwargs ):
40
+ cls .__storage_engine = None
41
+ return cls (* args , ** kwargs )
42
+
43
+ def __init__ (self , data_path = "./weaviate_data" ):
44
+ if hasattr (self , "initialized" ):
45
+ return
46
+
47
+ self .initialized = True
48
+ self .data_path = data_path
49
+ self .inference_engine = LlamaCppInferenceEngine ()
50
+ self .model_path = (
51
+ f"{ Config .get_config ().model_base_path } /{ Config .get_config ().embedding_model } "
52
+ )
53
+ self .schema_config = schema_config
54
+
55
+ # setup schema for weaviate
56
+ self .weaviate_client = self .get_client (self .data_path )
57
+ if self .weaviate_client is not None :
58
+ try :
59
+ self .weaviate_client .connect ()
60
+ self .setup_schema (self .weaviate_client )
61
+ except Exception as e :
62
+ logger .error (f"Failed to connect or setup schema: { str (e )} " )
63
+
64
+ def __del__ (self ):
30
65
try :
31
- # Get current config
32
- config = Config .get_config ()
66
+ self .weaviate_client .close ()
67
+ except Exception as e :
68
+ logger .error (f"Failed to close client: { str (e )} " )
33
69
70
+ def get_client (self , data_path ):
71
+ try :
34
72
# Configure Weaviate logging
35
73
additional_env_vars = {
36
74
# Basic logging configuration
37
- "LOG_FORMAT" : config .log_format .value .lower (),
38
- "LOG_LEVEL" : config .log_level .value .lower (),
75
+ "LOG_FORMAT" : Config . get_config () .log_format .value .lower (),
76
+ "LOG_LEVEL" : Config . get_config () .log_level .value .lower (),
39
77
# Disable colored output
40
78
"LOG_FORCE_COLOR" : "false" ,
41
79
# Configure JSON format
42
80
"LOG_JSON_FIELDS" : "timestamp, level,message" ,
43
81
# Configure text format
44
- "LOG_METHOD" : config .log_format .value .lower (),
82
+ "LOG_METHOD" : Config . get_config () .log_format .value .lower (),
45
83
"LOG_LEVEL_IN_UPPER" : "false" , # Keep level lowercase like codegate format
46
84
# Disable additional fields
47
85
"LOG_GIT_HASH" : "false" ,
@@ -60,28 +98,6 @@ def get_client(self, data_path):
60
98
logger .error (f"Error during client creation: { str (e )} " )
61
99
return None
62
100
63
- def __init__ (self , data_path = "./weaviate_data" ):
64
- self .data_path = data_path
65
- self .inference_engine = LlamaCppInferenceEngine ()
66
- self .model_path = "./models/all-minilm-L6-v2-q5_k_m.gguf"
67
- self .schema_config = schema_config
68
-
69
- # setup schema for weaviate
70
- weaviate_client = self .get_client (self .data_path )
71
- if weaviate_client is not None :
72
- try :
73
- weaviate_client .connect ()
74
- self .setup_schema (weaviate_client )
75
- except Exception as e :
76
- logger .error (f"Failed to connect or setup schema: { str (e )} " )
77
- finally :
78
- try :
79
- weaviate_client .close ()
80
- except Exception as e :
81
- logger .error (f"Failed to close client: { str (e )} " )
82
- else :
83
- logger .error ("Could not find client, skipping schema setup." )
84
-
85
101
def setup_schema (self , client ):
86
102
for class_config in self .schema_config :
87
103
if not client .collections .exists (class_config ["name" ]):
@@ -95,18 +111,16 @@ async def search_by_property(self, name: str, properties: List[str]) -> list[obj
95
111
return []
96
112
97
113
# Perform the vector search
98
- weaviate_client = self .get_client (self .data_path )
99
- if weaviate_client is None :
114
+ if self .weaviate_client is None :
100
115
logger .error ("Could not find client, not returning results." )
101
116
return []
102
117
103
- if not weaviate_client :
118
+ if not self . weaviate_client :
104
119
logger .error ("Invalid client, cannot perform search." )
105
120
return []
106
121
107
122
try :
108
- weaviate_client .connect ()
109
- packages = weaviate_client .collections .get ("Package" )
123
+ packages = self .weaviate_client .collections .get ("Package" )
110
124
response = packages .query .fetch_objects (
111
125
filters = Filter .by_property (name ).contains_any (properties ),
112
126
)
@@ -117,8 +131,6 @@ async def search_by_property(self, name: str, properties: List[str]) -> list[obj
117
131
except Exception as e :
118
132
logger .error (f"An error occurred: { str (e )} " )
119
133
return []
120
- finally :
121
- weaviate_client .close ()
122
134
123
135
async def search (self , query : str , limit = 5 , distance = 0.3 , packages = None ) -> list [object ]:
124
136
"""
@@ -135,14 +147,8 @@ async def search(self, query: str, limit=5, distance=0.3, packages=None) -> list
135
147
query_vector = await self .inference_engine .embed (self .model_path , [query ])
136
148
137
149
# Perform the vector search
138
- weaviate_client = self .get_client (self .data_path )
139
- if weaviate_client is None :
140
- logger .error ("Could not find client, not returning results." )
141
- return []
142
-
143
150
try :
144
- weaviate_client .connect ()
145
- collection = weaviate_client .collections .get ("Package" )
151
+ collection = self .weaviate_client .collections .get ("Package" )
146
152
if packages :
147
153
response = collection .query .near_vector (
148
154
query_vector [0 ],
@@ -159,16 +165,10 @@ async def search(self, query: str, limit=5, distance=0.3, packages=None) -> list
159
165
return_metadata = MetadataQuery (distance = True ),
160
166
)
161
167
162
- weaviate_client .close ()
163
168
if not response :
164
169
return []
165
170
return response .objects
166
171
167
172
except Exception as e :
168
173
logger .error (f"Error during search: { str (e )} " )
169
174
return []
170
- finally :
171
- try :
172
- weaviate_client .close ()
173
- except Exception as e :
174
- logger .error (f"Failed to close client: { str (e )} " )
0 commit comments