fix: 优化代码
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled
Typos Check / Spell Check with Typos (push) Has been cancelled

This commit is contained in:
shaohuzhang1 2024-06-28 12:15:48 +08:00
parent fe58586be0
commit 4dc28666d4
5 changed files with 138 additions and 112 deletions

View File

@ -235,15 +235,21 @@ class WorkflowManage:
for node in self.node_context:
properties = node.node.properties
fields = properties.get('fields')
if fields is not None:
for field in fields:
prompt = prompt.replace(field.get('globeLabel'), field.get('globeValue'))
global_fields = properties.get('globalFields')
if global_fields is not None:
for field in global_fields:
prompt = prompt.replace(field.get('globeLabel'), field.get('globeValue'))
context[node.id] = node.context
node_config = properties.get('config')
if node_config is not None:
fields = node_config.get('fields')
if fields is not None:
for field in fields:
globeLabel = f"{properties.get('stepName')}.{field.get('value')}"
globeValue = f"context['{node.id}'].{field.get('value')}"
prompt = prompt.replace(globeLabel, globeValue)
global_fields = node_config.get('globalFields')
if global_fields is not None:
for field in global_fields:
globeLabel = f"全局变量.{field.get('value')}"
globeValue = f"context['global'].{field.get('value')}"
prompt = prompt.replace(globeLabel, globeValue)
context[node.id] = node.context
prompt_template = PromptTemplate.from_template(prompt, template_format='jinja2')
value = prompt_template.format(context=context)

View File

@ -53,14 +53,14 @@ function _getIncomingNode(id: String, startId: String) {
value: item.id,
label: item.properties.stepName,
type: item.type,
children: item.properties?.fields || []
children: item.properties?.config?.fields || []
})
if (item.properties?.globalFields && item.type === 'start-node') {
firstElement = {
value: 'global',
label: '全局变量',
type: 'global',
children: item.properties?.globalFields || []
children: item.properties?.config?.globalFields || []
}
}
}

View File

