refactor(ai): 迁移chat到知识库中

This commit is contained in:
2025-06-15 19:59:35 +08:00
parent e3f86e6497
commit 9c658afbd7
21 changed files with 87 additions and 601 deletions

View File

@@ -1,9 +1,8 @@
import {ClearOutlined, FileOutlined, UserOutlined} from '@ant-design/icons'
import {ClearOutlined, UserOutlined} from '@ant-design/icons'
import {Bubble, Sender, useXAgent, useXChat, Welcome} from '@ant-design/x'
import {fetchEventSource} from '@echofly/fetch-event-source'
import {useMount} from 'ahooks'
import {Button, Collapse, Flex, Popover, Radio, Typography} from 'antd'
import {isEqual, isStrBlank, trim} from 'licia'
import {Button, Collapse, Flex, Typography} from 'antd'
import {isStrBlank, trim} from 'licia'
import {useRef, useState} from 'react'
import styled from 'styled-components'
import {commonInfo} from '../../util/amis.tsx'
@@ -40,24 +39,10 @@ type ChatMessage = { role: string, content?: string, reason?: string }
function Conversation() {
const abortController = useRef<AbortController | null>(null)
const [input, setInput] = useState<string>('')
const [knowledge, setKnowledge] = useState<string>('0')
const [knowledgeList, setKnowledgeList] = useState<{ id: string, name: string }[]>([])
useMount(async () => {
let response = await fetch(`${commonInfo.baseAiKnowledgeUrl}/knowledge/list`, {
headers: commonInfo.authorizationHeaders,
})
let items = (await response.json()).data.items
setKnowledgeList(items.map((item: { id: string, name: string }) => ({id: item.id, name: item.name})))
})
const [agent] = useXAgent<ChatMessage>({
request: async (info, callbacks) => {
let requestUrl = `${commonInfo.baseAiChatUrl}/chat/async`
if (!isEqual('0', info.knowledge)) {
requestUrl = `${requestUrl}?knowledge_id=${info.knowledge}`
}
await fetchEventSource(requestUrl, {
await fetchEventSource(`${commonInfo.baseAiChatUrl}/chat/async`, {
method: 'POST',
headers: commonInfo.authorizationHeaders,
body: JSON.stringify(info.messages),
@@ -180,7 +165,6 @@ function Conversation() {
content: message,
},
stream: true,
knowledge: knowledge,
})
setInput('')
}}
@@ -190,32 +174,6 @@ function Conversation() {
return (
<Flex justify="space-between" align="center">
<Flex gap="small" align="center">
<Popover
title="选择知识库"
trigger="hover"
content={<Radio.Group
style={{
display: 'flex',
flexDirection: 'column',
gap: 10,
}}
disabled={agent.isRequesting()}
value={knowledge}
onChange={event => setKnowledge(event.target.value)}
options={[
{value: '0', label: '无'},
...knowledgeList.map(k => ({label: k.name, value: k.id})),
]}
/>}
>
<Button
icon={<FileOutlined/>}
type="text"
size="small"
>
</Button>
</Popover>
<Button
icon={<ClearOutlined/>}
type="text"

View File

@@ -10,8 +10,9 @@ import {isEqual} from 'licia'
export const commonInfo = {
debug: isEqual(import.meta.env.MODE, 'development'),
baseUrl: 'http://132.126.207.130:35690/hudi_services/service_web',
baseAiChatUrl: 'http://132.126.207.130:35690/hudi_services/ai_chat',
baseAiKnowledgeUrl: 'http://132.126.207.130:35690/hudi_services/ai_knowledge',
baseAiChatUrl: 'http://localhost:8080',
// baseAiKnowledgeUrl: 'http://132.126.207.130:35690/hudi_services/ai_knowledge',
baseAiKnowledgeUrl: 'http://localhost:8080',
// baseUrl: '/hudi_services/service_web',
authorizationHeaders: {
'Authorization': 'Basic QXhoRWJzY3dzSkRiWU1IMjpjWXhnM2I0UHRXb1ZENVNqRmF5V3h0blNWc2p6UnNnNA==',