Creating Plot Twists
    Preparing search index...

    Class AIAbstract

    Built-in tool for prompting Large Language Models (LLMs).

    The AI tool provides twists and tools with access to LLM capabilities for natural language processing, text generation, data extraction, and intelligent decision making within their workflows.

    Features:

    • Access to multiple AI providers (OpenAI, Anthropic, Google, Workers AI)
    • Multi-turn conversation support with messages
    • Tool calling with automatic execution
    • Structured output with Typebox schemas via outputSchema
    • Unified API across all models via Vercel AI SDK
    • Automatic response parsing and validation with full type inference
    import { Type } from "typebox";

    class SmartEmailTool extends Tool {
    private ai: AI;

    constructor(id: string, tools: ToolBuilder) {
    super();
    this.ai = tools.get(AI);
    }

    async categorizeEmail(emailContent: string) {
    // Define the output schema using Typebox
    const schema = Type.Object({
    category: Type.Union([
    Type.Literal("work"),
    Type.Literal("personal"),
    Type.Literal("spam"),
    Type.Literal("promotional")
    ]),
    confidence: Type.Number({ minimum: 0, maximum: 1 }),
    reasoning: Type.Optional(Type.String())
    });

    const response = await this.ai.prompt({
    model: { speed: "fast", cost: "medium" },
    system: "Classify emails into categories: work, personal, spam, or promotional.",
    prompt: `Categorize this email: ${emailContent}`,
    outputSchema: schema
    });

    return response.output;
    }

    async generateResponse(emailContent: string) {
    const response = await this.ai.prompt({
    model: { speed: "fast", cost: "medium" },
    system: "Generate professional email responses that are helpful and concise.",
    prompt: `Write a response to: ${emailContent}`
    });

    return response.text;
    }
    }

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    Methods

    • Sends a request to an AI model and returns the response using the Vercel AI SDK.

      Supports text generation, multi-turn conversations, structured outputs, and tool calling across multiple AI providers via Cloudflare AI Gateway.

      Type Parameters

      • TOOLS extends AIToolSet
      • SCHEMA extends TSchema = never

      Parameters

      • request: AIRequest<TOOLS, SCHEMA>

        AI request with model, prompt/messages, and optional configuration

      Returns Promise<AIResponse<TOOLS, SCHEMA>>

      Promise resolving to the AI response with generated text and metadata

      // Simple text generation
      const response = await ai.prompt({
      model: { speed: "fast", cost: "medium" },
      prompt: "Explain quantum computing in simple terms"
      });
      console.log(response.text);

      // Fast and cheap for simple tasks
      const response = await ai.prompt({
      model: { speed: "fast", cost: "low" },
      prompt: "Summarize this text..."
      });
      console.log(response.text);

      // With system instructions for complex reasoning
      const response = await ai.prompt({
      model: { speed: "capable", cost: "high" },
      system: "You are a helpful physics tutor.",
      prompt: "Explain quantum entanglement"
      });
      console.log(response.text);

      // Multi-turn conversation
      const response = await ai.prompt({
      model: { speed: "balanced", cost: "medium" },
      messages: [
      { role: "user", content: "What is 2+2?" },
      { role: "assistant", content: "2+2 equals 4." },
      { role: "user", content: "What about 3+3?" }
      ]
      });
      console.log(response.text);

      // Structured output with Typebox schema
      const response = await ai.prompt({
      model: { speed: "fast", cost: "medium" },
      prompt: "Extract information: John is 30 years old",
      outputSchema: Type.Object({
      name: Type.String(),
      age: Type.Number()
      })
      });
      console.log(response.output); // { name: "John", age: 30 }

      // Tool calling
      const response = await ai.prompt({
      model: { speed: "balanced", cost: "medium" },
      prompt: "What's the weather in San Francisco?",
      tools: {
      getWeather: {
      description: "Get weather for a city",
      parameters: Type.Object({
      city: Type.String()
      }),
      execute: async ({ city }) => {
      return { temp: 72, condition: "sunny" };
      }
      }
      }
      });
      console.log(response.text); // Model's response using tool results
      console.log(response.toolCalls); // Array of tool calls made