$ai
Updated on Aug 19, 2025 7 minutes to readThe AI Plugin provides an API for sending prompts to an AI model and retrieving generated responses.
Methods
| Method | Description |
|---|---|
| generateText | Calls the API and returns a text completion response. |
Methods Details
generateText()
• Type
(prompt: string | AiPrompt) => Promise<AiChatCompletion>
🔽 Show more
• Details
Expects a string or an AiPrompt object that contains the user input or conversation history.
Returns a Promise that resolves with an AiChatCompletion object. The AI-generated message can be accessed via choices[0].message.content.
• 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.");
// Select a model and request a response that conforms to a JSON schema
const completion = await E8App.$ai.generateText({
model: "gpt-4.1-mini",
messages: [
{
role: "developer",
content: "Extract the requested information and return valid JSON."
},
{
role: "user",
content: "Ada Lovelace was born in London in 1815."
}
],
response_format: {
type: "json_schema",
json_schema: {
name: "person",
strict: true,
schema: {
type: "object",
properties: {
name: { type: "string" },
birthplace: { type: "string" },
birthYear: { type: "integer" }
},
required: ["name", "birthplace", "birthYear"],
additionalProperties: false
}
}
}
});
const person = JSON.parse(completion.choices[0].message.content);
// Combine text with a previously uploaded file
const fileCompletion = await E8App.$ai.generateText({
messages: [
{
role: "user",
content: [
{ type: "text", text: "Summarize the attached file." },
{ type: "file", file: { file_id: "file-abc123" } }
]
}
]
});
🔽 Show more