쿠키를 사용합니다. 쿠키와 브라우저 저장소는 로그인 상태를 유지하고 환경설정을 기억합니다. 동의하시면 사이트 이용 방식도 측정합니다. 언제든지 할 수 있습니다. 자세한 내용은 쿠키 정책을 참고하세요.

Vercel AI SDK

SDK 가이드

Vercel AI SDK

Vercel AI SDK의 OpenAI 호환 제공자를 api14에 연결해 generateText, streamText, generateObject, embed로 모든 모델을 호출하는 방법과 도구 및 오류 처리를 설명합니다.

Vercel AI SDK는 OpenAI 호환 제공자를 통해 api14와 통신합니다. 모든 모델 ID를 generateText, streamText, generateObject, embed에 전달할 수 있는 모델로 사용할 수 있습니다.

설치

npm install ai @ai-sdk/openai-compatible zod
export API14_API_KEY="sk-api14-..."

설정

import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

export const api14 = createOpenAICompatible({
  name: "api14",
  baseURL: "https://api14.com/api/v1",
  apiKey: process.env.API14_API_KEY,
  includeUsage: true,
});

@ai-sdk/openai도 사용할 수 있습니다: createOpenAI({ baseURL: "https://api14.com/api/v1", apiKey })로 만든 뒤, 요청이 /chat/completions로 가도록 openai.chat("anthropic/claude-sonnet-4.5")를 사용하세요.

텍스트 생성

import { generateText } from "ai";

const { text, usage } = await generateText({
  model: api14("anthropic/claude-sonnet-4.5"),
  prompt: "Explain vector databases in two sentences.",
});
console.log(text, usage);

스트리밍

import { streamText } from "ai";

const result = streamText({
  model: api14("openai/gpt-5-mini"),
  prompt: "Write a haiku about APIs.",
});

for await (const delta of result.textStream) process.stdout.write(delta);

Next.js 라우트 핸들러에서는 스트림을 그대로 반환하세요.

// app/api/chat/route.ts
import { convertToModelMessages, streamText, type UIMessage } from "ai";

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();
  const result = streamText({ model: api14("openai/gpt-5-mini"), messages: convertToModelMessages(messages) });
  return result.toUIMessageStreamResponse();
}

도구와 구조화된 출력

import { generateObject, generateText, tool } from "ai";
import { z } from "zod";

const { text } = await generateText({
  model: api14("openai/gpt-5-mini"),
  prompt: "What is the weather in Istanbul?",
  tools: {
    getWeather: tool({
      description: "Get the current weather",
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, tempC: 21 }),
    }),
  },
});

const { object } = await generateObject({
  model: api14("openai/gpt-5-mini"),
  schema: z.object({ city: z.string(), country: z.string() }),
  prompt: 'Extract the place from: "I live in Lisbon, Portugal."',
});

임베딩

import { embedMany } from "ai";

const { embeddings } = await embedMany({
  model: api14.textEmbeddingModel("openai/text-embedding-3-small"),
  values: ["The quick brown fox", "Lorem ipsum"],
});

오류

import { APICallError } from "ai";

try {
  await generateText({ model: api14("openai/gpt-5-mini"), prompt: "Hi" });
} catch (e) {
  if (APICallError.isInstance(e) && e.statusCode === 402) console.error("Balance too low, add funds");
  else if (APICallError.isInstance(e) && e.statusCode === 429) console.error("Retry after", e.responseHeaders?.["retry-after"]);
  else throw e;
}

SDK는 기본적으로 429와 5xx를 두 번 재시도합니다(각 호출의 maxRetries). 오류를 참고하세요.