Skip to content

Commit

Permalink
Feat/0.2.4 (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit authored Apr 10, 2024
2 parents 81c8542 + f2e94a4 commit 7230892
Show file tree
Hide file tree
Showing 41 changed files with 874 additions and 141 deletions.
3 changes: 3 additions & 0 deletions docker/docker-compose-model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ services:
device_ids: ['0,1'] # 指定想映射给rt服务使用的宿主机上的GPU ID号,如想映射多个卡,可写为['0','1','2']
environment:
TZ: Asia/Shanghai
# 不使用闭源模型的话,用下面的启动命令
command: ["./bin/rtserver", "f"]
# 使用闭源模型的话,用下面的启动命令,地址替换为授权地址
# command: ["bash", "bin/entrypoint.sh", "--serveraddr=<license srv host>"]
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/data/llm:/opt/bisheng-rt/models/model_repository # 冒号前为宿主机上放置模型目录的路径,请根据实际环境修改;冒号后为映射到容器内的路径,请勿修改
healthcheck:
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
- ${DOCKER_VOLUME_DIRECTORY:-.}/redis/redis.conf:/etc/redis.conf
command: redis-server /etc/redis.conf
healthcheck:
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
test: ["CMD-SHELL", "redis-cli ping | grep -e 'PONG\|NOAUTH'"]
interval: 10s
timeout: 5s
retries: 3
Expand Down
2 changes: 1 addition & 1 deletion src/backend/bisheng/api/services/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_assistant(cls,
role_ids = [role.id for role in user_role]
role_access = RoleAccessDao.get_role_access(role_ids, AccessType.ASSISTANT_READ)
if role_access:
assistant_ids_extra = [access.third_id for access in role_access]
assistant_ids_extra = [UUID(access.third_id).hex for access in role_access]
res, total = AssistantDao.get_assistants(user.user_id, name, assistant_ids_extra, status, page, limit)

for one in res:
Expand Down
44 changes: 27 additions & 17 deletions src/backend/bisheng/api/v1/callback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json
from typing import Any, Dict, List, Union

from bisheng.api.v1.schemas import ChatResponse
Expand Down Expand Up @@ -354,15 +355,31 @@ def parse_tool_category(tool_name) -> (str, str):
tool_name = tool_name.replace('knowledge_', '')
return tool_name, tool_category

async def on_chat_model_start(self, serialized: Dict[str, Any],
messages: List[List[BaseMessage]], **kwargs: Any) -> Any:
# """Run when retriever end running."""
# content = messages[0][0] if isinstance(messages[0][0], str) else messages[0][0].get('content')
# stream = ChatResponse(message=f'{content}', type='stream')
# await self.websocket.send_json(stream.dict())
logger.debug(f'on_chat_model_start serialized={serialized} messages={messages} kwargs={kwargs}')
resp = ChatResponse(type='start',
category='processing',
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp.dict())

async def on_llm_end(self, response: LLMResult, **kwargs: Any) -> Any:
"""Run when LLM ends running."""
logger.debug(f'llm_end response={response}')
resp = ChatResponse(type='end',
category='processing',
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp.dict())

async def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwargs: Any) -> Any:
"""Run when tool starts running."""
logger.debug(f'on_tool_start serialized={serialized} input_str={input_str} kwargs={kwargs}')
resp_end = ChatResponse(type='end',
category='processing',
intermediate_steps='',
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp_end.dict())

tool_name, tool_category = self.parse_tool_category(serialized['name'])
resp = ChatResponse(type='start',
Expand All @@ -371,7 +388,7 @@ async def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwar
message={'tool_key': tool_name, 'serialized': serialized, 'input_str': input_str},
flow_id=self.flow_id,
chat_id=self.chat_id,
sender=kwargs.get('run_id').hex)
extra=json.dumps({'run_id': kwargs.get('run_id').hex}))
await self.websocket.send_json(resp.dict())

ChatMessageDao.insert_one(ChatMessageModel(
Expand All @@ -383,7 +400,7 @@ async def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwar
flow_id=self.flow_id,
chat_id=self.chat_id,
user_id=self.user_id,
sender=kwargs.get('run_id').hex
extra=json.dumps({'run_id': kwargs.get('run_id').hex})
))

