OpenAI
OpenAI
Typically, openai-node
is used in Node.js environments. If you need to use it in a browser environment, you must enable dangerouslyAllowBrowser
.
openai-node
import { useXAgent, useXChat, Sender } from '@ant-design/x';import OpenAI from 'openai';const client = new OpenAI({apiKey: process.env['OPENAI_API_KEY'],dangerouslyAllowBrowser: true,});// React environment setupconst [agent] = useXAgent({request: async (info, callbacks) => {const stream = await client.chat.completions.create({model: 'gpt-4o',messages: [{ role: 'user', content: 'Say this is a test' }],stream: true,});for await (const chunk of stream) {// Trigger the callbackcallbacks.onUpdate(chunk.choices[0]?.delta?.content || '');}},});const {// Used to initiate conversation requestsonRequest,// Used to bind the viewmessages,} = useXChat({ agent });const items = messages.map((message) => ({content: message,}));return (<div><Bubble.List items={items} /><Sender onSubmit={onRequest} /></div>);