cheapaimodel

Vercel AI SDK

Dùng mô hình của cheapaimodel trong Vercel AI SDK qua provider tương thích OpenAI.

Sao chép

AI SDK kết nối với các dịch vụ tương thích OpenAI qua provider @ai-sdk/openai-compatible. Tạo một provider cho cheapaimodel rồi truyền mô hình từ provider đó cho generateText, streamText và các hàm khác của AI SDK.

Thiết lập AI SDKGiá trị
Base URLbaseURLhttps://api.cheapaimodel.ai/v1
API keyapiKeyKey của bạn từ trang API key trong bảng điều khiển
ID mô hìnhcheapaimodel(modelId)deepseek-v4-1-flash, hoặc bất kỳ ID nào trong danh mục

Thiết lập

Terminal
npm install ai @ai-sdk/openai-compatible
cheapaimodel.ts
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

export const cheapaimodel = createOpenAICompatible({
  name: "cheapaimodel",
  baseURL: "https://api.cheapaimodel.ai/v1",
  apiKey: process.env.CHEAPAIMODEL_API_KEY,
  includeUsage: true, // token usage on streamed responses
});
generate.ts
import { generateText } from "ai";
import { cheapaimodel } from "./cheapaimodel";

const { text } = await generateText({
  model: cheapaimodel("deepseek-v4-1-flash"),
  prompt: "Explain recursion in one sentence.",
});
console.log(text);

Provider này gửi yêu cầu tới /v1/chat/completions. Trong route handler của Next.js cũng dùng y như vậy: truyền cheapaimodel("deepseek-v4-1-flash") làm model.

Streaming

stream.ts
import { streamText } from "ai";
import { cheapaimodel } from "./cheapaimodel";

const result = streamText({
  model: cheapaimodel("deepseek-v4-1-flash"),
  prompt: "Write a haiku about rivers.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
console.log("\nTokens:", (await result.usage).totalTokens);

Lỗi thường gặp

  • Unauthenticated request to AI Gateway. Mô hình được truyền dưới dạng chuỗi thuần, ví dụ model: "deepseek-v4-1-flash". AI SDK gửi chuỗi mô hình thuần tới Vercel AI Gateway, không phải tới cheapaimodel. Truyền cheapaimodel("deepseek-v4-1-flash") thay vào đó.
  • Not Found hoặc Invalid URL (POST /v1/chat/completions/chat/completions). baseURL thiếu /v1, hoặc có kèm đường dẫn endpoint. Đặt đúng https://api.cheapaimodel.ai/v1; provider tự thêm /chat/completions.
  • Lượng token đã dùng là undefined khi gọi streaming. Provider tương thích OpenAI chỉ yêu cầu thông tin mức sử dụng trong stream khi bạn đặt includeUsage: true. Giữ thiết lập này trong provider.
  • Hội thoại nhiều lượt qua @ai-sdk/openai. createOpenAI của gói này cũng nhận Base URL của chúng tôi, nhưng openai(modelId) mặc định gọi Responses API, và khi bạn truyền lại các câu trả lời trước của SDK trong messages, SDK gửi các câu trả lời đó dưới dạng ID item_reference thay vì nội dung văn bản. Cách này chỉ chạy được nếu máy chủ đã lưu các phản hồi đó. Dùng provider tương thích OpenAI ở trên, hoặc openai.chat(modelId).
Sao chép
Mở trong

Trên trang này