feat: fastchat

This commit is contained in:
superobk
2024-04-11 15:40:15 +08:00
parent a717b643b8
commit 6c4bcf2533
2 changed files with 13 additions and 11 deletions

View File

@ -8,9 +8,9 @@ from .blackbox import Blackbox
from .calculator import Calculator from .calculator import Calculator
from .text_to_audio import TextToAudio from .text_to_audio import TextToAudio
from .tesou import Tesou from .tesou import Tesou
from .fastcaht import Fastchat from .fastchat import Fastchat
class BlackboxFactor: class BlackboxFactory:
def __init__(self) -> None: def __init__(self) -> None:
self.tts = TTS() self.tts = TTS()

View File

@ -17,7 +17,9 @@ class Fastchat(Blackbox):
return isinstance(data, list) return isinstance(data, list)
# model_name有 Qwen1.5-14B-Chat , internlm2-chat-20b # 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' url = 'http://120.196.116.194:48892/v1/chat/completions'
# history可以为空列表也可以是用户的对话历史 # history可以为空列表也可以是用户的对话历史
@ -31,11 +33,10 @@ class Fastchat(Blackbox):
# "content": "作为一个AI模型我没有吃饭的需要因为我并不具备实体形态。我专注于提供信息和帮助回答你的问题。你有什么需要帮助的吗" # "content": "作为一个AI模型我没有吃饭的需要因为我并不具备实体形态。我专注于提供信息和帮助回答你的问题。你有什么需要帮助的吗"
# }, # },
# ] # ]
history = []
fastchat_inputs={ fastchat_inputs={
"model": model_name, "model": model_name,
"messages": history + [ "messages": context + [
{ {
"role": "user", "role": "user",
"content": template + prompt "content": template + prompt
@ -46,10 +47,10 @@ class Fastchat(Blackbox):
fastchat_response = requests.post(url, json=fastchat_inputs) fastchat_response = requests.post(url, json=fastchat_inputs)
user_message = fastchat_inputs["messages"] user_message = fastchat_inputs["messages"]
history.append(user_message) context.append(user_message)
assistant_message = fastchat_response.json()["choices"][0]["message"] assistant_message = fastchat_response.json()["choices"][0]["message"]
history.append(assistant_message) context.append(assistant_message)
fastchat_content = assistant_message["content"] fastchat_content = assistant_message["content"]
@ -61,9 +62,10 @@ class Fastchat(Blackbox):
except: except:
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST) return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
user_model_name = data.get("model_name") user_model_name = data.get("model_name")
user_prompt = data.get("prompt") user_context = data.get("context")
user_template = data.get("template") user_prompt = data.get("prompt")
user_template = data.get("template")
if user_prompt is None: if user_prompt is None:
return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST) return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST)
@ -77,4 +79,4 @@ class Fastchat(Blackbox):
else: else:
user_template = f"使用{user_template}的语气说话。" user_template = f"使用{user_template}的语气说话。"
return JSONResponse(content={"Response": self.processing(user_model_name, user_prompt, user_template)}, status_code=status.HTTP_200_OK) return JSONResponse(content={"response": self.processing(user_model_name, user_prompt, user_template, user_context)}, status_code=status.HTTP_200_OK)