# Vercel AI SDK (/vi/docs/guides/vercel-ai-sdk)

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



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 SDK        | Giá trị                                                                                    |
| ---------- | ----------------------- | ------------------------------------------------------------------------------------------ |
| Base URL   | `baseURL`               | `https://api.cheapaimodel.ai/v1`                                                           |
| API key    | `apiKey`                | Key của bạn từ trang [API key](https://console.cheapaimodel.ai/keys) trong bảng điều khiển |
| ID mô hình | `cheapaimodel(modelId)` | `deepseek-v4-1-flash`, hoặc bất kỳ ID nào trong [danh mục](/vi/models)                     |

## Thiết lập [#set-it-up]

```bash title="Terminal"
npm install ai @ai-sdk/openai-compatible
```

```ts title="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
});
```

```ts title="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 [#streaming]

```ts title="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 [#common-mistakes]

* **`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)`.