async def on_tool_end(self, output: str, **kwargs: Any) -> Any:
Expand All @@ -404,7 +421,7 @@ async def on_tool_end(self, output: str, **kwargs: Any) -> Any:
message={'tool_key': tool_name, 'output': output},
flow_id=self.flow_id,
chat_id=self.chat_id,
sender=kwargs.get('run_id').hex)
extra=json.dumps({'run_id': kwargs.get('run_id').hex}))

await self.websocket.send_json(resp.dict())
ChatMessageDao.insert_one(ChatMessageModel(
Expand All @@ -416,12 +433,5 @@ async def on_tool_end(self, output: str, **kwargs: Any) -> Any:
flow_id=self.flow_id,
chat_id=self.chat_id,
user_id=self.user_id,
sender=kwargs.get('run_id').hex
extra=json.dumps({'run_id': kwargs.get('run_id').hex})
))

resp_start = ChatResponse(type='start',
category='processing',
intermediate_steps='',
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp_start.dict())
10 changes: 5 additions & 5 deletions src/backend/bisheng/api/v1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ async def access_refresh(*, data: RoleRefresh, Authorize: AuthJWT = Depends()):
session.commit()
# 添加新的权限
with session_getter() as session:
for id in access_id:
if access_type == AccessType.FLOW.value:
id = UUID(id).hex
role_access = RoleAccess(role_id=role_id, third_id=str(id), type=access_type)
for third_id in access_id:
if access_type in [AccessType.FLOW.value, AccessType.ASSISTANT_READ.value]:
third_id = UUID(third_id).hex
role_access = RoleAccess(role_id=role_id, third_id=str(third_id), type=access_type)
session.add(role_access)
session.commit()
return resp_200()
Expand All @@ -399,7 +399,7 @@ async def access_list(*, role_id: int, type: Optional[int] = None, Authorize: Au
total_count = session.scalar(count_sql)
# uuid 和str的转化
for access in db_role_access:
if access.type == AccessType.FLOW.value:
if access.type in [AccessType.FLOW.value, AccessType.ASSISTANT_READ.value]:
access.third_id = UUID(access.third_id)

return resp_200({
Expand Down
2 changes: 0 additions & 2 deletions src/backend/bisheng/chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ async def handle_gpts_message(self, message: Dict[any, any]):
await self.init_gpts_agent()

await self.send_response('processing', 'begin', '')
await self.send_response('processing', 'start', '')

# 将用户问题写入到数据库
await self.add_message('human', json.dumps(inputs, ensure_ascii=False), 'question')
Expand All @@ -191,7 +190,6 @@ async def handle_gpts_message(self, message: Dict[any, any]):

res = await self.add_message('bot', answer, 'answer')

await self.send_response('processing', 'end', '')
await self.send_response('answer', 'start', '')
await self.send_response('answer', 'end', answer, message_id=res.id if res else None)

Expand Down
2 changes: 1 addition & 1 deletion src/backend/bisheng/chat/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def add_message(
if chat_id and (message.message or message.intermediate_steps
or message.files) and message.type != 'stream':
msg = message.copy()
msg.message = str(msg.message) if isinstance(msg.message, dict) else msg.message
msg.message = json.dumps(msg.message) if isinstance(msg.message, dict) else msg.message
files = json.dumps(msg.files) if msg.files else ''
msg.__dict__.pop('files')
db_message = ChatMessage(files=files, **msg.__dict__)
Expand Down
2 changes: 2 additions & 0 deletions src/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ psycopg-binary = "^3.1.9"
emoji = "^2.10.1"
platformdirs = ">=2.5"
scipy = "*"
arxiv = "2.1.0"
matplotlib = "3.8.4"

[tool.poetry.dev-dependencies]
black = "^23.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetCurTimeInput(BaseModel):
@tool(args_schema=GetCurTimeInput)
def get_current_time(timezone='Asia/Shanghai'):
"""
Get the current UTC time and the time in the major time zones, which can be used for calculations related to time, date, and other scenarios.
获取当前UTC时间以及主要时区的时间,可用于时间、日期等场景相关的计算。当问题涉及到时间,调用此工具来查询和时间有关的内容。
"""
tz = pytz.timezone(timezone)
current_time = datetime.now(tz)
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/public/locales/zh/bs.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"skills": {
"manageTemplate": "管理技能模板",
"createNew": "新建技能",
"manageProjects": "这里管理您的个人项目,对技能上下线、编辑等等",
"manageProjects": "在此页面管理您的技能,对技能上下线、编辑等等",
"skillSearch": "搜索您需要的技能",
"confirmDeleteSkill": "确认删除该技能?",
"backToSkillList": "返回技能列表",
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import uniqueId from "lodash-es/uniqueId";
import cloneDeep from "lodash-es/cloneDeep";
import uniqueId from "lodash-es/uniqueId";
import { useContext, useEffect, useState } from "react";
import { RouterProvider, useSearchParams } from "react-router-dom";
import { RouterProvider } from "react-router-dom";
import "reactflow/dist/style.css";
import "./App.css";

import i18next from "i18next";
import { useTranslation } from "react-i18next";
import ErrorAlert from "./alerts/error";
import NoticeAlert from "./alerts/notice";
import SuccessAlert from "./alerts/success";
import { Toaster } from "./components/bs-ui/toast";
import { alertContext } from "./contexts/alertContext";
import { locationContext } from "./contexts/locationContext";
import { userContext } from "./contexts/userContext";
import { LoginPage } from "./pages/login";
import router from "./routes";
import { useTranslation } from "react-i18next";
import i18next from "i18next";
import { Toaster } from "./components/bs-ui/toast";

export default function App() {
let { setCurrent, setShowSideBar, setIsStackedOpen } = useContext(locationContext);
Expand Down
16 changes: 11 additions & 5 deletions src/frontend/src/components/bs-comp/cardComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AssistantIcon } from "@/components/bs-icons/assistant";
import { cname } from "@/components/bs-ui/utils";
import { useState } from "react";
import { AddToIcon } from "../../bs-icons/addTo";
import { DelIcon } from "../../bs-icons/del";
import { GoIcon } from "../../bs-icons/go";
import { PlusIcon } from "../../bs-icons/plus";
Expand All @@ -7,8 +10,6 @@ import { SkillIcon } from "../../bs-icons/skill";
import { UserIcon } from "../../bs-icons/user";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "../../bs-ui/card";
import { Switch } from "../../ui/switch";
import { AddToIcon } from "../../bs-icons/addTo";
import { cname } from "@/components/bs-ui/utils";

interface IProps<T> {
data: T,
Expand All @@ -22,6 +23,7 @@ interface IProps<T> {
user?: string,
isAdmin?: boolean,
footer?: React.ReactNode,
icon?: any,
onClick?: () => void,
onAddTemp?: (data: T) => void,
onCheckedChange?: (b: boolean, data: T) => Promise<any>
Expand Down Expand Up @@ -59,11 +61,11 @@ export function TitleIconBg({ id, className = '', children = <SkillIcon /> }) {
return <div className={cname(`rounded-sm flex justify-center items-center ${gradients[parseInt(id + '', 16) % gradients.length]}`, className)}>{children}</div>
}


export default function CardComponent<T>({
id = '',
data,
type,
icon: Icon = SkillIcon,
edit = false,
user,
title,
Expand Down Expand Up @@ -121,7 +123,9 @@ export default function CardComponent<T>({
<CardHeader className="pb-2">
<CardTitle className="truncate-doubleline">
<div className="flex gap-2 pb-2 items-center">
<TitleIconBg id={id}></TitleIconBg>
<TitleIconBg id={id}>
<Icon />
</TitleIconBg>
<p className=" align-middle">{title}</p>
</div>
{/* <span></span> */}
Expand All @@ -140,7 +144,9 @@ export default function CardComponent<T>({
return <Card className="group w-[320px] cursor-pointer" onClick={onClick}>
<CardHeader>
<div className="flex justify-between pb-2">
<TitleIconBg id={id}></TitleIconBg>
<TitleIconBg id={id} >
{type === 'skill' ? <SkillIcon /> : <AssistantIcon />}
</TitleIconBg>
{edit && <Switch checked={_checked} onCheckedChange={handleCheckedChange} onClick={e => e.stopPropagation()}></Switch>}
</div>
<CardTitle className="truncate-doubleline leading-5">{title}</CardTitle>
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/src/components/bs-comp/chatComponent/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default function ChatInput({ clear, form, questions, inputForm, wsUrl, on
}, [])

const handleSendClick = async () => {
// 解除锁定状态下 form 按钮开放的状态
setShowWhenLocked(false)
// 关闭引导词
setShowGuideQuestion(false)
// 收起表单
Expand All @@ -83,6 +85,12 @@ export default function ChatInput({ clear, form, questions, inputForm, wsUrl, on
const chatid = chatId
await createWebSocket(chatid)
sendWsMsg(wsMsg)

// 滚动聊天到底
const messageDom = document.getElementById('message-panne')
if (messageDom) {
messageDom.scrollTop = messageDom.scrollHeight;
}
}

const sendWsMsg = async (msg) => {
Expand Down Expand Up @@ -239,6 +247,7 @@ export default function ChatInput({ clear, form, questions, inputForm, wsUrl, on
><ClearIcon ></ClearIcon></div>
}
</div>
{/* form */}
<div className="flex absolute left-3 top-4 z-10">
{
form && <div
Expand All @@ -247,6 +256,7 @@ export default function ChatInput({ clear, form, questions, inputForm, wsUrl, on
><FormIcon className={!showWhenLocked && inputLock.locked ? 'text-gray-400' : 'text-gray-950'}></FormIcon></div>
}
</div>
{/* send */}
<div className="flex gap-2 absolute right-3 top-4 z-10">
<div
id="bs-send-btn"
Expand Down
17 changes: 8 additions & 9 deletions src/frontend/src/components/bs-comp/chatComponent/MessageBs.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { ThunmbIcon } from "@/components/bs-icons/thumbs";
import SourceEntry from "./SourceEntry";
import { AvatarIcon } from "@/components/bs-icons/avatar";
import { LoadIcon } from "@/components/bs-icons/loading";
import { ChatMessageType } from "@/types/chat";
import { CodeBlock } from "@/modals/formModal/chatMessage/codeBlock";
import { useMemo, useRef, useState } from "react";
import { ChatMessageType } from "@/types/chat";
import { copyText } from "@/utils";
import { useMemo, useRef } from "react";
import ReactMarkdown from "react-markdown";
import rehypeMathjax from "rehype-mathjax";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import { copyText } from "@/utils";
import MessageButtons from "./MessageButtons";
import SourceEntry from "./SourceEntry";
import { useMessageStore } from "./messageStore";
import { AvatarIcon } from "@/components/bs-icons/avatar";

// 颜色列表
const colorList = [
Expand All @@ -28,7 +27,7 @@ const colorList = [
"#95A5A6"
]

export default function MessageBs({ data, onUnlike, onSource }: { data: ChatMessageType, onUnlike: any, onSource: any }) {
export default function MessageBs({ data, onUnlike = () => { }, onSource }: { data: ChatMessageType, onUnlike?: any, onSource?: any }) {
const avatarColor = colorList[
(data.sender?.split('').reduce((num, s) => num + s.charCodeAt(), 0) || 0) % colorList.length
]
Expand All @@ -39,7 +38,7 @@ export default function MessageBs({ data, onUnlike, onSource }: { data: ChatMess
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeMathjax]}
linkTarget="_blank"
className="markdown prose inline-block break-all dark:prose-invert max-w-full text-sm text-[#111]"
className="bs-mkdown inline-block break-all max-w-full text-sm text-[#111]"
components={{
code: ({ node, inline, className, children, ...props }) => {
if (children.length) {
Expand Down Expand Up @@ -102,7 +101,7 @@ export default function MessageBs({ data, onUnlike, onSource }: { data: ChatMess
end={data.end}
source={data.source}
className="pl-8"
onSource={() => onSource({
onSource={() => onSource?.({
chatId,
messageId: data.id,
message: data.message || data.thought,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ export default function MessagePanne({ useName, guideWord, loadMore }) {
return () => messagesRef.current?.removeEventListener('scroll', handleScroll)
}, [messagesRef.current, messages, chatId]);

return <div ref={messagesRef} className="h-full overflow-y-auto scrollbar-hide pt-12 pb-52">
{guideWord && <MessageSystem key={99999} data={{ category: 'guide', thought: guideWord }} />}
return <div id="message-panne" ref={messagesRef} className="h-full overflow-y-auto scrollbar-hide pt-12 pb-52">
{guideWord && <MessageBs
key={9999}
data={{ message: guideWord, isSend: false, chatKey: '', end: true, user_name: '' }} />}
{
messages.map(msg => {
// 工厂
Expand Down
Loading

0 comments on commit 7230892

Please sign in to comment.