$ai
Updated on Aug 19, 2025 3 minutes to readThe AI Plugin provides an API for interacting with AI assistants, creating conversation threads, sending messages, and retrieving AI-generated responses.
Methods
Method | Description |
---|---|
generateText | Calls the API and returns a text completion response. |
Methods Details
generateText()
• Type
(prompt: string | AiPrompt) => Promise<AiChatCompletion>
• Details
Expects a string or an AiPrompt object that contains the user input or conversation history.
Returns a AiChatCompletion object containing the AI-generated message(s), which can be accessed via choices[0].message.content for displaying in chat interfaces, logging, or further processing.
• Example
// Example function using the AI plugin
async function askAI(promptText) {
try {
// Prepare a simple prompt as a user message
const prompt = {
messages: [
{ role: "user", content: promptText }
]
};
// Call the AI plugin to generate a response
const completion = await E8App.$ai.generateText(prompt);
// Retrieve the first choice returned by the AI
const aiMessage = completion.choices[0].message;
console.log("AI response:", aiMessage.content);
return aiMessage.content;
} catch (error) {
console.error("AI call failed:", error);
return null;
}
}
// Usage example
askAI("Write a short mystical poem about the moon and stars.");