From f426878f9e8af5a170ad754e7dfb7aebe6553ea4 Mon Sep 17 00:00:00 2001 From: killua4396 <1223086337@qq.com> Date: Sat, 18 May 2024 11:09:44 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BF=AE=E6=94=B9requests=E4=B8=BAai?= =?UTF-8?q?ohttp=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=BC=82=E6=AD=A5http=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/chat_controller.py | 157 +++++++++++++++-------------- 1 file changed, 80 insertions(+), 77 deletions(-) diff --git a/app/controllers/chat_controller.py b/app/controllers/chat_controller.py index ff54226..2c42bfb 100644 --- a/app/controllers/chat_controller.py +++ b/app/controllers/chat_controller.py @@ -10,7 +10,7 @@ from config import get_config import uuid import json import asyncio -import requests +import aiohttp # 依赖注入获取logger logger = get_logger() @@ -266,35 +266,36 @@ async def sct_llm_handler(ws,session_id,response_type,llm_info,tts_info,db,redis 'Authorization': f"Bearer {Config.MINIMAX_LLM.API_KEY}", 'Content-Type': 'application/json' } - response = requests.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload,stream=True) #调用大模型 except Exception as e: - logger.error(f"llm调用发生错误: {str(e)}") + logger.error(f"编辑http请求时发生错误: {str(e)}") try: - for chunk in response.iter_lines(): - chunk_data = parseChunkDelta(chunk) - is_end = chunk_data == "end" - if not is_end: - llm_response += chunk_data - sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) #断句 - for sentence in sentences: - if response_type == RESPONSE_TEXT: - response_message = {"type": "text", "code":200, "msg": sentence} - await ws.send_text(json.dumps(response_message, ensure_ascii=False)) #返回文本信息 - elif response_type == RESPONSE_AUDIO: - sr,audio = tts.synthesize(sentence, tts_info["speaker_id"], tts_info["language"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"],return_bytes=True) - response_message = {"type": "text", "code":200, "msg": sentence} - await ws.send_bytes(audio) #返回音频数据 - await ws.send_text(json.dumps(response_message, ensure_ascii=False)) #返回文本信息 - logger.debug(f"websocket返回: {sentence}") - if is_end: - logger.debug(f"llm返回结果: {llm_response}") - await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) - is_end = False #重置is_end标志位 - messages.append({'role': 'assistant', "content": llm_response}) - session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 - redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session - is_first = True - llm_response = "" + async with aiohttp.ClientSession() as client: + async with client.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload) as response: #发送大模型请求 + async for chunk in response.content.iter_any(): + chunk_data = parseChunkDelta(chunk) + is_end = chunk_data == "end" + if not is_end: + llm_response += chunk_data + sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) #断句 + for sentence in sentences: + if response_type == RESPONSE_TEXT: + response_message = {"type": "text", "code":200, "msg": sentence} + await ws.send_text(json.dumps(response_message, ensure_ascii=False)) #返回文本信息 + elif response_type == RESPONSE_AUDIO: + sr,audio = tts.synthesize(sentence, tts_info["speaker_id"], tts_info["language"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"],return_bytes=True) + response_message = {"type": "text", "code":200, "msg": sentence} + await ws.send_bytes(audio) #返回音频数据 + await ws.send_text(json.dumps(response_message, ensure_ascii=False)) #返回文本信息 + logger.debug(f"websocket返回: {sentence}") + if is_end: + logger.debug(f"llm返回结果: {llm_response}") + await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) + is_end = False #重置is_end标志位 + messages.append({'role': 'assistant', "content": llm_response}) + session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 + redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session + is_first = True + llm_response = "" except Exception as e: logger.error(f"处理llm返回结果发生错误: {str(e)}") chat_finished_event.set() @@ -418,34 +419,35 @@ async def scl_llm_handler(ws,session_id,response_type,llm_info,tts_info,db,redis 'Authorization': f"Bearer {Config.MINIMAX_LLM.API_KEY}", 'Content-Type': 'application/json' } - response = requests.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload,stream=True) - for chunk in response.iter_lines(): - chunk_data = parseChunkDelta(chunk) - is_end = chunk_data == "end" - if not is_end: - llm_response += chunk_data - sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) - for sentence in sentences: - if response_type == RESPONSE_TEXT: - logger.debug(f"websocket返回: {sentence}") - response_message = {"type": "text", "code":200, "msg": sentence} - await ws.send_text(json.dumps(response_message, ensure_ascii=False)) - elif response_type == RESPONSE_AUDIO: - sr,audio = tts.synthesize(sentence, tts_info["speaker_id"], tts_info["language"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"],return_bytes=True) - response_message = {"type": "text", "code":200, "msg": sentence} - await ws.send_bytes(audio) - await ws.send_text(json.dumps(response_message, ensure_ascii=False)) - logger.debug(f"websocket返回: {sentence}") - if is_end: - logger.debug(f"llm返回结果: {llm_response}") - await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) - is_end = False + async with aiohttp.ClientSession() as client: + async with client.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload) as response: #发送大模型请求 + async for chunk in response.content.iter_any(): + chunk_data = parseChunkDelta(chunk) + is_end = chunk_data == "end" + if not is_end: + llm_response += chunk_data + sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) + for sentence in sentences: + if response_type == RESPONSE_TEXT: + logger.debug(f"websocket返回: {sentence}") + response_message = {"type": "text", "code":200, "msg": sentence} + await ws.send_text(json.dumps(response_message, ensure_ascii=False)) + elif response_type == RESPONSE_AUDIO: + sr,audio = tts.synthesize(sentence, tts_info["speaker_id"], tts_info["language"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"],return_bytes=True) + response_message = {"type": "text", "code":200, "msg": sentence} + await ws.send_bytes(audio) + await ws.send_text(json.dumps(response_message, ensure_ascii=False)) + logger.debug(f"websocket返回: {sentence}") + if is_end: + logger.debug(f"llm返回结果: {llm_response}") + await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) + is_end = False - messages.append({'role': 'assistant', "content": llm_response}) - session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 - redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session - is_first = True - llm_response = "" + messages.append({'role': 'assistant', "content": llm_response}) + session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 + redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session + is_first = True + llm_response = "" except asyncio.TimeoutError: continue except Exception as e: @@ -578,29 +580,30 @@ async def voice_call_llm_handler(ws,session_id,llm_info,tts_info,db,redis,asr_re 'Authorization': f"Bearer {Config.MINIMAX_LLM.API_KEY}", 'Content-Type': 'application/json' } - response = requests.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload,stream=True) - for chunk in response.iter_lines(): - chunk_data = parseChunkDelta(chunk) - is_end = chunk_data == "end" - if not is_end: - llm_response += chunk_data - sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) - for sentence in sentences: - sr,audio = tts.synthesize(sentence, tts_info["language"], tts_info["speaker_id"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"], return_bytes=True) - text_response = {"type": "llm_text", "code": 200, "msg": sentence} - await ws.send_bytes(audio) #返回音频二进制流数据 - await ws.send_text(json.dumps(text_response, ensure_ascii=False)) #返回文本数据 - logger.debug(f"llm返回结果: {sentence}") - if is_end: - logger.debug(f"llm返回结果: {llm_response}") - await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) - is_end = False + async with aiohttp.ClientSession() as client: + async with client.post(Config.MINIMAX_LLM.URL, headers=headers, data=payload) as response: #发送大模型请求 + async for chunk in response.content.iter_any(): + chunk_data = parseChunkDelta(chunk) + is_end = chunk_data == "end" + if not is_end: + llm_response += chunk_data + sentences,current_sentence,is_first = split_string_with_punctuation(current_sentence,chunk_data,is_first,is_end) + for sentence in sentences: + sr,audio = tts.synthesize(sentence, tts_info["language"], tts_info["speaker_id"], tts_info["noise_scale"], tts_info["noise_scale_w"], tts_info["length_scale"], return_bytes=True) + text_response = {"type": "llm_text", "code": 200, "msg": sentence} + await ws.send_bytes(audio) #返回音频二进制流数据 + await ws.send_text(json.dumps(text_response, ensure_ascii=False)) #返回文本数据 + logger.debug(f"llm返回结果: {sentence}") + if is_end: + logger.debug(f"llm返回结果: {llm_response}") + await ws.send_text(json.dumps({"type": "end", "code": 200, "msg": ""}, ensure_ascii=False)) + is_end = False - messages.append({'role': 'assistant', "content": llm_response}) - session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 - redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session - is_first = True - llm_response = "" + messages.append({'role': 'assistant', "content": llm_response}) + session_content["messages"] = json.dumps(messages,ensure_ascii=False) #更新对话 + redis.set(session_id,json.dumps(session_content,ensure_ascii=False)) #更新session + is_first = True + llm_response = "" except asyncio.TimeoutError: continue except Exception as e: