@genkit-ai/compat-oai

中文文档
1.44.0发布于 2026/11/04Apache-2.0TypeScriptESM + CJS近 30 天下载 10.4万

Genkit AI framework plugin for OpenAI APIs.

原始使用文档
文档目录14 节
  1. Supported models
  2. Installation
  3. Usage
  4. Initialize
  5. Basic examples
  6. Structured Outputs
  7. Multi-modal prompt
  8. Text Embeddings
  9. Within a flow
  10. Tool use
  11. Custom models & other Cloud providers
  12. Contributing
  13. Need help?
  14. License

OpenAI Compatible API plugin for Genkit

@genkit-ai/compat-oai is a plugin for using OpenAI Compatible APIs with Genkit.

Building with a coding agent? Install the Genkit JS skill first.

npx skills add genkit-ai/skills --skill developing-genkit-js

It teaches your agent the current Genkit JS APIs and common gotchas. Source, manual install and skills for other languages: genkit-ai/skills.

This Genkit plugin allows to use OpenAI models through their official APIs.

Official OpenAI-compatible provider documentation:

Supported models

This plugin also supports OpenAI models, and custom models from other model providers.

Installation

Install the plugin in your project with your favorite package manager:

  • npm install @genkit-ai/compat-oai
  • yarn add @genkit-ai/compat-oai
  • pnpm add @genkit-ai/compat-oai

Usage

Initialize

import dotenv from 'dotenv';
import { genkit } from 'genkit';
import openAI, { gpt35Turbo } from '@genkit-ai/compat-oai';

dotenv.config();

const ai = genkit({
  plugins: [openAI({ apiKey: process.env.OPENAI_API_KEY })],
  // specify a default model if not provided in generate params:
  model: gpt35Turbo,
});

Basic examples

The simplest way to generate text is by using the generate method:

const response = await ai.generate({
  model: gpt4o
  prompt: 'Tell me a joke.',
});

console.log(response.text);

Structured Outputs

Use config.strict: true with an output schema on a model that supports OpenAI Structured Outputs:

import { z } from 'genkit';
import { openAI } from '@genkit-ai/compat-oai/openai';

const response = await ai.generate({
  model: openAI.model('gpt-5'),
  prompt: 'Extract the answer from the text.',
  output: {
    schema: z
      .object({
        answer: z.string(),
      })
      .strict(),
  },
  config: {
    strict: true,
  },
});

console.log(response.output);

Apply .strict() to every object in the schema; Genkit does not automatically set additionalProperties: false. See the Structured Outputs guide.

Multi-modal prompt

const response = await ai.generate({
  model: gpt4o,
  prompt: [
    { text: 'What animal is in the photo?' },
    { media: { url: imageUrl } },
  ],
  config: {
    // control of the level of visual detail when processing image embeddings
    // Low detail level also decreases the token usage
    visualDetailLevel: 'low',
  },
});
console.log(response.text);

Text Embeddings

import { textEmbeddingAda002 } from '@genkit-ai/compat-oai';

const embedding = await ai.embed({
  embedder: textEmbeddingAda002,
  content: 'Hello world',
});

console.log(embedding);

Within a flow

import { z } from 'genkit';

export const jokeFlow = ai.defineFlow(
  {
    name: 'jokeFlow',
    inputSchema: z.string(),
    outputSchema: z.string(),
  },
  async (subject) => {
    const llmResponse = await ai.generate({
      prompt: `tell me a joke about ${subject}`,
    });
    return llmResponse.text;
  }
);

Tool use

import { z } from 'genkit';

// ...initialize genkit (as shown above)

const createReminder = ai.defineTool(
  {
    name: 'createReminder',
    description: 'Use this to create reminders for things in the future',
    inputSchema: z.object({
      time: z
        .string()
        .describe('ISO timestamp string, e.g. 2024-04-03T12:23:00Z'),
      reminder: z.string().describe('the content of the reminder'),
    }),
    outputSchema: z.number().describe('the ID of the created reminder'),
  },
  (reminder) => Promise.resolve(3)
);

const result = await ai.generate({
  tools: [createReminder],
  prompt: `
  You are a reminder assistant.
  If you create a reminder, describe in text the reminder you created as a response.

  Query: I have a meeting with Anna at 3 for dinner - can you set a reminder for the time?
  `,
});

console.log(result.text);

Custom models & other Cloud providers

import { GenerationCommonConfigSchema, genkit, z } from 'genkit';
import { ModelInfo } from 'genkit/model';
import openAI from '@genkit-ai/compat-oai';

const modelInfo: ModelInfo = {
  label: 'Claude - Claude 3.7 Sonnet',
  supports: {
    multiturn: true,
    tools: true,
    media: false,
    systemRole: true,
    output: ['json', 'text'],
  },
};
const schema = GenerationCommonConfigSchema.extend({});

const ai = genkit({
  plugins: [
    openAI({
      apiKey: process.env.ANTHROPIC_API_KEY,
      baseURL: 'https://api.anthropic.com/v1/',
      models: [
        { name: 'claude-3-7-sonnet', info: modelInfo, configSchema: schema },
      ],
    }),
  ],
});

export const customModelFlow = ai.defineFlow(
  {
    name: 'customModelFlow',
    inputSchema: z.string(),
    outputSchema: z.string(),
  },
  async (subject) => {
    const llmResponse = await ai.generate({
      prompt: `tell me a joke about ${subject}`,
      model: 'openai/claude-3-7-sonnet',
      config: {
        version: 'claude-3-7-sonnet-20250219',
      },
    });
    return llmResponse.text;
  }
);

Contributing

Want to contribute to the project? That's awesome! Head over to our Contribution Guidelines.

Need help?

License

This project is licensed under the Apache 2.0 License.

License: Apache 2.0

版本与发布记录

@genkit-ai/compat-oai 共发布 67 个版本,首次发布于 2025/07/11;过去 12 个月发布了 40 个版本。

版本发布日期距今
1.44.0latest2026/11/0412 天前
1.43.02026/10/2225 天前
1.43.0-rc.3next2026/10/2225 天前
1.43.0-rc.22026/10/2126 天前
1.43.0-rc.12026/10/091 个月前
1.43.0-rc.02026/10/091 个月前

关于 @genkit-ai/compat-oai 的常见问题

@genkit-ai/compat-oai 是什么?

Genkit AI framework plugin for OpenAI APIs.

如何安装 @genkit-ai/compat-oai?

在项目目录运行 npm install @genkit-ai/compat-oai,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:genkit(^1.44.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@genkit-ai/compat-oai 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@genkit-ai/compat-oai 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@genkit-ai/compat-oai 可以商用吗?

@genkit-ai/compat-oai 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@genkit-ai/compat-oai 的下载量有多少?

根据 npm 官方下载统计,@genkit-ai/compat-oai 在 2026/10/16 – 2026/11/14 这 30 天内被下载 103,859 次,日均约 3,462 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@genkit-ai/compat-oai 还在维护吗?

最新版本 1.44.0 发布于 2026/11/04(12 天前);至今共发布 67 个版本,其中过去 12 个月发布了 40 个;首次发布于 2025/07/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。