Skip to content

OpenAI-compatible plugin

The OpenAI-Compatible API package (genkit-plugin-compat-oai) provides a unified interface for accessing multiple AI providers that implement OpenAI’s API specification. This includes OpenAI and other compatible services.

The compat_oai package serves as a foundation for building plugins that work with OpenAI-compatible APIs. It includes:

  • Base Implementation: Common functionality for OpenAI-compatible APIs
  • OpenAI Plugin: Direct access to OpenAI’s models and embeddings
  • Extensible Framework: Build custom plugins for other compatible providers

Depending on which provider you use, you’ll need:

Terminal window
uv add genkit-plugin-compat-oai
from genkit import Genkit
from genkit.plugins.compat_oai import OpenAI
# Custom base URL for OpenAI-compatible services
ai = Genkit(
plugins=[
OpenAI(
api_key='YOUR_API_KEY',
base_url='https://your-custom-endpoint.com/v1',
organization='your-org-id', # Optional
default_headers={'Custom-Header': 'value'}, # Optional
)
],
)

OpenAI-compatible configuration options are supported:

from genkit import Genkit
from genkit.plugins.compat_oai import OpenAI, openai_model
ai = Genkit(plugins=[OpenAI()])
response = await ai.generate(
model=openai_model('gpt-4o'),
prompt='Your prompt here',
config={
'temperature': 0.7,
'max_tokens': 1000,
'top_p': 0.9,
},
)