@ -41,11 +41,11 @@
</div>
</div>
<div>
<div @mousemove.stop @mousedown.stop @keydown.stop @click.stop>
<slot></slot>
<template v-if="nodeModel.properties.fields?.length > 0">
<template v-if="nodeFields.length > 0">
<h5 class="title-decoration-1 mb-8 mt-8">参数输出</h5>
<template v-for="(item, index) in nodeModel.properties.fields" :key="index">
<template v-for="(item, index) in nodeFields" :key="index">
<div
class="flex-between border-r-4 p-8-12 mb-8 layout-bg lighter"
@mouseenter="showicon = index"
@ -136,23 +136,21 @@ const resizeStepContainer = (wh: any) => {
const props = defineProps<{
nodeModel: any
}>()
watch(
() => props.nodeModel.properties?.stepName,
() => {
if (props.nodeModel.properties.fields) {
const fields = props.nodeModel.properties.fields?.map((field: any) => {
return {
label: field.label,
value: field.value,
globeLabel: `{{${props.nodeModel.properties.stepName}.${field.value}}}`,
globeValue: `{{context['${props.nodeModel.id}'].${field.value}}}`
}
})
set(props.nodeModel.properties, 'fields', fields)
}
},
{ immediate: true }
)
const nodeFields = computed(() => {
if (props.nodeModel.properties.config.fields) {
const fields = props.nodeModel.properties.config.fields?.map((field: any) => {
return {
label: field.label,
value: field.value,
globeLabel: `{{${props.nodeModel.properties.stepName}.${field.value}}}`,
globeValue: `{{context['${props.nodeModel.id}'].${field.value}}}`
}
})
return fields
}
return []
})
function showOperate(type: string) {
return type !== WorkflowType.Base && type !== WorkflowType.Start
}

View File

@ -9,7 +9,7 @@ import { createApp, h } from 'vue'
import directives from '@/directives'
import i18n from '@/locales'
import { WorkflowType } from '@/enums/workflow'
import { nodeDict } from '@/workflow/common/data'
class AppNode extends HtmlResize.view {
isMounted
r
@ -43,12 +43,7 @@ class AppNode extends HtmlResize.view {
props.model.properties.stepName = props.model.properties.stepName + (filterNodes.length - 1)
}
}
if (props.model.properties?.fields?.length > 0) {
props.model.properties.fields.map((item: any) => {
item['globeLabel'] = `{{${props.model.properties.stepName}.${item.value}}}`
item['globeValue'] = `{{context['${props.model.id}'].${item.value}}}`
})
}
props.model.properties.config = nodeDict[props.model.type].properties.config
}
getAnchorShape(anchorData: any) {

View File

@ -1,61 +1,61 @@
import { WorkflowType } from '@/enums/workflow'
/**
*
* type nodes
*/
export const baseNodes = [
{
id: WorkflowType.Base,
type: WorkflowType.Base,
x: 200,
y: 270,
properties: {
height: 200,
stepName: '基本信息',
node_data: {
name: '',
desc: '',
prologue:
'您好,我是 MaxKB 小助手,您可以向我提出 MaxKB 使用问题。\n- MaxKB 主要功能有什么?\n- MaxKB 支持哪些大语言模型?\n- MaxKB 支持哪些文档类型?'
}
}
},
{
id: WorkflowType.Start,
type: WorkflowType.Start,
x: 180,
y: 720,
properties: {
height: 200,
stepName: '开始',
export const startNode = {
id: WorkflowType.Start,
type: WorkflowType.Start,
x: 180,
y: 720,
properties: {
height: 200,
stepName: '开始',
config: {
fields: [
{
label: '用户问题',
value: 'question',
globeLabel: '{{开始.question}}',
globeValue: `{{context['${WorkflowType.Start}'].question}}`
value: 'question'
}
],
globalFields: [
{
value: 'time',
label: '当前时间',
globeLabel: '{{全局变量.time}}',
globeValue: "{{context['global'].time}}"
label: '当前时间'
}
]
}
}
]
export const menuNodes = [
{
type: WorkflowType.AiChat,
text: '与 AI 大模型进行对话',
label: 'AI 对话',
properties: {
stepName: 'AI 对话',
}
export const baseNode = {
id: WorkflowType.Base,
type: WorkflowType.Base,
x: 200,
y: 270,
properties: {
height: 200,
stepName: '基本信息',
node_data: {
name: '',
desc: '',
prologue:
'您好,我是 MaxKB 小助手,您可以向我提出 MaxKB 使用问题。\n- MaxKB 主要功能有什么?\n- MaxKB 支持哪些大语言模型?\n- MaxKB 支持哪些文档类型?'
},
config: {}
}
}
/**
*
* type nodes
*/
export const baseNodes = [baseNode, startNode]
/**
* ai对话节点配置数据
*/
export const aiChatNode = {
type: WorkflowType.AiChat,
text: '与 AI 大模型进行对话',
label: 'AI 对话',
properties: {
stepName: 'AI 对话',
config: {
fields: [
{
label: 'AI 回答内容',
@ -63,13 +63,18 @@ export const menuNodes = [
}
]
}
},
{
type: WorkflowType.SearchDataset,
text: '关联知识库,查找与问题相关的分段',
label: '知识库检索',
properties: {
stepName: '知识库检索',
}
}
/**
*
*/
export const searchDatasetNode = {
type: WorkflowType.SearchDataset,
text: '关联知识库,查找与问题相关的分段',
label: '知识库检索',
properties: {
stepName: '知识库检索',
config: {
fields: [
{ label: '段落列表', value: 'paragraph_list' },
{ label: '满足直接回答的段落列表', value: 'is_hit_handling_method_list' },
@ -83,13 +88,15 @@ export const menuNodes = [
}
]
}
},
{
type: WorkflowType.Question,
text: '根据历史聊天记录优化完善当前问题,更利于匹配知识库分段',
label: '问题优化',
properties: {
stepName: '问题优化',
}
}
export const questionNode = {
type: WorkflowType.Question,
text: '根据历史聊天记录优化完善当前问题,更利于匹配知识库分段',
label: '问题优化',
properties: {
stepName: '问题优化',
config: {
fields: [
{
label: '问题优化结果',
@ -97,22 +104,32 @@ export const menuNodes = [
}
]
}
},
{
type: WorkflowType.Condition,
text: '根据不同条件执行不同的节点',
label: '判断器',
properties: {
width: 600,
stepName: '判断器'
}
}
export const conditionNode = {
type: WorkflowType.Condition,
text: '根据不同条件执行不同的节点',
label: '判断器',
properties: {
width: 600,
stepName: '判断器',
config: {
fields: [
{
label: '分支名称',
value: 'branch_name'
}
]
}
},
{
type: WorkflowType.Reply,
text: '指定回复内容,引用变量会转换为字符串进行输出',
label: '指定回复',
properties: {
stepName: '指定回复',
}
}
export const replyNode = {
type: WorkflowType.Reply,
text: '指定回复内容,引用变量会转换为字符串进行输出',
label: '指定回复',
properties: {
stepName: '指定回复',
config: {
fields: [
{
label: '内容',
@ -121,7 +138,8 @@ export const menuNodes = [
]
}
}
]
}
export const menuNodes = [aiChatNode, searchDatasetNode, questionNode, conditionNode, replyNode]
export const compareList = [
{ value: 'is_null', label: '为空' },
@ -140,6 +158,15 @@ export const compareList = [
{ value: 'lt', label: '小于' }
]
export const nodeDict: any = {
[WorkflowType.AiChat]: aiChatNode,
[WorkflowType.SearchDataset]: searchDatasetNode,
[WorkflowType.Question]: questionNode,
[WorkflowType.Condition]: conditionNode,
[WorkflowType.Base]: baseNode,
[WorkflowType.Start]: startNode,
[WorkflowType.Reply]: replyNode
}
export function isWorkFlow(type: string | undefined) {
return type === 'WORK_FLOW'
}