Vercel AI SDK

Use Gerbil as a drop-in provider for the Vercel AI SDK. Full compatibility with generateText, streamText, generateObject, generateSpeech, and transcribe.

Recommended: The AI SDK integration is the best way to use Gerbil in production apps. It provides streaming, structured output, and tool calling out of the box.

Installation

Terminal
npm install @tryhamster/gerbil ai

Quick Start

basic.ts
01import { generateText } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const { text } = await generateText({
05 model: gerbil("qwen3-0.6b"),
06 prompt: "Write a haiku about TypeScript",
07});
08
09console.log(text);

Streaming Responses

Stream text token by token for real-time UIs:

streaming.ts
01import { streamText } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const stream = streamText({
05 model: gerbil("qwen3-0.6b"),
06 prompt: "Explain quantum computing",
07});
08
09for await (const chunk of stream.textStream) {
10 process.stdout.write(chunk);
11}
12
13// Get final result
14const result = await stream;
15console.log("\nTokens:", result.usage.totalTokens);

Thinking Mode

Enable chain-of-thought reasoning with Qwen3 models:

thinking.ts
01import { generateText } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const { text, experimental_providerMetadata } = await generateText({
05 model: gerbil("qwen3-0.6b", { thinking: true }),
06 prompt: "What is 127 × 43? Show your work.",
07});
08
09// Access the thinking process
10const thinking = experimental_providerMetadata?.gerbil?.thinking;
11console.log("Reasoning:", thinking);
12console.log("Answer:", text);

Structured Output

Generate type-safe structured data with Zod schemas:

structured.ts
01import { generateObject } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03import { z } from "zod";
04
05const { object } = await generateObject({
06 model: gerbil("qwen3-0.6b"),
07 schema: z.object({
08 name: z.string(),
09 age: z.number(),
10 city: z.string(),
11 interests: z.array(z.string()),
12 }),
13 prompt: "Extract: John is 32, lives in NYC, loves hiking and photography",
14});
15
16console.log(object);
17// { name: "John", age: 32, city: "NYC", interests: ["hiking", "photography"] }

Tool Calling

Let the model call functions to accomplish tasks:

tools.ts
01import { generateText, tool } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03import { z } from "zod";
04
05const { text, toolCalls } = await generateText({
06 model: gerbil("qwen3-0.6b"),
07 prompt: "What's the weather in San Francisco?",
08 tools: {
09 getWeather: tool({
10 description: "Get weather for a city",
11 parameters: z.object({
12 city: z.string(),
13 }),
14 execute: async ({ city }) => {
15 return `Weather in ${city}: 72°F, sunny`;
16 },
17 }),
18 },
19});
20
21console.log("Tool calls:", toolCalls);
22console.log("Response:", text);

System Prompts

system.ts
01import { generateText } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const { text } = await generateText({
05 model: gerbil("qwen3-0.6b"),
06 system: "You are a helpful coding assistant. Be concise.",
07 prompt: "How do I reverse a string in JavaScript?",
08});

Multi-turn Conversations

messages.ts
01import { generateText } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const { text } = await generateText({
05 model: gerbil("qwen3-0.6b"),
06 messages: [
07 { role: "user", content: "My name is Alice" },
08 { role: "assistant", content: "Hello Alice! Nice to meet you." },
09 { role: "user", content: "What's my name?" },
10 ],
11});
12
13console.log(text); // "Your name is Alice!"

Speech Generation (TTS)

Generate speech from text using Kokoro or Supertonic TTS:

speech.ts
01import { experimental_generateSpeech as generateSpeech } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03import { writeFile } from "fs/promises";
04
05const result = await generateSpeech({
06 model: gerbil.speech(), // kokoro-82m by default
07 text: "Hello, welcome to Gerbil!",
08 voice: "af_heart", // Female American voice
09});
10
11// result.audio is a Uint8Array in WAV format
12await writeFile("output.wav", result.audio);

List available voices:

voices.ts
01const voices = gerbil.listVoices();
02// Returns: [{ id, name, gender, language }, ...]
03
04// Example voices:
05// - af_heart (Female, American) ⭐
06// - bf_emma (Female, British)
07// - am_fenrir (Male, American)
08// - bm_daniel (Male, British)

Configure speech options:

speech-options.ts
01const result = await generateSpeech({
02 model: gerbil.speech("kokoro-82m", {
03 voice: "bf_emma", // Default voice
04 speed: 1.2, // Speed multiplier (0.5-2.0)
05 }),
06 text: "Speak faster!",
07});

Transcription (STT)

Transcribe audio to text using Whisper:

transcription.ts
01import { experimental_transcribe as transcribe } from "ai";
02import { gerbil } from "@tryhamster/gerbil/ai";
03import { readFile } from "fs/promises";
04
05const result = await transcribe({
06 model: gerbil.transcription(), // whisper-tiny.en by default
07 audio: await readFile("audio.wav"),
08});
09
10console.log(result.text); // "Hello world"
11console.log(result.language); // "en"
12console.log(result.durationInSeconds); // 2.5
13console.log(result.segments); // Timestamped segments

Use a larger model for better accuracy:

transcription-models.ts
01// Larger model for better accuracy
02const result = await transcribe({
03 model: gerbil.transcription("whisper-base"),
04 audio: audioBuffer,
05});
06
07// Multilingual model with language hint
08const spanish = await transcribe({
09 model: gerbil.transcription("whisper-small", { language: "es" }),
10 audio: spanishAudio,
11});

