Queries the AI agent with a prompt and optional session messages. It returns a ChatBuilder object that can be used to generate text or stream responses.
Optionalmedia?: null | InlineDataOptional media data to include in the chat. This can be an inline data object containing file data and mime type.
The user's message to start the chat. This should be a non-empty string representing the user's input.
Optionalsession?: null | CoreMessage[]An optional array of previous messages in the chat session. This allows the AI agent to continue the conversation with the existing context.
A ChatBuilder object with methods to generate text or stream responses.
const { user, session, saveHistory } = await chatSession.useJSONFileSession({
folderName: "sessions",
sessionFileNameSuffix: username,
user: {
username: username,
phone: phone,
},
})
const chatBuilder = await agent.query({
session: session,
prompt: "Hi there!",
media: null
});
const textResponse = await chatBuilder.generateText();
const streamResponse = await chatBuilder.generateStream();
const objectResponse = await chatBuilder.generateObject(
z.object({
answer: z.string()
})
);
console.log(textResponse.text);
console.log(streamResponse.textStream);
console.log(objectResponse.object);
Starts a chat session with the AI agent. It initializes the conversation and returns the AI's response.
Optionalmedia?: null | InlineDataOptional media data to include in the chat. This can be an inline data object containing file data and mime type. If not provided, the chat will not include any media.
The user's message to start the chat. This should be a non-empty string representing the user's input. If the prompt is invalid or empty, an error will be thrown.
Optionalsession?: null | CoreMessage[]An optional array of previous messages in the chat session. This allows the AI agent to continue the conversation with the existing context. If not provided, a new chat session will be started.
OptionalstreamMethod?: "text" | "stream"The method to use for streaming responses. Can be "stream" for streaming responses or "text" for text responses. Default is "text".
A promise that resolves to the AI's response, which can be either a text response or a stream of text responses.
AiAgent class for interacting with an AI agent via OpenAI API. It initializes the agent with a URL and API key, and allows starting a chat session. This class provides methods to start a chat session, generate text responses, and handle various tasks.
Param: config
Configuration for the AI agent.
Example