From 9fddd07afd19ff6abe5e644cecb830903e3c9620 Mon Sep 17 00:00:00 2001 From: liunux4odoo <41217877+liunux4odoo@users.noreply.github.com> Date: Mon, 6 May 2024 09:09:56 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=96=B0=E5=8A=9F=E8=83=BD=20(#3944)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - streamlit 更新到 1.34,webui 支持 Dialog 操作 - streamlit-chatbox 更新到 1.1.12,更好的多会话支持 - 开发者 - 在 API 中增加项目图片路由(/img/{file_name}),方便前端使用 --- .../chatchat/configs/basic_config.py.example | 5 +- .../chatchat/configs/prompt_config.py.example | 4 + .../server/api_server/openai_routes.py | 4 +- .../chatchat/server/api_server/server_app.py | 7 +- chatchat-server/chatchat/server/chat/chat.py | 10 + chatchat-server/chatchat/server/utils.py | 3 - chatchat-server/chatchat/webui.py | 82 +-- .../chatchat/webui_pages/dialogue/dialogue.py | 531 +++++++++--------- chatchat-server/chatchat/webui_pages/utils.py | 8 + requirements.txt | 10 +- 10 files changed, 349 insertions(+), 315 deletions(-) diff --git a/chatchat-server/chatchat/configs/basic_config.py.example b/chatchat-server/chatchat/configs/basic_config.py.example index f8cd6bbdb4..d172c90b3b 100644 --- a/chatchat-server/chatchat/configs/basic_config.py.example +++ b/chatchat-server/chatchat/configs/basic_config.py.example @@ -11,8 +11,11 @@ langchain.verbose = False # 通常情况下不需要更改以下内容 +# chatchat 项目根目录 +CHATCHAT_ROOT = str(Path(__file__).absolute().parent.parent) + # 用户数据根目录 -DATA_PATH = str(Path(__file__).absolute().parent.parent / "data") +DATA_PATH = os.path.join(CHATCHAT_ROOT, "data") if not os.path.exists(DATA_PATH): os.mkdir(DATA_PATH) diff --git a/chatchat-server/chatchat/configs/prompt_config.py.example b/chatchat-server/chatchat/configs/prompt_config.py.example index c94465836a..701b1b8985 100644 --- a/chatchat-server/chatchat/configs/prompt_config.py.example +++ b/chatchat-server/chatchat/configs/prompt_config.py.example @@ -19,6 +19,10 @@ PROMPT_TEMPLATES = { '{history}\n' 'Human: {input}\n' 'AI:', + "rag": + '【指令】根据已知信息,简洁和专业的来回答问题。如果无法从中得到答案,请说 “根据已知信息无法回答该问题”,不允许在答案中添加编造成分,答案请使用中文。\n\n' + '【已知信息】{context}\n\n' + '【问题】{question}\n', }, "action_model": { "GPT-4": diff --git a/chatchat-server/chatchat/server/api_server/openai_routes.py b/chatchat-server/chatchat/server/api_server/openai_routes.py index bde5b04c27..5e71307362 100644 --- a/chatchat-server/chatchat/server/api_server/openai_routes.py +++ b/chatchat-server/chatchat/server/api_server/openai_routes.py @@ -16,7 +16,7 @@ from sse_starlette.sse import EventSourceResponse from .api_schemas import * -from chatchat.configs import logger, BASE_TEMP_DIR +from chatchat.configs import logger, BASE_TEMP_DIR, log_verbose from chatchat.server.utils import get_model_info, get_config_platforms, get_OpenAIClient @@ -126,6 +126,8 @@ async def create_chat_completions( request: Request, body: OpenAIChatInput, ): + if log_verbose: + print(body) async with get_model_client(body.model) as client: result = await openai_request(client.chat.completions.create, body) return result diff --git a/chatchat-server/chatchat/server/api_server/server_app.py b/chatchat-server/chatchat/server/api_server/server_app.py index 693718a170..3689044f58 100644 --- a/chatchat-server/chatchat/server/api_server/server_app.py +++ b/chatchat-server/chatchat/server/api_server/server_app.py @@ -1,4 +1,5 @@ import argparse +import os from typing import Literal from fastapi import FastAPI, Body @@ -7,7 +8,7 @@ from starlette.responses import RedirectResponse import uvicorn -from chatchat.configs import VERSION, MEDIA_PATH +from chatchat.configs import VERSION, MEDIA_PATH, CHATCHAT_ROOT from chatchat.configs.server_config import OPEN_CROSS_DOMAIN from chatchat.server.api_server.chat_routes import chat_router from chatchat.server.api_server.kb_routes import kb_router @@ -55,6 +56,10 @@ async def document(): # 媒体文件 app.mount("/media", StaticFiles(directory=MEDIA_PATH), name="media") + # 项目相关图片 + img_dir = os.path.join(CHATCHAT_ROOT, "img") + app.mount("/img", StaticFiles(directory=img_dir), name="img") + return app diff --git a/chatchat-server/chatchat/server/chat/chat.py b/chatchat-server/chatchat/server/chat/chat.py index 6c043d8a53..6147b376e1 100644 --- a/chatchat-server/chatchat/server/chat/chat.py +++ b/chatchat-server/chatchat/server/chat/chat.py @@ -202,6 +202,16 @@ async def chat_iterator() -> AsyncIterable[OpenAIChatOutput]: message_id=message_id, ) yield ret.model_dump_json() + yield OpenAIChatOutput( # return blank text lastly + id=f"chat{uuid.uuid4()}", + object="chat.completion.chunk", + content="", + role="assistant", + model=models["llm_model"].model_name, + status = data["status"], + message_type = data["message_type"], + message_id=message_id, + ) await task if stream: diff --git a/chatchat-server/chatchat/server/utils.py b/chatchat-server/chatchat/server/utils.py index 06a73f05dd..b059981daf 100644 --- a/chatchat-server/chatchat/server/utils.py +++ b/chatchat-server/chatchat/server/utils.py @@ -664,9 +664,6 @@ def get_httpx_client( # construct Client kwargs.update(timeout=timeout, proxies=default_proxies) - if log_verbose: - logger.info(f'{get_httpx_client.__class__.__name__}:kwargs: {kwargs}') - if use_async: return httpx.AsyncClient(**kwargs) else: diff --git a/chatchat-server/chatchat/webui.py b/chatchat-server/chatchat/webui.py index 7739235f22..b24f0ed000 100644 --- a/chatchat-server/chatchat/webui.py +++ b/chatchat-server/chatchat/webui.py @@ -1,30 +1,23 @@ +import sys + import streamlit as st +import streamlit_antd_components as sac -# from chatchat.webui_pages.loom_view_client import update_store -# from chatchat.webui_pages.openai_plugins import openai_plugins_page +from chatchat.configs import VERSION +from chatchat.server.utils import api_address from chatchat.webui_pages.utils import * -from streamlit_option_menu import option_menu from chatchat.webui_pages.dialogue.dialogue import dialogue_page, chat_box from chatchat.webui_pages.knowledge_base.knowledge_base import knowledge_base_page -import os -import sys -from chatchat.configs import VERSION -from chatchat.server.utils import api_address -# def on_change(key): -# if key: -# update_store() -img_dir = os.path.dirname(os.path.abspath(__file__)) - api = ApiRequest(base_url=api_address()) if __name__ == "__main__": - is_lite = "lite" in sys.argv + is_lite = "lite" in sys.argv # TODO: remove lite mode st.set_page_config( "Langchain-Chatchat WebUI", - os.path.join(img_dir, "img", "chatchat_icon_blue_square_v2.png"), + get_img_url("chatchat_icon_blue_square_v2.png"), initial_sidebar_state="expanded", menu_items={ 'Get Help': 'https://github.com/chatchat-space/Langchain-Chatchat', @@ -32,66 +25,47 @@ 'About': f"""欢迎使用 Langchain-Chatchat WebUI {VERSION}!""" }, layout="wide" - ) # use the following code to set the app to wide mode and the html markdown to increase the sidebar width st.markdown( """