feat(ai): 完善AI对话

This commit is contained in:
v-zhangjc9
2025-05-16 19:00:26 +08:00
parent 8fbc665abf
commit be976290b6
7 changed files with 116 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
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 {Button, Divider, Flex, Switch, Tooltip, Typography} from 'antd'
import markdownIt from 'markdown-it'
import {useRef, useState} from 'react'
@@ -30,7 +31,7 @@ const ConversationDiv = styled.div`
border-left: 3px solid;
padding-left: 5px;
margin-bottom: 10px;
//white-space: pre-line;
white-space: pre-line;
}
}
@@ -40,11 +41,6 @@ const ConversationDiv = styled.div`
padding-right: 30px;
}
`
const llmConfig = {
base: 'http://132.121.206.65:10086',
model: 'Qwen3-1.7',
secret: 'Bearer *XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4',
}
function Conversation() {
const abortController = useRef<AbortController | null>(null)
@@ -52,20 +48,34 @@ function Conversation() {
const [think, setThink] = useState<boolean>(true)
const [agent] = useXAgent<{ role: string, content: string }>({
baseURL: `${llmConfig.base}/v1/chat/completions`,
model: llmConfig.model,
dangerouslyApiKey: llmConfig.secret,
request: async (info, callbacks) => {
await fetchEventSource('http://127.0.0.1:8080/chat/async', {
method: 'POST',
headers: {
'Authorization': 'Basic QXhoRWJzY3dzSkRiWU1IMjpjWXhnM2I0UHRXb1ZENVNqRmF5V3h0blNWc2p6UnNnNA==',
'Content-Type': 'application/json',
},
body: JSON.stringify(info.messages),
signal: abortController.current?.signal,
onmessage: ev => {
console.log(ev)
callbacks.onUpdate({
id: ev.id,
event: 'delta',
data: ev.data,
})
},
onclose: () => callbacks.onSuccess([]),
})
},
})
const {onRequest, messages, setMessages} = useXChat({
agent,
transformMessage: ({originMessage, chunk}) => {
let text = ''
try {
if (chunk?.data && !chunk?.data.includes('DONE')) {
const message = JSON.parse(chunk?.data)
text = !message?.choices?.[0].delta?.content
? ''
: message?.choices?.[0].delta?.content
if (chunk?.data) {
text = chunk.data
}
} catch (error) {
console.error(error)
@@ -128,7 +138,6 @@ function Conversation() {
</div>)}
<div className="conversation-sender">
<Sender
loading={agent.isRequesting()}
value={input}
onChange={setInput}
onSubmit={message => {