Installation
Copy
npm install ai @ai-sdk/deepinfra
Text generation
Copy
import { createDeepInfra } from "@ai-sdk/deepinfra";
import { generateText } from "ai";
const deepinfra = createDeepInfra({ apiKey: "$DEEPINFRA_TOKEN" });
const { text, usage, finishReason } = await generateText({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
prompt: "Write a vegetarian lasagna recipe for 4 people.",
});
console.log(text);
Copy
const { text } = await generateText({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
prompt: "Write a vegetarian lasagna recipe for 4 people.",
system: "You are a professional writer. You write simple, clear, and concise content.",
});
Streaming
Copy
import { createDeepInfra } from "@ai-sdk/deepinfra";
import { streamText } from "ai";
const deepinfra = createDeepInfra({ apiKey: "$DEEPINFRA_TOKEN" });
const result = streamText({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
prompt: "Invent a new holiday and describe its traditions.",
system: "You are a professional writer. You write simple, clear, and concise content.",
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}
Conversations
Copy
const { text } = await generateText({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
messages: [
{ role: "system", content: "Respond like a michelin starred chef." },
{ role: "user", content: "Can you name at least two different techniques to cook lamb?" },
{ role: "assistant", content: "Bonjour! Let me tell you..." },
{ role: "user", content: "Tell me more about the second method." },
],
});
streamText with messages.
Structured data
Generate typed, structured output using Zod schemas:Copy
import { createDeepInfra } from "@ai-sdk/deepinfra";
import { generateObject } from "ai";
import { z } from "zod";
const deepinfra = createDeepInfra({ apiKey: "$DEEPINFRA_TOKEN" });
const { object } = await generateObject({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
steps: z.array(z.string()),
}),
}),
prompt: "Generate a lasagna recipe.",
});
console.log(object.recipe.name);
Copy
const { object } = await generateObject({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
output: "enum",
enum: ["action", "comedy", "drama", "horror", "sci-fi"],
prompt: 'Classify: "A group of astronauts travel through a wormhole..."',
});
Tool / function calling
Copy
import { createDeepInfra } from "@ai-sdk/deepinfra";
import { generateText, tool } from "ai";
import { z } from "zod";
const deepinfra = createDeepInfra({ apiKey: "$DEEPINFRA_TOKEN" });
const result = await generateText({
model: deepinfra("deepseek-ai/DeepSeek-V3"),
tools: {
weather: tool({
description: "Get the weather in a location",
parameters: z.object({
location: z.string().describe("The location to get the weather for"),
}),
execute: async ({ location }) => ({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
}),
},
prompt: "What is the weather in San Francisco?",
maxSteps: 2,
});
console.log(result.text);