$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) => AiResponse
🔽 Show more
• 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 data.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
// Select a model and request a response that conforms to a JSON schema
const completion = 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
}
}
}
});
if (completion.success && completion.data?.choices?.length) {
const person = JSON.parse(completion.data.choices[0].message.content);
}
// Combine text with a previously uploaded file
const fileCompletion = E8App.$ai.generateText({
messages: [
{
role: "user",
content: [
{ type: "text", text: "Summarize the attached file." },
{ type: "file", file: { file_id: "file-abc123" } }
]
}
]
});
🔽 Show more