AI

AI Usage

Text generation, streaming responses, structured output, and embeddings using the Vercel AI SDK and VitNode model registry.

VitNode integrates the Vercel AI SDK. Resolve models from c.get("ai") and call native SDK functions directly.

Request passing through c.get('ai') model registry and invoking AI SDK functions

Model Resolvers

ResolverReturned TypeUsed By
c.get("ai").model(id?)LanguageModelgenerateText, streamText, generateObject
c.get("ai").embeddingModel(id?)EmbeddingModelembed, embedMany
c.get("ai").imageModel(id?)ImageModelgenerateImage

1. Text Generation

Generate text responses in a Hono API route:

plugins/blog/src/api/modules/posts/routes/summarize.route.ts
import { buildRoute } from "@vitnode/core/api/lib/route"
import { generateText } from "ai"
import { z } from "zod"

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

    const { text: summary } = await generateText({
      model: c.get("ai").model(),
      system: "You are a concise summary assistant.",
      prompt: `Summarize the following content:

${text}`,
    })

    return c.json({ summary })
  },
})

2. Streaming Responses

Stream LLM responses directly to the client:

import { streamText } from "ai"

handler: async (c) => {
  const result = streamText({
    model: c.get("ai").model(),
    prompt: "Write an introduction to WebSockets.",
  })

  return result.toDataStreamResponse()
}

3. Structured Output (generateObject)

Extract strongly typed JSON from model completions using Zod:

import { generateObject } from "ai"
import { z } from "zod"

const postMetadataSchema = z.object({
  title: z.string(),
  tags: z.array(z.string()),
  estimatedReadingMinutes: z.number(),
})

const { object } = await generateObject({
  model: c.get("ai").model(),
  schema: postMetadataSchema,
  prompt: "Generate SEO metadata for an article on Postgres indexing.",
})

4. Generating Embeddings

Calculate vector embeddings for semantic search:

import { embed } from "ai"

const { embedding } = await embed({
  model: c.get("ai").embeddingModel(),
  value: "How to configure Redis caching in VitNode",
})

Learn More