$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) => AiResponse
• Details
Expects a string or an AiPrompt object that contains the user input or conversation history.
Returns an AiResponse object. The AI-generated message can be accessed via choices[0].message.content.
• Example
// Example function using the AI plugin on server-side
function askAI(promptText) {
try {
const prompt = {
messages: [
{ role: "user", content: promptText }
]
};
const completion = E8App.$ai.generateText(prompt);
if (completion.data?.choices?.length) {
const aiMessage = completion.data.choices[0].message;
// AI response is stored in aiMessage.content
return aiMessage.content;
} else {
// Handle case when AI call failed or returned no choices
return null;
}
} catch (error) {
// Handle unexpected errors
return null;
}
}
// Usage example
const response = askAI("Write a short mystical poem about the moon and stars.");
// response now contains the AI-generated text