Available transcription models:

ModelSizeLanguages
whisper-tiny.en39MEnglish only (fastest)
whisper-base.en74MEnglish only
whisper-small.en244MEnglish only
whisper-base74M99 languages
whisper-small244M99 languages
whisper-large-v3-turbo809M80+ languages (best quality)

Custom Provider Configuration

Create a custom provider with specific settings:

custom-provider.ts
01import { createGerbil } from "@tryhamster/gerbil/ai";
02import { generateText, experimental_generateSpeech, experimental_transcribe } from "ai";
03
04// Create a custom provider
05const local = createGerbil({
06 device: "gpu", // "auto" | "gpu" | "cpu"
07 dtype: "q4", // "q4" | "q8" | "fp16" | "fp32"
08 cacheDir: "./models", // Custom cache directory
09});
10
11// Text generation
12const { text } = await generateText({
13 model: local("qwen3-0.6b", { thinking: true }),
14 prompt: "Write a poem",
15});
16
17// Speech (TTS)
18const speech = await experimental_generateSpeech({
19 model: local.speech(),
20 text: "Hello world",
21});
22
23// Transcription (STT)
24const transcript = await experimental_transcribe({
25 model: local.transcription(),
26 audio: audioData,
27});

Model Preloading & Caching

Download models ahead of time so users don't wait on first use. The provider maintains a model cache — preloaded models are automatically reused across all subsequent calls.

preload.ts
01import { gerbil } from "@tryhamster/gerbil/ai";
02import { generateText } from "ai";
03
04// Preload once at app startup
05await gerbil.preload("qwen3-0.6b", {
06 keepLoaded: true, // Keep in memory
07 onProgress: (p) => console.log(p.status, p.progress),
08});
09
10// All subsequent calls reuse the cached model instance - instant!
11const { text } = await generateText({
12 model: gerbil("qwen3-0.6b"),
13 prompt: "Hello!",
14});
15
16// This also uses the same cached instance
17const { text: text2 } = await generateText({
18 model: gerbil("qwen3-0.6b"),
19 prompt: "Another prompt",
20});

keepLoaded Option

ValueBehaviorLoad Time
falseDownload → dispose → loads from disk later~1-2s
true ⭐Download → cache instance → reuse everywhere0ms
keep-loaded.ts
// Download only (default) - frees RAM, loads from disk later (~1-2s)
await gerbil.preload("qwen3-0.6b");
// Keep in memory - instant inference, model cached for reuse
await gerbil.preload("qwen3-0.6b", { keepLoaded: true });

Cache Checking

Check if a model is already cached before downloading:

cache-check.ts
// Check disk cache
const isCached = await gerbil.isCached("qwen3-0.6b");
if (!isCached) {
await gerbil.preload("qwen3-0.6b", { keepLoaded: true });
}

Preload methods on the provider:

MethodDescription
gerbil.preload(modelId, opts?)Preload model (with optional caching)
gerbil.isCached(modelId)Check if model is in disk cache

Model Options

OptionTypeDefaultDescription
thinkingbooleanfalseEnable chain-of-thought reasoning
maxTokensnumber256Maximum tokens to generate
temperaturenumber0.7Sampling temperature (0-2)
topPnumber0.9Nucleus sampling threshold
topKnumber50Top-k sampling

Full Next.js Example

Complete example with API route and React component:

app/api/chat/route.ts
01// app/api/chat/route.ts
02import { streamText } from "ai";
03import { gerbil } from "@tryhamster/gerbil/ai";
04
05export async function POST(req: Request) {
06 const { messages } = await req.json();
07
08 const result = streamText({
09 model: gerbil("qwen3-0.6b"),
10 messages,
11 });
12
13 return result.toDataStreamResponse();
14}
app/page.tsx
01// app/page.tsx
02"use client";
03import { useChat } from "ai/react";
04
05export default function Chat() {
06 const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
07
08 return (
09 <div className="flex flex-col h-screen p-4">
10 <div className="flex-1 overflow-auto space-y-4">
11 {messages.map((m) => (
12 <div key={m.id} className={m.role === "user" ? "text-right" : ""}>
13 <span className="font-bold">{m.role}:</span> {m.content}
14 </div>
15 ))}
16 </div>
17 <form onSubmit={handleSubmit} className="flex gap-2 mt-4">
18 <input
19 value={input}
20 onChange={handleInputChange}
21 placeholder="Say something..."
22 className="flex-1 p-2 border rounded"
23 />
24 <button type="submit" disabled={isLoading}>
25 {isLoading ? "..." : "Send"}
26 </button>
27 </form>
28 </div>
29 );
30}

Works with AI SDK Tools

Gerbil works seamlessly with ai-sdk-tools.dev for multi-agent orchestration, state management, and artifact streaming:

agent.ts
01import { createAgent } from "ai-sdk-tools";
02import { gerbil } from "@tryhamster/gerbil/ai";
03
04const agent = createAgent({
05 model: gerbil("qwen3-0.6b"),
06 tools: {
07 // Your tools here
08 },
09});
10
11const result = await agent.run("Help me plan a trip to Japan");

Specification

Gerbil implements the following AI SDK v5 interfaces:

InterfacePurposeMethod
LanguageModelV2Text generationgerbil(modelId)
SpeechModelV2Text-to-Speechgerbil.speech()
TranscriptionModelV2Speech-to-Textgerbil.transcription()