Kung Quang commited on
Commit
b4bc2db
·
1 Parent(s): 199f5c8

Fix api reference empty bug (#1655)

Browse files

### What problem does this PR solve?

fix api reference empty bug
```
for chunk_i in answer['reference'].get('chunks',[]):
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get'
```
```
return np.array([d["relevance_score"] for d in res["results"]]), res["meta"]["tokens"]["input_tokens"]+res["meta"]["tokens"]["output_tokens"]
~~~^^^^^^^^^^^
KeyError: 'results'
```
### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Files changed (2) hide show
  1. api/apps/api_app.py +7 -7
  2. rag/llm/rerank_model.py +2 -0
api/apps/api_app.py CHANGED
@@ -199,9 +199,10 @@ def completion():
199
  conv.message[-1] = {"role": "assistant", "content": ans["answer"]}
200
 
201
  def rename_field(ans):
202
- for chunk_i in ans['reference'].get('chunks', []):
203
- chunk_i['doc_name'] = chunk_i['docnm_kwd']
204
- chunk_i.pop('docnm_kwd')
 
205
 
206
  def stream():
207
  nonlocal dia, msg, req, conv
@@ -232,10 +233,7 @@ def completion():
232
  API4ConversationService.append_message(conv.id, conv.to_dict())
233
  break
234
 
235
- for chunk_i in answer['reference'].get('chunks',[]):
236
- chunk_i['doc_name'] = chunk_i['docnm_kwd']
237
- chunk_i.pop('docnm_kwd')
238
-
239
  return get_json_result(data=answer)
240
 
241
  except Exception as e:
@@ -252,6 +250,8 @@ def get(conversation_id):
252
 
253
  conv = conv.to_dict()
254
  for referenct_i in conv['reference']:
 
 
255
  for chunk_i in referenct_i['chunks']:
256
  if 'docnm_kwd' in chunk_i.keys():
257
  chunk_i['doc_name'] = chunk_i['docnm_kwd']
 
199
  conv.message[-1] = {"role": "assistant", "content": ans["answer"]}
200
 
201
  def rename_field(ans):
202
+ for chunk in ans.get('reference', []):
203
+ for chunk_i in chunk.get('chunks', []):
204
+ chunk_i['doc_name'] = chunk_i['docnm_kwd']
205
+ chunk_i.pop('docnm_kwd')
206
 
207
  def stream():
208
  nonlocal dia, msg, req, conv
 
233
  API4ConversationService.append_message(conv.id, conv.to_dict())
234
  break
235
 
236
+ rename_field(answer)
 
 
 
237
  return get_json_result(data=answer)
238
 
239
  except Exception as e:
 
250
 
251
  conv = conv.to_dict()
252
  for referenct_i in conv['reference']:
253
+ if referenct_i is None or len(referenct_i) == 0:
254
+ continue
255
  for chunk_i in referenct_i['chunks']:
256
  if 'docnm_kwd' in chunk_i.keys():
257
  chunk_i['doc_name'] = chunk_i['docnm_kwd']
rag/llm/rerank_model.py CHANGED
@@ -147,6 +147,8 @@ class XInferenceRerank(Base):
147
  }
148
 
149
  def similarity(self, query: str, texts: list):
 
 
150
  data = {
151
  "model": self.model_name,
152
  "query": query,
 
147
  }
148
 
149
  def similarity(self, query: str, texts: list):
150
+ if len(texts) == 0:
151
+ return np.array([]), 0
152
  data = {
153
  "model": self.model_name,
154
  "query": query,