Skip to content

Vercel AI SDK

The Vercel AI SDK emphasizes developer experience and streaming. Use Glitch as the underlying client to secure inputs, retrieved context, and streaming outputs.

import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
// Configure OpenAI provider with Glitch
const glitchModel = openai('gpt-4', {
baseURL: 'https://api.golabrat.ai/v1',
apiKey: process.env.GLITCH_API_KEY!, // Your Glitch API key
});
import { openai } from '@ai-sdk/openai';
import { generateText, streamText } from 'ai';
// Configure with Glitch
const model = openai('gpt-4', {
baseURL: 'https://api.golabrat.ai/v1',
apiKey: process.env.GLITCH_API_KEY!,
});
// Generate text - automatically secured
const result = await generateText({
model,
prompt: 'What is the capital of France?',
});
console.log(result.text);
// Streaming is also secured
const stream = await streamText({
model,
prompt: 'Write a poem about AI',
});
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}

Handle security blocks gracefully:

import { generateText } from 'ai';
try {
const result = await generateText({
model,
prompt: userInput,
});
return result.text;
} catch (error: any) {
if (error?.status === 403 || error?.statusCode === 403) {
// Security block - handle gracefully
return "I can't process that request.";
}
throw error;
}

Glitch fully supports streaming responses with the Vercel AI SDK:

import { streamText } from 'ai';
const stream = await streamText({
model,
prompt: 'Write a long story',
});
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}