Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新 webui,支持 st.dialog #3944

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chatchat-server/chatchat/configs/basic_config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions chatchat-server/chatchat/configs/prompt_config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ PROMPT_TEMPLATES = {
'{history}\n'
'Human: {input}\n'
'AI:',
"rag":
'【指令】根据已知信息,简洁和专业的来回答问题。如果无法从中得到答案,请说 “根据已知信息无法回答该问题”,不允许在答案中添加编造成分,答案请使用中文。\n\n'
'【已知信息】{context}\n\n'
'【问题】{question}\n',
},
"action_model": {
"GPT-4":
Expand Down
4 changes: 3 additions & 1 deletion chatchat-server/chatchat/server/api_server/openai_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion chatchat-server/chatchat/server/api_server/server_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os
from typing import Literal

from fastapi import FastAPI, Body
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down
10 changes: 10 additions & 0 deletions chatchat-server/chatchat/server/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,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:
Expand Down
3 changes: 0 additions & 3 deletions chatchat-server/chatchat/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
82 changes: 28 additions & 54 deletions chatchat-server/chatchat/webui.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,71 @@
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',
'Report a bug': "https://github.com/chatchat-space/Langchain-Chatchat/issues",
'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(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px;
[data-testid="stSidebarUserContent"] {
padding-top: 20px;
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 600px;
margin-left: -600px;
.block-container {
padding-top: 25px;
}
[data-testid="stBottomBlockContainer"] {
padding-bottom: 20px;
}

""",
unsafe_allow_html=True,
)
pages = {
"对话": {
"icon": "chat",
"func": dialogue_page,
},
"知识库管理": {
"icon": "hdd-stack",
"func": knowledge_base_page,
},
# "模型服务": {
# "icon": "hdd-stack",
# "func": openai_plugins_page,
# },
}
# 更新状态
# if "status" not in st.session_state \
# or "run_plugins_list" not in st.session_state \
# or "launch_subscribe_info" not in st.session_state \
# or "list_running_models" not in st.session_state \
# or "model_config" not in st.session_state:
# update_store()

with st.sidebar:
st.image(
os.path.join(img_dir, "img", 'logo-long-chatchat-trans-v2.png'),
get_img_url('logo-long-chatchat-trans-v2.png'),
use_column_width=True
)
st.caption(
f"""<p align="right">当前版本:{VERSION}</p>""",
unsafe_allow_html=True,
)
options = list(pages)
icons = [x["icon"] for x in pages.values()]

default_index = 0
selected_page = option_menu(
menu_title="",
selected_page = sac.menu(
[
sac.MenuItem("对话", icon="chat"),
sac.MenuItem("知识库管理", icon="hdd-stack"),
],
key="selected_page",
options=options,
icons=icons,
# menu_icon="chat-quote",
default_index=default_index,
open_index=0
)

if selected_page in pages:
pages[selected_page]["func"](api=api, is_lite=is_lite)
sac.divider()

if selected_page == "知识库管理":
knowledge_base_page(api=api, is_lite=is_lite)
else:
dialogue_page(api=api, is_lite=is_lite)
Loading