matsuap commited on
Commit
5a18e3b
·
1 Parent(s): 76a3ac5

住所処理の埋め込み機能を改善し、リクエストを2048件ずつ分割して処理するように変更。全てのレスポンスを統合して返すように修正しました。

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -221,23 +221,28 @@ class InferenceEndpointError(Exception):
221
 
222
  def embed_via_multilingual_e5_large(query_addresses):
223
  headers = {
224
- "Accept" : "application/json",
225
  "Authorization": f"Bearer {HUGGING_FACE_TOKEN}",
226
- "Content-Type": "application/json"
227
  }
228
 
229
- response = requests.post(EMBEDDING_MODEL_ENDPOINT, headers=headers, json={"inputs": query_addresses})
230
- response_json = response.json()
231
-
232
- if 'error' in response_json:
233
- if response_json['error'] == 'Bad Request: Invalid state':
234
- raise InferenceEndpointError(InferenceEndpointErrorCode.INVALID_STATE, "Bad Request: Invalid state")
235
- elif response_json['error'] == '503 Service Unavailable':
236
- raise InferenceEndpointError(InferenceEndpointErrorCode.SERVICE_UNAVAILABLE, "Service Unavailable")
237
- else:
238
- raise InferenceEndpointError(InferenceEndpointErrorCode.UNKNOWN_ERROR, response_json['error'])
239
-
240
- return response_json
 
 
 
 
 
241
 
242
  def search_via_milvus(query_vector, top_k, collection_name, thresh=0.0):
243
  search_params = {"metric_type": "COSINE", "params": {"nprobe": 10}} # MiniLM系はCOSINE推奨
 
221
 
222
  def embed_via_multilingual_e5_large(query_addresses):
223
  headers = {
224
+ "Accept": "application/json",
225
  "Authorization": f"Bearer {HUGGING_FACE_TOKEN}",
226
+ "Content-Type": "application/json"
227
  }
228
 
229
+ all_responses = []
230
+ for i in range(0, len(query_addresses), 2048):
231
+ chunk = query_addresses[i:i + 2048]
232
+ response = requests.post(EMBEDDING_MODEL_ENDPOINT, headers=headers, json={"inputs": chunk})
233
+ response_json = response.json()
234
+
235
+ if 'error' in response_json:
236
+ if response_json['error'] == 'Bad Request: Invalid state':
237
+ raise InferenceEndpointError(InferenceEndpointErrorCode.INVALID_STATE, "Bad Request: Invalid state")
238
+ elif response_json['error'] == '503 Service Unavailable':
239
+ raise InferenceEndpointError(InferenceEndpointErrorCode.SERVICE_UNAVAILABLE, "Service Unavailable")
240
+ else:
241
+ raise InferenceEndpointError(InferenceEndpointErrorCode.UNKNOWN_ERROR, response_json['error'])
242
+
243
+ all_responses.extend(response_json)
244
+
245
+ return all_responses
246
 
247
  def search_via_milvus(query_vector, top_k, collection_name, thresh=0.0):
248
  search_params = {"metric_type": "COSINE", "params": {"nprobe": 10}} # MiniLM系はCOSINE推奨