API Reference
Complete reference for all expo-ai-kit methods and types.
isAvailable()
iOS Checks if on-device AI capabilities are available on the current device.
function isAvailable(): Promise<boolean>Returns
A Promise that resolves to true if on-device AI is available,false otherwise.
Example
import { isAvailable } from 'expo-ai-kit';
const available = await isAvailable();
if (available) {
// Proceed with AI features
} else {
// Show fallback or disable AI features
}On iOS, this checks for Apple Intelligence availability. The function returns false if the device doesn't support Apple Intelligence, if it's disabled in Settings, or if the device is running iOS below 18.4.
prepareModel()
iOS Pre-loads and prepares the AI model for faster first response. This is optional but recommended if you know the user will soon interact with AI features.
function prepareModel(): Promise<void>Returns
A Promise that resolves when the model is ready.
Example
import { prepareModel, isAvailable } from 'expo-ai-kit';
// Call early, e.g., when user navigates to a chat screen
async function onScreenFocus() {
const available = await isAvailable();
if (available) {
await prepareModel();
console.log('Model ready for fast responses');
}
}Calling prepareModel() uses system resources. Only call it when you're reasonably sure the user will use AI features soon.
createSession()
iOS Creates a new AI session for conversation. Sessions maintain context across multiple messages, enabling multi-turn conversations.
function createSession(options?: SessionOptions): Promise<Session>Parameters
| Name | Type | Description |
|---|---|---|
options | SessionOptions | Optional configuration for the session |
SessionOptions
interface SessionOptions {
// System prompt to set the AI's behavior
systemPrompt?: string;
// Maximum tokens in the conversation history
maxHistoryTokens?: number;
}Returns
A Promise that resolves to a Session object.
Example
import { createSession } from 'expo-ai-kit';
// Basic session
const session = await createSession();
// Session with custom system prompt
const assistantSession = await createSession({
systemPrompt: 'You are a helpful cooking assistant. Provide recipes and cooking tips.',
});
// Session with limited history
const lightSession = await createSession({
maxHistoryTokens: 2000,
});sendMessage()
iOS Sends a message to the AI and receives a response. Supports both full responses and streaming.
function sendMessage(
session: Session,
options: SendMessageOptions
): Promise<Response>Parameters
| Name | Type | Description |
|---|---|---|
session | Session | The session to send the message in |
options | SendMessageOptions | Message configuration |
SendMessageOptions
interface SendMessageOptions {
// The message to send
message: string;
// Callback for streaming tokens
onToken?: (token: string) => void;
// Callback when streaming completes
onComplete?: (response: Response) => void;
// Callback for errors during streaming
onError?: (error: Error) => void;
}Returns
A Promise that resolves to a Response object containing the full response.
Example - Basic Usage
import { createSession, sendMessage } from 'expo-ai-kit';
const session = await createSession();
const response = await sendMessage(session, {
message: 'What are the benefits of TypeScript?',
});
console.log(response.text);Example - Streaming
import { createSession, sendMessage } from 'expo-ai-kit';
const session = await createSession();
const response = await sendMessage(session, {
message: 'Tell me a short story',
onToken: (token) => {
// Called for each token as it's generated
process.stdout.write(token);
},
onComplete: (response) => {
console.log('\nGeneration complete!');
console.log('Total tokens:', response.tokenCount);
},
onError: (error) => {
console.error('Error during generation:', error);
},
});When using onToken, the function still returns a Promise that resolves with the complete response. The streaming callbacks are called as tokens arrive, while the Promise resolves after all tokens are received.
Types
Session
Represents an active AI conversation session.
interface Session {
// Unique identifier for the session
id: string;
// When the session was created
createdAt: Date;
// Current message count
messageCount: number;
// End the session and free resources
close(): Promise<void>;
// Clear conversation history but keep the session
clearHistory(): Promise<void>;
}SendMessageOptions
Options for sending a message.
interface SendMessageOptions {
message: string;
onToken?: (token: string) => void;
onComplete?: (response: Response) => void;
onError?: (error: Error) => void;
}Response
The response from an AI message.
interface Response {
// The full text response
text: string;
// Number of tokens in the response
tokenCount: number;
// Time taken to generate (milliseconds)
generationTime: number;
// Whether the response was cut off due to length limits
truncated: boolean;
}