@@ -178,7 +178,8 @@ def get_all_dialogs_by_tenant_id(cls, tenant_id):
178178 offset += limit
179179 return res
180180
181- def chat_solo (dialog , messages , stream = True ):
181+
182+ async def async_chat_solo (dialog , messages , stream = True ):
182183 attachments = ""
183184 if "files" in messages [- 1 ]:
184185 attachments = "\n \n " .join (FileService .get_files (messages [- 1 ]["files" ]))
@@ -197,7 +198,8 @@ def chat_solo(dialog, messages, stream=True):
197198 if stream :
198199 last_ans = ""
199200 delta_ans = ""
200- for ans in chat_mdl .chat_streamly (prompt_config .get ("system" , "" ), msg , dialog .llm_setting ):
201+ answer = ""
202+ async for ans in chat_mdl .async_chat_streamly (prompt_config .get ("system" , "" ), msg , dialog .llm_setting ):
201203 answer = ans
202204 delta_ans = ans [len (last_ans ):]
203205 if num_tokens_from_string (delta_ans ) < 16 :
@@ -208,7 +210,7 @@ def chat_solo(dialog, messages, stream=True):
208210 if delta_ans :
209211 yield {"answer" : answer , "reference" : {}, "audio_binary" : tts (tts_mdl , delta_ans ), "prompt" : "" , "created_at" : time .time ()}
210212 else :
211- answer = chat_mdl .chat (prompt_config .get ("system" , "" ), msg , dialog .llm_setting )
213+ answer = await chat_mdl .async_chat (prompt_config .get ("system" , "" ), msg , dialog .llm_setting )
212214 user_content = msg [- 1 ].get ("content" , "[content not available]" )
213215 logging .debug ("User: {}|Assistant: {}" .format (user_content , answer ))
214216 yield {"answer" : answer , "reference" : {}, "audio_binary" : tts (tts_mdl , answer ), "prompt" : "" , "created_at" : time .time ()}
@@ -347,13 +349,12 @@ def filter_out(v2docs, operator, value):
347349 return []
348350 return list (doc_ids )
349351
350-
351- def chat (dialog , messages , stream = True , ** kwargs ):
352+ async def async_chat (dialog , messages , stream = True , ** kwargs ):
352353 assert messages [- 1 ]["role" ] == "user" , "The last content of this conversation is not from user."
353354 if not dialog .kb_ids and not dialog .prompt_config .get ("tavily_api_key" ):
354- for ans in chat_solo (dialog , messages , stream ):
355+ async for ans in async_chat_solo (dialog , messages , stream ):
355356 yield ans
356- return None
357+ return
357358
358359 chat_start_ts = timer ()
359360
@@ -400,7 +401,7 @@ def chat(dialog, messages, stream=True, **kwargs):
400401 ans = use_sql (questions [- 1 ], field_map , dialog .tenant_id , chat_mdl , prompt_config .get ("quote" , True ), dialog .kb_ids )
401402 if ans :
402403 yield ans
403- return None
404+ return
404405
405406 for p in prompt_config ["parameters" ]:
406407 if p ["key" ] == "knowledge" :
@@ -508,7 +509,8 @@ def chat(dialog, messages, stream=True, **kwargs):
508509 empty_res = prompt_config ["empty_response" ]
509510 yield {"answer" : empty_res , "reference" : kbinfos , "prompt" : "\n \n ### Query:\n %s" % " " .join (questions ),
510511 "audio_binary" : tts (tts_mdl , empty_res )}
511- return {"answer" : prompt_config ["empty_response" ], "reference" : kbinfos }
512+ yield {"answer" : prompt_config ["empty_response" ], "reference" : kbinfos }
513+ return
512514
513515 kwargs ["knowledge" ] = "\n ------\n " + "\n \n ------\n \n " .join (knowledges )
514516 gen_conf = dialog .llm_setting
@@ -612,7 +614,7 @@ def decorate_answer(answer):
612614 if stream :
613615 last_ans = ""
614616 answer = ""
615- for ans in chat_mdl .chat_streamly (prompt + prompt4citation , msg [1 :], gen_conf ):
617+ async for ans in chat_mdl .async_chat_streamly (prompt + prompt4citation , msg [1 :], gen_conf ):
616618 if thought :
617619 ans = re .sub (r"^.*</think>" , "" , ans , flags = re .DOTALL )
618620 answer = ans
@@ -626,19 +628,19 @@ def decorate_answer(answer):
626628 yield {"answer" : thought + answer , "reference" : {}, "audio_binary" : tts (tts_mdl , delta_ans )}
627629 yield decorate_answer (thought + answer )
628630 else :
629- answer = chat_mdl .chat (prompt + prompt4citation , msg [1 :], gen_conf )
631+ answer = await chat_mdl .async_chat (prompt + prompt4citation , msg [1 :], gen_conf )
630632 user_content = msg [- 1 ].get ("content" , "[content not available]" )
631633 logging .debug ("User: {}|Assistant: {}" .format (user_content , answer ))
632634 res = decorate_answer (answer )
633635 res ["audio_binary" ] = tts (tts_mdl , answer )
634636 yield res
635637
636- return None
638+ return
637639
638640
639641def use_sql (question , field_map , tenant_id , chat_mdl , quota = True , kb_ids = None ):
640642 sys_prompt = """
641- You are a Database Administrator. You need to check the fields of the following tables based on the user's list of questions and write the SQL corresponding to the last question.
643+ You are a Database Administrator. You need to check the fields of the following tables based on the user's list of questions and write the SQL corresponding to the last question.
642644Ensure that:
6436451. Field names should not start with a digit. If any field name starts with a digit, use double quotes around it.
6446462. Write only the SQL, no explanations or additional text.
@@ -805,8 +807,7 @@ def tts(tts_mdl, text):
805807 return None
806808 return binascii .hexlify (bin ).decode ("utf-8" )
807809
808-
809- def ask (question , kb_ids , tenant_id , chat_llm_name = None , search_config = {}):
810+ async def async_ask (question , kb_ids , tenant_id , chat_llm_name = None , search_config = {}):
810811 doc_ids = search_config .get ("doc_ids" , [])
811812 rerank_mdl = None
812813 kb_ids = search_config .get ("kb_ids" , kb_ids )
@@ -880,7 +881,7 @@ def decorate_answer(answer):
880881 return {"answer" : answer , "reference" : refs }
881882
882883 answer = ""
883- for ans in chat_mdl .chat_streamly (sys_prompt , msg , {"temperature" : 0.1 }):
884+ async for ans in chat_mdl .async_chat_streamly (sys_prompt , msg , {"temperature" : 0.1 }):
884885 answer = ans
885886 yield {"answer" : answer , "reference" : {}}
886887 yield decorate_answer (answer )
0 commit comments