> ## Documentation Index
> Fetch the complete documentation index at: https://friendli.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Use the Vercel AI SDK with FriendliAI for streaming chat UIs in Next.js and React. Connect to Model APIs or Dedicated Endpoints with minimal setup.

You can use [**Vercel AI SDK**](https://sdk.vercel.ai) to interact with FriendliAI.
This makes migration of existing applications already using Vercel AI SDK particularly easy.

## How to Use

Before you start, ensure you've already obtained the `API_KEY` from the [Friendli Suite > Personal Settings > API Keys](https://friendli.ai/suite/~/setting/keys).

<CodeGroup>
  ```bash npm theme={null}
  npm i ai @friendliai/ai-provider
  ```

  ```bash yarn theme={null}
  yarn add ai @friendliai/ai-provider
  ```

  ```bash pnpm theme={null}
  pnpm add ai @friendliai/ai-provider
  ```
</CodeGroup>

### Instantiation

Instantiate your models using a Friendli provider instance.
Choose the example for your endpoint type:

<CodeGroup>
  ```ts Model APIs {4,7-9} theme={null}
  import { friendli } from "@friendliai/ai-provider";

  // Automatically select Model APIs
  const model = friendli("zai-org/GLM-5.2");

  // Or specify a specific Model API
  const model = friendli("zai-org/GLM-5.2", {
    endpoint: "serverless",
  });
  ```

  ```ts Dedicated Endpoints {4,7-9} theme={null}
  import { friendli } from "@friendliai/ai-provider";

  // Replace YOUR_ENDPOINT_ID with the ID of your endpoint, e.g. "zbimjgovmlcb"
  const model = friendli("YOUR_ENDPOINT_ID");

  // Specify a dedicated endpoint instead of auto-selecting
  const model = friendli("YOUR_ENDPOINT_ID", {
    endpoint: "dedicated",
  });
  ```

  ```ts Friendli Container {9} theme={null}
  import { createFriendli } from "@friendliai/ai-provider";

  const friendli = createFriendli({
    // Update with the URL where your container is running.
    baseURL: "http://localhost:8000/v1",
  });

  // Containers do not require a model id.
  const model = friendli("");
  ```
</CodeGroup>

### Example: Generating Text

Generate a response with the `generateText` function:

```ts theme={null}
import { friendli } from "@friendliai/ai-provider";
import { generateText } from "ai";

const { text } = await generateText({
  model: friendli("zai-org/GLM-5.2"),
  prompt: "Write a vegetarian lasagna recipe for 4 people.",
});

console.log(text);
```

### Example: Using Enforcing Patterns (Regex)

Specify a specific pattern (e.g., CSV), character sets, or specific language characters (e.g., Korean Hangul characters) for your LLM's output.

```ts {6} theme={null}
import { friendli } from "@friendliai/ai-provider";
import { generateText } from "ai";

const { text } = await generateText({
  model: friendli("zai-org/GLM-5.2", {
    regex: new RegExp("[\n ,.?!0-9\uac00-\ud7af]*"),
  }),
  prompt: "Who is the first king of the Joseon Dynasty?",
});

console.log(text);
```

### Example: Using Built-In Tools

<Info>
  This feature is in Beta and available only on the **Model APIs**.
</Info>

Using the tool-assisted chat completion API, models can utilize built-in tools prepared for tool calls, enhancing their capability to provide more comprehensive and actionable responses.

To learn more, see [Built-in Tools](/guides/model-apis/tool-assisted-api#built-in-tools).

```ts {6-9} theme={null}
import { friendli } from "@friendliai/ai-provider";
import { streamText } from "ai";

const result = await streamText({
  model: friendli("zai-org/GLM-5.2", {
    tools: [
        {"type": "web:search"},
        {"type": "math:calculator"},
    ],
  }),
  prompt: "Find the current USD to CAD exchange rate and calculate how much $5,000 USD would be in Canadian dollars.",
});

for await (const textPart of result.textStream) {
  console.log(textPart);
}
```

## OpenAI Compatibility

You can also use `@ai-sdk/openai` as the APIs are OpenAI-compatible.

```ts theme={null}
import { createOpenAI } from "@ai-sdk/openai";

const friendli = createOpenAI({
  baseURL: 'https://api.friendli.ai/serverless/v1',
  apiKey: process.env.API_KEY,
});
```

If you are using Dedicated Endpoints:

```ts theme={null}
import { createOpenAI } from "@ai-sdk/openai";

const friendli = createOpenAI({
  baseURL: 'https://api.friendli.ai/dedicated/v1',
  apiKey: process.env.API_KEY,
});
```

## Further Resources

* [Implementing a simple streaming chat with Next.js](https://sdk.vercel.ai/examples/next-app/basics/streaming-text-generation)
* [Build a Next.js app with the Vercel AI SDK](https://sdk.vercel.ai/docs/getting-started/nextjs-app-router)
* [Explore the Vercel AI SDK Core Reference](https://sdk.vercel.ai/docs/ai-sdk-core/overview)
