Links

AI

The AI API provides developers with seamless access to AI functionality without requiring API keys, configuration, or extra dependencies.
Some users might not have access to this API. If a user doesn't have access to Raycast AI, they will be asked if they want to get access when your extension calls the AI API. If the user doesn't wish to get access, the API call will throw an error.
You can check if a user has access to the API using environment.canAccess(AI).

API Reference

AI.ask

Ask AI anything you want. Use this in “no-view” Commands, effects, or callbacks. In a React component, you might want to use the useAI util hook instead.

Signature

async function ask(prompt: string, options?: AskOptions): Promise<string> & EventEmitter;

Example

Basic Usage
Error handling
Stream answer
User Feedback
Check for access
import { AI, Clipboard } from "@raycast/api";
export default async function command() {
const answer = await AI.ask("Suggest 5 jazz songs");
await Clipboard.copy(answer);
}
import { AI, showToast, Toast } from "@raycast/api";
export default async function command() {
try {
await AI.ask("Suggest 5 jazz songs");
} catch (error) {
// Handle error here, eg: by showing a Toast
await showToast({
style: Toast.Style.Failure,
title: "Failed to generate answer",
});
}
}
import { AI, getSelectedFinderItems, showHUD } from "@raycast/api";
import fs from "fs";
export default async function main() {
let allData = "";
const [file] = await getSelectedFinderItems();
const answer = AI.ask("Suggest 5 jazz songs");
// Listen to "data" event to stream the answer
answer.on("data", async (data) => {
allData += data;
await fs.promises.writeFile(`${file.path}`, allData.trim(), "utf-8");
});
await answer;
await showHUD("Done!");
}
import { AI, getSelectedFinderItems, showHUD } from "@raycast/api";
import fs from "fs";
export default async function main() {
let allData = "";
const [file] = await getSelectedFinderItems();
// If you're doing something that happens in the background
// Consider showing a HUD or a Toast as the first step
// To give users feedback about what's happening
await showHUD("Generating answer...");
const answer = await AI.ask("Suggest 5 jazz songs");
await fs.promises.writeFile(`${file.path}`, allData.trim(), "utf-8");
// Then, when everythig is done, notify the user again
await showHUD("Done!");
}
import { AI, getSelectedFinderItems, showHUD, environment } from "@raycast/api";
import fs from "fs";
export default async function main() {
if (environment.canAccess(AI)) {
const answer = await AI.ask("Suggest 5 jazz songs");
await Clipboard.copy(answer);
} else {
await showHUD("You don't have access :(");
}
}

Parameters

Name
Description
Type
prompt*
string
options

Return

A Promise that resolves with a prompt completion.

Types

AI.Creativity

Concrete tasks, such as fixing grammar, require less creativity while open-ended questions, such as generating ideas, require more.
type Creativity = "none" | "low" | "medium" | "high" | "maximum" | number;
If a number is passed, it needs to be in the range 0-2. For larger values, 2 will be used. For lower values, 0 will be used.

AI.Model

The AI model to use to answer to the prompt. Defaults to "text-davinci-003".
type Model = "text-davinci-003" | "gpt-3.5-turbo";

AI.AskOptions

Properties

Property
Description
Type
creativity
Concrete tasks, such as fixing grammar, require less creativity while open-ended questions, such as generating ideas, require more. If a number is passed, it needs to be in the range 0-2. For larger values, 2 will be used. For lower values, 0 will be used.
model
The AI model to use to answer to the prompt.
AI.Model
signal
Abort signal to cancel the request.
AbortSignal