2023-07-27 23:22:07 +08:00
|
|
|
from fastapi import Body
|
|
|
|
|
from fastapi.responses import StreamingResponse
|
2023-09-17 13:27:11 +08:00
|
|
|
from configs import LLM_MODEL, TEMPERATURE
|
2023-09-17 16:19:50 +08:00
|
|
|
from server.utils import wrap_done, get_ChatOpenAI
|
2023-07-27 23:22:07 +08:00
|
|
|
from langchain import LLMChain
|
|
|
|
|
from langchain.callbacks import AsyncIteratorCallbackHandler
|
|
|
|
|
from typing import AsyncIterable
|
|
|
|
|
import asyncio
|
2023-08-08 23:54:51 +08:00
|
|
|
from langchain.prompts.chat import ChatPromptTemplate
|
2023-08-10 21:26:05 +08:00
|
|
|
from typing import List
|
2023-08-08 23:54:51 +08:00
|
|
|
from server.chat.utils import History
|
2023-09-17 13:27:11 +08:00
|
|
|
from server.utils import get_prompt_template
|
2023-07-27 23:22:07 +08:00
|
|
|
|
|
|
|
|
|
2023-09-08 12:25:02 +08:00
|
|
|
async def chat(query: str = Body(..., description="用户输入", examples=["恼羞成怒"]),
|
|
|
|
|
history: List[History] = Body([],
|
2023-08-10 21:26:05 +08:00
|
|
|
description="历史对话",
|
|
|
|
|
examples=[[
|
|
|
|
|
{"role": "user", "content": "我们来玩成语接龙,我先来,生龙活虎"},
|
|
|
|
|
{"role": "assistant", "content": "虎头虎脑"}]]
|
|
|
|
|
),
|
2023-09-13 10:00:54 +08:00
|
|
|
stream: bool = Body(False, description="流式输出"),
|
|
|
|
|
model_name: str = Body(LLM_MODEL, description="LLM 模型名称。"),
|
2023-09-27 21:17:50 +08:00
|
|
|
temperature: float = Body(TEMPERATURE, description="LLM 采样温度", ge=0.0, le=1.0),
|
2023-09-13 10:00:54 +08:00
|
|
|
# top_p: float = Body(TOP_P, description="LLM 核采样。勿与temperature同时设置", gt=0.0, lt=1.0),
|
2023-09-17 13:27:11 +08:00
|
|
|
prompt_name: str = Body("llm_chat", description="使用的prompt模板名称(在configs/prompt_config.py中配置)"),
|
2023-08-08 23:54:51 +08:00
|
|
|
):
|
2023-08-23 08:35:26 +08:00
|
|
|
history = [History.from_data(h) for h in history]
|
2023-08-10 21:26:05 +08:00
|
|
|
|
2023-08-08 23:54:51 +08:00
|
|
|
async def chat_iterator(query: str,
|
2023-08-09 10:48:37 +08:00
|
|
|
history: List[History] = [],
|
2023-09-01 23:58:09 +08:00
|
|
|
model_name: str = LLM_MODEL,
|
2023-09-17 13:27:11 +08:00
|
|
|
prompt_name: str = prompt_name,
|
2023-08-08 23:54:51 +08:00
|
|
|
) -> AsyncIterable[str]:
|
2023-07-27 23:22:07 +08:00
|
|
|
callback = AsyncIteratorCallbackHandler()
|
2023-09-15 17:52:22 +08:00
|
|
|
model = get_ChatOpenAI(
|
2023-09-01 23:58:09 +08:00
|
|
|
model_name=model_name,
|
2023-09-13 10:00:54 +08:00
|
|
|
temperature=temperature,
|
2023-09-15 17:52:22 +08:00
|
|
|
callbacks=[callback],
|
2023-07-27 23:22:07 +08:00
|
|
|
)
|
2023-09-17 13:27:11 +08:00
|
|
|
|
|
|
|
|
prompt_template = get_prompt_template(prompt_name)
|
|
|
|
|
input_msg = History(role="user", content=prompt_template).to_msg_template(False)
|
2023-08-08 23:54:51 +08:00
|
|
|
chat_prompt = ChatPromptTemplate.from_messages(
|
2023-08-23 08:35:26 +08:00
|
|
|
[i.to_msg_template() for i in history] + [input_msg])
|
2023-08-08 23:54:51 +08:00
|
|
|
chain = LLMChain(prompt=chat_prompt, llm=model)
|
2023-07-27 23:22:07 +08:00
|
|
|
|
|
|
|
|
# Begin a task that runs in the background.
|
|
|
|
|
task = asyncio.create_task(wrap_done(
|
2023-08-08 23:54:51 +08:00
|
|
|
chain.acall({"input": query}),
|
2023-07-27 23:22:07 +08:00
|
|
|
callback.done),
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-14 10:35:47 +08:00
|
|
|
if stream:
|
|
|
|
|
async for token in callback.aiter():
|
|
|
|
|
# Use server-sent-events to stream the response
|
|
|
|
|
yield token
|
|
|
|
|
else:
|
|
|
|
|
answer = ""
|
|
|
|
|
async for token in callback.aiter():
|
|
|
|
|
answer += token
|
|
|
|
|
yield answer
|
|
|
|
|
|
2023-07-27 23:22:07 +08:00
|
|
|
await task
|
2023-08-08 23:54:51 +08:00
|
|
|
|
2023-09-17 13:27:11 +08:00
|
|
|
return StreamingResponse(chat_iterator(query=query,
|
|
|
|
|
history=history,
|
|
|
|
|
model_name=model_name,
|
|
|
|
|
prompt_name=prompt_name),
|
2023-08-08 23:54:51 +08:00
|
|
|
media_type="text/event-stream")
|