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.
import { AI, Clipboard } from"@raycast/api";exportdefaultasyncfunctioncommand() {constanswer=awaitAI.ask("Suggest 5 jazz songs");awaitClipboard.copy(answer);}
import { AI, showToast, Toast } from"@raycast/api";exportdefaultasyncfunctioncommand() {try {awaitAI.ask("Suggest 5 jazz songs"); } catch (error) {// Handle error here, eg: by showing a ToastawaitshowToast({ style:Toast.Style.Failure, title:"Failed to generate answer", }); }}
import { AI, getSelectedFinderItems, showHUD } from"@raycast/api";import fs from"fs";exportdefaultasyncfunctionmain() {let allData ="";const [file] =awaitgetSelectedFinderItems();constanswer=AI.ask("Suggest 5 jazz songs");// Listen to "data" event to stream the answeranswer.on("data",async (data) => { allData += data;awaitfs.promises.writeFile(`${file.path}`,allData.trim(),"utf-8"); });await answer;awaitshowHUD("Done!");}
import { AI, getSelectedFinderItems, showHUD } from"@raycast/api";import fs from"fs";exportdefaultasyncfunctionmain() {let allData ="";const [file] =awaitgetSelectedFinderItems();// 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 happeningawaitshowHUD("Generating answer...");constanswer=awaitAI.ask("Suggest 5 jazz songs");awaitfs.promises.writeFile(`${file.path}`,allData.trim(),"utf-8");// Then, when everythig is done, notify the user againawaitshowHUD("Done!");}
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.