Call AI from code

ai.chat returns text. ai.image generates an image. Pass true as the second argument to chat when you want JSON.

On this page

Chat

async ({ ai }) => {
  const text = await ai.chat("Name three follow-up questions for a demo.")
  const parsed = await ai.chat("Return JSON { questions: string[] }", true)
  return { text, parsed }
}

Images and embeddings

ai.analyzeImage reads an image. ai.image generates one. ai.embedding returns a vector for a string. Each call spends credits based on the work it does.