AI

AI Usage

Learn how to resolve models with c.get("ai") and call native Vercel AI SDK functions in VitNode.

Flowchart showing request handling in a VitNode route: c.get('ai') model registry resolving model instance to Vercel AI SDK functions (generateText, streamText, embed, generateImage).

Using AI in your backend routes is as simple as getting the model from c.get("ai") and passing it to native Vercel AI SDK functions!

Quick Reference

FunctionMethodDescription
c.get("ai").model(id?)Language ModelText generation, streaming, structured outputs
c.get("ai").embeddingModel(id?)Embedding ModelVector embeddings for search and RAG
c.get("ai").imageModel(id?)Image ModelImage generation

Omit the id argument to get the default model (first one in config).


bun i ai
pnpm i ai
npm i ai

Examples

1. Generate Text

Generate Text
import { buildRoute } from "@vitnode/core/api/lib/route";
import { generateText } from "ai";
import { z } from "@hono/zod-openapi";

export const summarizeRoute = buildRoute({
  pluginId: CONFIG_PLUGIN.pluginId,
  route: {
    method: "post",
    path: "/summarize",
    request: {
      body: {
        content: {
          "application/json": { schema: z.object({ text: z.string() }) },
        },
      },
    },
    responses: {
      200: {
        content: {
          "application/json": { schema: z.object({ summary: z.string() }) },
        },
        description: "Summary",
      },
    },
  },
  handler: async c => {
    const { text } = c.req.valid("json");

    const result = await generateText({
      model: c.get("ai").model(), // default model
      system: "You are a concise summarizer.",
      prompt: `Summarize:\n\n${text}`,
    });

    return c.json({ summary: result.text });
  },
});

2. Stream Text

Stream Text
import { createTextStreamResponse, streamText } from "ai";

handler: c => {
  const result = streamText({
    model: c.get("ai").model(),
    prompt: "Write a haiku about databases.",
  });

  return createTextStreamResponse({ stream: result.textStream });
};

3. Structured Output

Generate Structured Object
import { generateText, Output } from "ai";
import { z } from "zod";

const result = await generateText({
  model: c.get("ai").model(),
  output: Output.object({
    schema: z.object({
      title: z.string(),
      tags: z.array(z.string()),
    }),
  }),
  prompt:
    "Suggest a title and tags for a post about Postgres full-text search.",
});

// Access typed output safely:
result.output.title; // string
result.output.tags; // string[]

4. Embeddings

Generate Embeddings
import { embed, embedMany } from "ai";

// Single value
const { embedding } = await embed({
  model: c.get("ai").embeddingModel(),
  value: "sunny day at the beach",
});

// Batch values
const { embeddings } = await embedMany({
  model: c.get("ai").embeddingModel(),
  values: ["first document", "second document"],
});

5. Image Generation

Generate Image
import { generateImage } from "ai";

const { image } = await generateImage({
  model: c.get("ai").imageModel(),
  prompt: "A watercolor fox reading a book",
});

Client-Side Integration

The GET /api/{core}/session API automatically includes the configured AI models for the frontend:

Session Response (excerpt)
{
  "ai": {
    "models": [
      {
        "id": "default",
        "name": "Claude Sonnet 5",
        "model": "anthropic/claude-sonnet-5",
      },
      {
        "id": "fast",
        "name": "Claude Haiku 4.5",
        "model": "anthropic/claude-haiku-4.5",
      },
    ],
  },
}

Use these IDs to build model pickers or select dynamic models on the client side! 🎨

On this page