#Hooks
#使用示例
import { useChat } from '@/react';
import { MujianProvider } from '@/react';
function Chat() {
const { messages, append, regenerate, continueGenerate, stop } = useChat({
body: {
model: 'gpt-4o',
},
});
return <div>
{messages.map((message) => (
<div key={message.id}>{message.content}</div>
))}
<button onClick={() => append('Hello')}>Append</button>
</div>;
}
function App() {
return (
<MujianProvider loadingComponent={<div>Loading...</div>}>
<Chat />
</MujianProvider>
);
}
export default App;#参数
| 参数 | 类型 | 描述 | 必填 | 默认值 |
|---|---|---|---|---|
| body | object | 请求体 | 否 | - |
| onError | function | 错误回调 | 否 | - |
| onFinish | function | 完成回调 | 否 | - |
| pageSize | number | 分页大小 | 否 | - |
#返回值
| 返回值 | 类型 | 描述 |
|---|---|---|
| messages | Message[] | 消息列表 |
| status | string | 状态 |
| error | Error | 错误 |
| append | function | 添加消息 |
| regenerate | function | 重新生成最后一条回答 |
| continueGenerate | function | 让AI继续说 |
| stop | function | 停止对话 |
| setMessages | function | 设置消息 |
| input | string | 用户输入 |
| setInput | function | 设置输入 |
| handleSubmit | function | 提交用户输入,让AI回答 |
| setSwipe | function | 设置Swipe |
| editMessage | function | 编辑消息 |
| deleteMessage | function | 删除消息 |
| loadMoreMessage | function | 加载更多消息 |
| messagesStatus | string | 消息状态 |