From 6c4bcf2533c19a62c08b4f49427015587a834fe7 Mon Sep 17 00:00:00 2001 From: superobk Date: Thu, 11 Apr 2024 15:40:15 +0800 Subject: [PATCH] feat: fastchat --- src/blackbox/blackbox_factory.py | 4 ++-- src/blackbox/fastchat.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/blackbox/blackbox_factory.py b/src/blackbox/blackbox_factory.py index 487815c..5e3219f 100644 --- a/src/blackbox/blackbox_factory.py +++ b/src/blackbox/blackbox_factory.py @@ -8,9 +8,9 @@ from .blackbox import Blackbox from .calculator import Calculator from .text_to_audio import TextToAudio from .tesou import Tesou -from .fastcaht import Fastchat +from .fastchat import Fastchat -class BlackboxFactor: +class BlackboxFactory: def __init__(self) -> None: self.tts = TTS() diff --git a/src/blackbox/fastchat.py b/src/blackbox/fastchat.py index fb0188c..94af2b1 100755 --- a/src/blackbox/fastchat.py +++ b/src/blackbox/fastchat.py @@ -17,7 +17,9 @@ class Fastchat(Blackbox): return isinstance(data, list) # model_name有 Qwen1.5-14B-Chat , internlm2-chat-20b - def processing(self, model_name, prompt, template) -> str: + def processing(self, model_name, prompt, template, context: list) -> str: + if context == None: + context = [] url = 'http://120.196.116.194:48892/v1/chat/completions' # history可以为空列表,也可以是用户的对话历史 @@ -31,11 +33,10 @@ class Fastchat(Blackbox): # "content": "作为一个AI模型,我没有吃饭的需要,因为我并不具备实体形态。我专注于提供信息和帮助回答你的问题。你有什么需要帮助的吗?" # }, # ] - history = [] fastchat_inputs={ "model": model_name, - "messages": history + [ + "messages": context + [ { "role": "user", "content": template + prompt @@ -46,10 +47,10 @@ class Fastchat(Blackbox): fastchat_response = requests.post(url, json=fastchat_inputs) user_message = fastchat_inputs["messages"] - history.append(user_message) + context.append(user_message) assistant_message = fastchat_response.json()["choices"][0]["message"] - history.append(assistant_message) + context.append(assistant_message) fastchat_content = assistant_message["content"] @@ -61,9 +62,10 @@ class Fastchat(Blackbox): except: return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST) - user_model_name = data.get("model_name") - user_prompt = data.get("prompt") - user_template = data.get("template") + user_model_name = data.get("model_name") + user_context = data.get("context") + user_prompt = data.get("prompt") + user_template = data.get("template") if user_prompt is None: return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST) @@ -77,4 +79,4 @@ class Fastchat(Blackbox): else: user_template = f"使用{user_template}的语气说话。" - return JSONResponse(content={"Response": self.processing(user_model_name, user_prompt, user_template)}, status_code=status.HTTP_200_OK) \ No newline at end of file + return JSONResponse(content={"response": self.processing(user_model_name, user_prompt, user_template, user_context)}, status_code=status.HTTP_200_OK) \ No newline at end of file