useXChat
#1811
Replies: 1 comment 12 replies
-
|
import { useXChat, XRequest, AbstractChatProvider } from '@ant-design/x';
// 1. 创建自定义 Provider
class CustomProvider extends AbstractChatProvider {
transformParams(requestParams, options) {
return { ...(options?.params || {}), ...(requestParams || {}) };
}
transformLocalMessage(requestParams) {
return { content: requestParams.query || '', role: 'user' };
}
transformMessage(info) {
const { originMessage, chunk } = info || {};
const data = JSON.parse(chunk.data);
return {
content: `${originMessage?.content || ''}${data.content || ''}`,
role: 'assistant'
};
}
}
// 2. 配置自定义 API 端点
const provider = new CustomProvider({
request: XRequest('https://your-api.com/endpoint', {
manual: true,
params: { /* 你的 API 参数 */ },
}),
});
// 3. 使用 useXChat
const { onRequest, messages, isRequesting } = useXChat({
provider,
requestPlaceholder: { content: '思考中...', role: 'assistant' },
requestFallback: { content: '发生错误', role: 'assistant' },
});关键点:
完整示例可参考 custom-provider-width-ui.tsx。 To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
useXChat 如何使用自定义api
Beta Was this translation helpful? Give feedback.
All reactions