mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-23 23:40:03 +08:00
## 🛠 新增功能 - 支持百川在线模型 (@hzg0601 @liunux4odoo in #1623) - 支持 Azure OpenAI 与 claude 等 Langchain 自带模型 (@zRzRzRzRzRzRzR in #1808) - Agent 功能大量更新,支持更多的工具、更换提示词、检索知识库 (@zRzRzRzRzRzRzR in #1626 #1666 #1785) - 加长 32k 模型的历史记录 (@zRzRzRzRzRzRzR in #1629 #1630) - *_chat 接口支持 max_tokens 参数 (@liunux4odoo in #1744) - 实现 API 和 WebUI 的前后端分离 (@liunux4odoo in #1772) - 支持 zlilliz 向量库 (@zRzRzRzRzRzRzR in #1785) - 支持 metaphor 搜索引擎 (@liunux4odoo in #1792) - 支持 p-tuning 模型 (@hzg0601 in #1810) - 更新完善文档和 Wiki (@imClumsyPanda @zRzRzRzRzRzRzR @glide-the in #1680 #1811) ## 🐞 问题修复 - 修复 bge-* 模型匹配超过 1 的问题 (@zRzRzRzRzRzRzR in #1652) - 修复系统代理为空的问题 (@glide-the in #1654) - 修复重建知识库时 `d == self.d assert error` (@liunux4odoo in #1766) - 修复对话历史消息错误 (@liunux4odoo in #1801) - 修复 OpenAI 无法调用的 bug (@zRzRzRzRzRzRzR in #1808) - 修复 windows下 BIND_HOST=0.0.0.0 时对话出错的问题 (@hzg0601 in #1810)
73 lines
3.3 KiB
Python
73 lines
3.3 KiB
Python
from fastapi import Body
|
||
from fastapi.responses import StreamingResponse
|
||
from configs import LLM_MODEL, TEMPERATURE
|
||
from server.utils import wrap_done, get_ChatOpenAI
|
||
from langchain.chains import LLMChain
|
||
from langchain.callbacks import AsyncIteratorCallbackHandler
|
||
from typing import AsyncIterable
|
||
import asyncio
|
||
from langchain.prompts.chat import ChatPromptTemplate
|
||
from typing import List
|
||
from server.chat.utils import History
|
||
from server.utils import get_prompt_template
|
||
|
||
|
||
async def chat(query: str = Body(..., description="用户输入", examples=["恼羞成怒"]),
|
||
history: List[History] = Body([],
|
||
description="历史对话",
|
||
examples=[[
|
||
{"role": "user", "content": "我们来玩成语接龙,我先来,生龙活虎"},
|
||
{"role": "assistant", "content": "虎头虎脑"}]]
|
||
),
|
||
stream: bool = Body(False, description="流式输出"),
|
||
model_name: str = Body(LLM_MODEL, description="LLM 模型名称。"),
|
||
temperature: float = Body(TEMPERATURE, description="LLM 采样温度", ge=0.0, le=1.0),
|
||
max_tokens: int = Body(None, description="限制LLM生成Token数量,默认None代表模型最大值"),
|
||
# top_p: float = Body(TOP_P, description="LLM 核采样。勿与temperature同时设置", gt=0.0, lt=1.0),
|
||
prompt_name: str = Body("default", description="使用的prompt模板名称(在configs/prompt_config.py中配置)"),
|
||
):
|
||
history = [History.from_data(h) for h in history]
|
||
|
||
async def chat_iterator(query: str,
|
||
history: List[History] = [],
|
||
model_name: str = LLM_MODEL,
|
||
prompt_name: str = prompt_name,
|
||
) -> AsyncIterable[str]:
|
||
callback = AsyncIteratorCallbackHandler()
|
||
model = get_ChatOpenAI(
|
||
model_name=model_name,
|
||
temperature=temperature,
|
||
max_tokens=max_tokens,
|
||
callbacks=[callback],
|
||
)
|
||
|
||
prompt_template = get_prompt_template("llm_chat", prompt_name)
|
||
input_msg = History(role="user", content=prompt_template).to_msg_template(False)
|
||
chat_prompt = ChatPromptTemplate.from_messages(
|
||
[i.to_msg_template() for i in history] + [input_msg])
|
||
chain = LLMChain(prompt=chat_prompt, llm=model)
|
||
|
||
# Begin a task that runs in the background.
|
||
task = asyncio.create_task(wrap_done(
|
||
chain.acall({"input": query}),
|
||
callback.done),
|
||
)
|
||
|
||
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
|
||
|
||
await task
|
||
|
||
return StreamingResponse(chat_iterator(query=query,
|
||
history=history,
|
||
model_name=model_name,
|
||
prompt_name=prompt_name),
|
||
media_type="text/event-stream")
|