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.
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.async function ask(prompt: string, options?: AskOptions): Promise<string> & EventEmitter;
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 :(");
}
}
Name | Description | Type |
---|---|---|
prompt* | | string |
options | |
A Promise that resolves with a prompt completion.
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.
The AI model to use to answer to the prompt. Defaults to
"text-davinci-003"
.type Model = "text-davinci-003" | "gpt-3.5-turbo";
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. | |
signal | Abort signal to cancel the request. | AbortSignal |
Last modified 1mo ago