Skip to content

Commit

Permalink
Feat/0.4.0.dev2 (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit authored Dec 9, 2024
2 parents 2d62fb1 024e6ff commit fd6b419
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 140 deletions.
18 changes: 15 additions & 3 deletions src/backend/bisheng/api/v1/mark_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 146,8 @@ async def mark(data: MarkData,

task = MarkTaskDao.get_task_byid(task_id=data.task_id)
msg_list = ChatMessageDao.get_msg_by_flows(task.app_id.split(","))
m_list = [msg.chat_id for msg in msg_list]
#m_list = [msg.chat_id for msg in msg_list]
m_list = msg_list
r_list = MarkRecordDao.get_list_by_taskid(data.task_id)
app_record = [r.session_id for r in r_list ]

Expand Down Expand Up @@ -191,8 192,8 @@ async def pre_or_next(chat_id:str,action:str,task_id:int,login_user: UserPayload
if record:
queue = deque()
for r in record:
if r.session_id == chat_id:
break
# if r.session_id == chat_id:
# break
queue.append(r)

if len(queue) == 0:
Expand Down Expand Up @@ -235,6 236,17 @@ async def pre_or_next(chat_id:str,action:str,task_id:int,login_user: UserPayload
result["chat_id"] = cur.chat_id
result["flow_id"] = cur.flow_id
return resp_200(data=result)
else:
cur = k_list[linked.head().data]
flow = FlowDao.get_flow_by_idstr(cur.flow_id)
if flow:
result['flow_type'] = 'flow'
else:
result['flow_type'] = 'assistant'
result["chat_id"] = cur.chat_id
result["flow_id"] = cur.flow_id
return resp_200(data=result)


return resp_200()

Expand Down
3 changes: 2 additions & 1 deletion src/backend/bisheng/api/v1/skillcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@
from sqlmodel import select

from bisheng.api.services.user_service import get_login_user
from bisheng.api.errcode.flow import FlowTemplateNameError
from bisheng.api.utils import remove_api_keys
from bisheng.api.v1.schemas import UnifiedResponseModel, resp_200
from bisheng.database.base import session_getter
Expand Down Expand Up @@ -31,7 32,7 @@ def create_template(*, template: TemplateCreate):
name_repeat = session.exec(
select(Template).where(Template.name == db_template.name)).first()
if name_repeat:
raise HTTPException(status_code=500, detail='Repeat name, please choose another name')
raise FlowTemplateNameError.http_exception()
# 增加 order_num x,x 65535
with session_getter() as session:
max_order = session.exec(select(Template).order_by(
Expand Down
36 changes: 1 addition & 35 deletions src/backend/bisheng/api/v1/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 3,10 @@
from typing import Optional
from uuid import UUID

from bisheng.api.v1.skillcenter import ORDER_GAP
from bisheng.database.models.template import Template, TemplateCreate, TemplateRead
from sqlmodel import select
from bisheng.api.errcode.base import UnAuthorizedError
from bisheng.api.errcode.flow import FlowOnlineEditError, FlowTemplateNameError
from bisheng.api.errcode.flow import FlowOnlineEditError
from bisheng.api.services.workflow import WorkFlowService
from bisheng.api.utils import get_L2_param_from_flow
from bisheng.database.base import session_getter
from bisheng.database.models.flow import Flow, FlowCreate, FlowDao, FlowRead, FlowReadWithStyle, FlowType, FlowUpdate
from bisheng.database.models.flow_version import FlowVersionDao
Expand Down Expand Up @@ -270,34 267,3 @@ def read_flows(*,
except Exception as e:
logger.exception(e)
raise HTTPException(status_code=500, detail=str(e)) from e


@router.post('/template/create',
response_model=UnifiedResponseModel[TemplateRead],
status_code=201)
def create_template(*, template: TemplateCreate):
"""Create a new workflow or assitant."""
template.flow_type = FlowType.WORKFLOW.value
db_template = Template.model_validate(template)
# TODO: if assitant need more data
if not db_template.data:
with session_getter() as session:
db_flow = session.get(Flow, template.flow_id)
db_template.data = db_flow.data
# 校验name
with session_getter() as session:
name_repeat = session.exec(
select(Template).where(Template.name == db_template.name)).first()
if name_repeat:
raise FlowTemplateNameError.http_exception()
# 增加 order_num x,x 65535
with session_getter() as session:
max_order = session.exec(select(Template).order_by(
Template.order_num.desc()).limit(1)).first()
# 如果没有数据,就从 65535 开始
db_template.order_num = max_order.order_num ORDER_GAP if max_order else ORDER_GAP
with session_getter() as session:
session.add(db_template)
session.commit()
session.refresh(db_template)
return resp_200(db_template)
7 changes: 3 additions & 4 deletions src/backend/bisheng/database/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 177,7 @@ def get_messages_by_chat_id(cls, chat_id: str, category_list: list = None, limit
@classmethod
def get_last_msg_by_flow_id(cls, flow_id: List[str], chat_id: List[str]):
with session_getter() as session:
statement = select(ChatMessage).where(ChatMessage.flow_id.in_(flow_id)).where(
not_(ChatMessage.chat_id.in_(chat_id))).group_by(ChatMessage.chat_id).order_by(
statement = select(ChatMessage).where(ChatMessage.flow_id.in_(flow_id)).where(not_(ChatMessage.chat_id.in_(chat_id))).group_by(ChatMessage.chat_id).order_by(
ChatMessage.create_time)
return session.exec(statement).all()

Expand All @@ -192,15 191,15 @@ def get_msg_by_chat_id(cls, chat_id: str):
def get_msg_by_flow(cls, flow_id: str):
with session_getter() as session:
# sql = text("select chat_id,count(*) as chat_count from chatmessage where flow_id=:flow_id group by chat_id")
st = select(ChatMessage).where(ChatMessage.flow_id == flow_id).group_by(ChatMessage.chat_id)
st = select(ChatMessage.chat_id).where(ChatMessage.flow_id == flow_id).group_by(ChatMessage.chat_id)
return session.exec(st).all()

@classmethod
def get_msg_by_flows(cls, flow_id: List[str]):
ids = [UUID(i) for i in flow_id]
with session_getter() as session:
# sql = text("select chat_id,count(*) as chat_count from chatmessage where flow_id=:flow_id group by chat_id")
st = select(ChatMessage).where(ChatMessage.flow_id.in_(ids)).group_by(ChatMessage.chat_id)
st = select(ChatMessage.chat_id).where(ChatMessage.flow_id.in_(ids)).group_by(ChatMessage.chat_id)
return session.exec(st).all()

@classmethod
Expand Down
1 change: 1 addition & 0 deletions src/backend/bisheng/interface/initialize/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 255,7 @@ def instantiate_llm(node_type, class_object, params: Dict, user_llm_request: boo
if is_openai_v1() and params.get('openai_proxy'):
params['http_client'] = httpx.Client(proxies=params.get('openai_proxy'))
params['http_async_client'] = httpx.AsyncClient(proxies=params.get('openai_proxy'))
del params['openai_proxy']

if node_type == '':
anthropic_api_key = params.pop('anthropic_api_key', None)
Expand Down
6 changes: 4 additions & 2 deletions src/backend/bisheng/utils/minio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 138,14 @@ def object_exists(self, bucket_name, object_name, **kwargs):
raise e

def get_object(self, bucket_name, object_name, **kwargs) -> bytes:
response = None
try:
response = self.minio_client.get_object(bucket_name, object_name, **kwargs)
return response.read()
finally:
response.close()
response.release_conn()
if response:
response.close()
response.release_conn()

def copy_object(
self,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/bisheng/workflow/nodes/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,8 @@ def __init__(self, *args, **kwargs):

def _run(self, unique_id: str):
# 下载报告模板文件
if not self._minio_client.object_exists(self._minio_client.bucket, self._object_name):
raise Exception(f"{self.name}节点模板文件不存在,请先编辑对应的报告模板")
file_content = self._minio_client.get_object(self._minio_client.bucket, self._object_name)
doc_parse = DocxTemplateRender(file_content=io.BytesIO(file_content))
# 获取所有的节点变量
Expand Down
1 change: 1 addition & 0 deletions src/frontend/public/locales/en/bs.json
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 830,7 @@
"10520": "Skill does not exist",
"10521": "Skill is online, cannot edit",
"10525": "Workflow is online and cannot be edited",
"10530": "Template name already exists",
"10900": "Knowledge base name already exists",
"10901": "Knowledge base must select an embedding model",
"10910": "Current knowledge base version does not support segment modification. Please create a new knowledge base to modify segments.",
Expand Down
1 change: 1 addition & 0 deletions src/frontend/public/locales/zh/bs.json
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 827,7 @@
"10520": "技能不存在",
"10521": "技能已上线,不可编辑",
"10525": "工作流已上线,不可编辑",
"10530": "模板名称已存在",
"10900": "知识库名称不可重复",
"10901": "知识库必须选择一个embedding模型",
"10910": "当前知识库版本不支持修改分段,请创建新知识库后进行分段修改",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/bs-comp/apiComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 27,7 @@ const ApiMainPage = ({ type = API_TYPE.ASSISTANT }) => {
case 'no-login-link':
return <ChatLink noLogin type={type} />;
case 'login-link':
// return <ChatLink type={type} />;
return <ChatLink type={type} />;
default:
return <ApiAccess />;
}
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/src/components/bs-icons/rbDrag/Drag.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/frontend/src/components/bs-icons/rbDrag/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
import React, { forwardRef } from "react";
import Drag from "./Drag.svg?react";

export const RbDragIcon = forwardRef<
SVGSVGElement & { className: any },
React.PropsWithChildren<{ className?: string }>
>(({ className, ...props }, ref) => {
return <Drag ref={ref} {...props} className={className || ''} />;
});
11 changes: 5 additions & 6 deletions src/frontend/src/components/inputFileComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,8 @@ import { TabsContext } from "../../contexts/tabsContext";
import { uploadFile } from "../../controllers/API";
import { uploadFileWithProgress } from "../../modals/UploadModal/upload";
import { FileComponentType } from "../../types/components";
import { LoadIcon } from "../bs-icons/loading";
import { Button } from "../bs-ui/button";

export default function InputFileComponent({
value,
Expand Down Expand Up @@ -115,20 117,17 @@ export default function InputFileComponent({
>
{myValue !== "" ? myValue : placeholder}
</span>
<button onClick={handleButtonClick}>
<Button size="icon" variant="ghost" onClick={handleButtonClick}>
{!editNode && !loading && (
<FileSearch2
strokeWidth={1.5}
className={
"icons-parameters-comp"
(disabled ? " text-ring " : " hover:text-accent-foreground")
}
/>
)}
{!editNode && loading && (
<span className="loading loading-spinner loading-sm pointer-events-none h-8 pl-3"></span>
)}
</button>
{!editNode && loading && (<LoadIcon className="text-primary duration-300 pointer-events-none" />)}
</Button>
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/controllers/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 85,7 @@ export async function readTempsDatabase(type, id?: number): Promise<FlowType[]>
}

/**
* 创建模板.
* 创建模板.(工作流\助手\技能)
*
* @param data {flow_id name description}
* @returns null.
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/controllers/API/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 336,7 @@ const workflowTemplate = [
{
"key": "model_id",
"label": "模型",
"type": "bisheng_model",
"type": "agent_model",
"required": true,
"value": "",
"placeholder": "请选择模型"
Expand Down Expand Up @@ -401,7 401,7 @@ const workflowTemplate = [
"type": "knowledge_select_multi",
"placeholder": "请选择知识库",
"value": {
"tab": "knowledge",
"type": "knowledge",
"value": []
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/frontend/src/pages/BuildPage/apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@ import AppTempSheet from "@/components/bs-comp/sheets/AppTempSheet";
import { LoadingIcon } from "@/components/bs-icons/loading";
import { MoveOneIcon } from "@/components/bs-icons/moveOne";
import { bsConfirm } from "@/components/bs-ui/alertDialog/useConfirm";
import { Badge } from "@/components/bs-ui/badge";
import { Button } from "@/components/bs-ui/button";
import { SearchInput } from "@/components/bs-ui/input";
import AutoPagination from "@/components/bs-ui/pagination/autoPagination";
Expand All @@ -12,7 13,7 @@ import SelectSearch from "@/components/bs-ui/select/select";
import { useToast } from "@/components/bs-ui/toast/use-toast";
import { userContext } from "@/contexts/userContext";
import { readTempsDatabase } from "@/controllers/API";
import { changeAssistantStatusApi } from "@/controllers/API/assistant";
import { changeAssistantStatusApi, deleteAssistantApi } from "@/controllers/API/assistant";
import { deleteFlowFromDatabase, getAppsApi, saveFlowToDatabase, updataOnlineState } from "@/controllers/API/flow";
import { onlineWorkflow } from "@/controllers/API/workflow";
import { captureAndAlertRequestErrorHoc } from "@/controllers/request";
Expand All @@ -27,7 28,6 @@ import { useQueryLabels } from "./assistant";
import CreateApp from "./CreateApp";
import CardSelectVersion from "./skills/CardSelectVersion";
import CreateTemp from "./skills/CreateTemp";
import { Badge } from "@/components/bs-ui/badge";

export const SelectType = ({ all = false, defaultValue = 'all', onChange }) => {
const [value, setValue] = useState<string>(defaultValue)
Expand Down Expand Up @@ -113,7 113,8 @@ export default function apps() {
desc: descMap[data.flow_type],
okTxt: t('delete'),
onOk(next) {
captureAndAlertRequestErrorHoc(deleteFlowFromDatabase(data.id).then(reload));
const promise = data.flow_type == 5 ? deleteAssistantApi(data.id) : deleteFlowFromDatabase(data.id)
captureAndAlertRequestErrorHoc(promise.then(reload));
next()
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,7 @@ import {
import { Button } from "@/components/bs-ui/button";
import { Input, InputList, Textarea } from "@/components/bs-ui/input";
import {
QuestionTooltip,
Tooltip,
TooltipContent,
TooltipProvider,
Expand Down Expand Up @@ -73,16 74,7 @@ export default function Setting() {
<div className="mb-4 px-6">
<label htmlFor="slider" className="bisheng-label flex gap-1">
{t("build.maxToken")}
<TooltipProvider delayDuration={200}>
<Tooltip>
<TooltipTrigger asChild>
<QuestionMarkCircledIcon />
</TooltipTrigger>
<TooltipContent>
<p>{t("build.maxTokenTip")}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<QuestionTooltip content={t("build.maxTokenTip")}></QuestionTooltip>
</label>
<Input value={assistantState.max_token} type="number" className="mt-2" defaultValue={32000} min={0} onChange={e =>
dispatchAssistant("setting", { max_token: Number(e.target.value) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 119,6 @@ export default function ChatMessages({ mark = false, logo, useName, guideWord, l
return <MessageBsChoose type='input' key={msg.message_id} data={msg} logo={logo} />;
case 'node_run':
return <MessageNodeRun key={msg.message_id} data={msg} />;
// case 'file':
// return <FileBs key={msg.id} data={msg} />;
// case 'runLog':
// return <RunLog key={msg.id} data={msg} />;
default:
return <div className="text-sm mt-2 border rounded-md p-2" key={msg.message_id}>Unknown message type</div>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 54,7 @@ const handleHistoryMsg = (data: any[]): ChatMessageType[] => {
.replace(/\t/g, '\\t') // 转义制表符
.replace(/'/g, '"'); // 将单引号替换为双引号

return data.filter(item => ['answer', 'question', 'processing', 'system', 'report', 'tool', 'knowledge', 'divider', 'flow'].includes(item.category)).map(item => {
return data.filter(item => ["question", "output_input_msg", "output_choose_msg", "stream_msg", "output_msg", "guide_question", "guide_word", "user_input", "node_run"].includes(item.category)).map(item => {
let { message, files, is_bot, intermediate_steps, category, ...other } = item
try {
message = message && message[0] === '{' ? JSON.parse(message) : message || ''
Expand Down Expand Up @@ -170,6 170,7 @@ export const useMessageStore = create<State & Actions>((set, get) => ({
})
},
async loadHistoryMsg(flowid, chatId, { lastMsg }) {
console.log('1234 :>> ', 1234);
const res = await getChatHistory(flowid, chatId, 30, 0)
const msgs = handleHistoryMsg(res)
const hisMessages = msgs.reverse()
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/pages/BuildPage/flow/FlowNode/Parameter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 67,8 @@ export default function Parameter({ nodeId, item, onOutPutChange, onStatusChange
return <OutputItem nodeId={nodeId} data={item} onChange={handleOnNewValue} onValidate={bindValidate} />
case 'bisheng_model':
return <ModelItem data={item} onChange={handleOnNewValue} onValidate={bindValidate} />
case 'agent_model':
return <ModelItem agent data={item} onChange={handleOnNewValue} onValidate={bindValidate} />
case 'slide':
return <SliderItem data={item} onChange={handleOnNewValue} />
case 'slide_switch':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 26,8 @@ type KnowledgeTypeValues = `${KnowledgeType}`;
export default function KnowledgeSelectItem({ data, onChange, onValidate }) {
const { flow } = useFlowStore()

const currentTabRef = useRef(data.value.tab)
const [tabType, setTabType] = useState<KnowledgeTypeValues>(data.value.tab)
const currentTabRef = useRef(data.value.type)
const [tabType, setTabType] = useState<KnowledgeTypeValues>(data.value.type)
const [value, setValue] = useState<any>(() => data.value.value.map(el => {
return { label: el.label, value: el.key }
}))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 1,13 @@
import { Label } from "@/components/bs-ui/label";
import Cascader from "@/components/bs-ui/select/cascader";
import { getModelListApi } from "@/controllers/API/finetune";
import { getAssistantModelList, getModelListApi } from "@/controllers/API/finetune";
import { useEffect, useMemo, useState } from "react";

export default function ModelItem({ data, onChange, onValidate }) {
export default function ModelItem({ agent = false, data, onChange, onValidate }) {
const [options, setOptions] = useState<any[]>([])

useEffect(() => {
getModelListApi().then(res => {
(agent ? getAssistantModelList() : getModelListApi()).then(res => {
let llmOptions = []
let embeddings = []
res.forEach(server => {
Expand Down
Loading

0 comments on commit fd6b419

Please sign in to comment.