Introduction
The AI API provides a powerful interface for accessing language model capabilities. This documentation covers everything you need to know to effectively use the AI in your applications.
Endpoint Details
POST /api/ai_completion
The main endpoint for AI completions and interactions.
Request Format
The API expects a POST request with the following JSON structure:
{
prompt: string, // Instructions for the AI
data?: any // Optional context data
}
Prompt Structure
The prompt should include:
- Clear instructions for the AI
- Expected response format using TypeScript interface
- Example response
Example Prompt Structure:
Generate 3 creative names for a pet store.
interface Response {
names: string[];
descriptions: string[];
}
{
"names": [
"Pawsome Pals",
"Whisker Wonderland",
"Tail Tales"
],
"descriptions": [
"A friendly neighborhood pet store",
"Magical pet supply emporium",
"Your pet's favorite story"
]
}
Response Format
The API returns JSON matching the specified TypeScript interface in your prompt.
⚠️ Important
Always validate the response against your expected interface to ensure type safety.
Examples
Best Practices
1. Clear Instructions
Provide specific, unambiguous instructions in your prompt
2. Type Safety
Always include a TypeScript interface for expected responses
3. Error Handling
Implement proper error handling for API calls
4. Context
Provide relevant context data when needed
Error Handling
async function callAI() {
try {
const response = await fetch('/api/ai_completion', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: '...',
data: '...'
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error calling AI:', error);
throw error;
}
}