文档
CodeRabbit
Cloudflare
AG Grid
Netlify
Neon
WorkOS
Clerk
Convex
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
Netlify
Neon
WorkOS
Clerk
Convex
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
类引用
函数引用
接口引用
类型别名引用
变量引用
社区适配器

Cencori

Cencori 适配器提供了一个统一的接口来访问 14+ 个 AI 提供商(OpenAI、Anthropic、Google、xAI 等),并内置了安全性、可观察性和成本跟踪功能。

安装

sh
npm install @cencori/ai-sdk

基本用法

typescript
import { chat } from "@tanstack/ai";
import { cencori } from "@cencori/ai-sdk/tanstack";

const adapter = cencori("gpt-4o");

for await (const chunk of chat({
  adapter,
  messages: [{ role: "user", content: "Hello!" }],
})) {
  if (chunk.type === "content") {
    console.log(chunk.delta);
  }
}

配置

typescript
import { createCencori } from "@cencori/ai-sdk/tanstack";

const cencori = createCencori({
  apiKey: process.env.CENCORI_API_KEY!,
  baseUrl: "https://cencori.com", // Optional
});

const adapter = cencori("gpt-4o");

流式传输

typescript
import { chat } from "@tanstack/ai";
import { cencori } from "@cencori/ai-sdk/tanstack";

const adapter = cencori("claude-3-5-sonnet");

for await (const chunk of chat({
  adapter,
  messages: [{ role: "user", content: "Tell me a story" }],
})) {
  if (chunk.type === "content") {
    process.stdout.write(chunk.delta);
  } else if (chunk.type === "done") {
    console.log("\nDone:", chunk.finishReason);
  }
}

工具调用

typescript
import { chat } from "@tanstack/ai";
import { cencori } from "@cencori/ai-sdk/tanstack";

const adapter = cencori("gpt-4o");

for await (const chunk of chat({
  adapter,
  messages: [{ role: "user", content: "What's the weather in NYC?" }],
  tools: {
    getWeather: {
      name: "getWeather",
      description: "Get weather for a location",
      inputSchema: {
        type: "object",
        properties: { location: { type: "string" } },
      },
    },
  },
})) {
  if (chunk.type === "tool_call") {
    console.log("Tool call:", chunk.toolCall);
  }
}

多提供商支持

通过单个参数在提供商之间切换

typescript
import { cencori } from "@cencori/ai-sdk/tanstack";

// OpenAI
const openai = cencori("gpt-4o");

// Anthropic
const anthropic = cencori("claude-3-5-sonnet");

// Google
const google = cencori("gemini-2.5-flash");

// xAI
const grok = cencori("grok-3");

// DeepSeek
const deepseek = cencori("deepseek-v3.2");

所有响应都使用相同的统一格式,无论提供商如何。

支持的模型

提供者模型
OpenAIgpt-5, gpt-4o, gpt-4o-mini, o3, o1
Anthropicclaude-opus-4, claude-sonnet-4, claude-3-5-sonnet
Googlegemini-3-pro, gemini-2.5-flash, gemini-2.0-flash
xAIgrok-4, grok-3
Mistralmistral-large, codestral, devstral
DeepSeekdeepseek-v3.2, deepseek-reasoner
+ 更多Groq, Cohere, Perplexity, Together, Qwen, OpenRouter

环境变量

sh
CENCORI_API_KEY=csk_your_api_key_here

获取 API 密钥

  1. 访问 Cencori
  2. 创建一个帐户并生成一个 API 密钥
  3. 将其添加到您的环境变量中

为什么选择 Cencori?

  • 🔒 安全性 — PII 过滤、越狱检测、内容审核
  • 📊 可观察性 — 请求日志、延迟指标、成本跟踪
  • 💰 成本控制 — 预算、警报、每路由分析
  • 🔌 多提供商 — 一个 API 密钥用于 14+ 个 AI 提供商
  • 🛠️ 工具调用 — 完全支持跨提供商的函数调用
  • 🔄 故障转移 — 自动重试和回退到替代提供商

API 参考

cencori(model)

使用环境变量创建一个 Cencori 适配器。

参数

  • model - 模型名称(例如 "gpt-4o", "claude-3-5-sonnet", "gemini-2.5-flash"

返回值: 一个 Cencori TanStack AI 适配器实例。

createCencori(config)

创建一个自定义 Cencori 适配器工厂。

参数

  • config.apiKey - 您的 Cencori API 密钥
  • config.baseUrl? - 自定义基础 URL(可选)

返回值: 一个函数,用于为特定模型创建适配器实例。

下一步