feat(ai): 完善AI对话
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"@ant-design/icons": "^6.0.0",
|
||||
"@ant-design/pro-components": "^2.8.7",
|
||||
"@ant-design/x": "^1.2.0",
|
||||
"@echofly/fetch-event-source": "^3.0.2",
|
||||
"@fortawesome/fontawesome-free": "^6.7.2",
|
||||
"@tinyflow-ai/react": "^0.1.6",
|
||||
"amis": "^6.12.0",
|
||||
|
||||
9
service-web/client/pnpm-lock.yaml
generated
9
service-web/client/pnpm-lock.yaml
generated
@@ -17,6 +17,9 @@ importers:
|
||||
'@ant-design/x':
|
||||
specifier: ^1.2.0
|
||||
version: 1.2.0(antd@5.25.0(moment@2.30.1)(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||
'@echofly/fetch-event-source':
|
||||
specifier: ^3.0.2
|
||||
version: 3.0.2
|
||||
'@fortawesome/fontawesome-free':
|
||||
specifier: ^6.7.2
|
||||
version: 6.7.2
|
||||
@@ -257,6 +260,10 @@ packages:
|
||||
peerDependencies:
|
||||
react: '>=16.8.0'
|
||||
|
||||
'@echofly/fetch-event-source@3.0.2':
|
||||
resolution: {integrity: sha512-woQtppGXKXGOPLgmHRoPyNflOXaE2o+0L7a/6jsrnj3y2YDltVbc0FfeuSugv7iijlI5tIHtJUcGbxf4zPFzxg==}
|
||||
engines: {node: '>=16.15'}
|
||||
|
||||
'@emotion/hash@0.8.0':
|
||||
resolution: {integrity: sha512-kBJtf7PH6aWwZ6fka3zQ0p6SBYzx4fl1LoZXE2RrnYST9Xljm7WfKJrU4g/Xr3Beg72MLrp1AWNUmuYJTL7Cow==}
|
||||
|
||||
@@ -3517,6 +3524,8 @@ snapshots:
|
||||
react: 18.3.1
|
||||
tslib: 2.8.1
|
||||
|
||||
'@echofly/fetch-event-source@3.0.2': {}
|
||||
|
||||
'@emotion/hash@0.8.0': {}
|
||||
|
||||
'@emotion/is-prop-valid@1.2.2':
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user