compat_oai

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 11 Imported by: 23 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

OpenAI-Compatible Plugin Package

This directory contains a package for building plugins that are compatible with the OpenAI API specification, along with plugins built on top of this package.

Package Overview

The compat_oai package provides a base implementation (OpenAICompatible) that handles:

  • Model and embedder registration
  • Message handling
  • Tool support
  • Configuration management

Usage Example

Here's how to implement a new OpenAI-compatible plugin:

type MyPlugin struct {
    compat_oai.OpenAICompatible
    // define other plugin-specific fields
}

var (
    supportedModels = map[string]ai.ModelInfo{
        // define supported models
    }
)

// Implement required methods
func (p *MyPlugin) Init(ctx context.Context, g *genkit.Genkit) error {
    // initialize the plugin with the common compatible package
    if err := p.OpenAICompatible.Init(ctx, g); err != nil {
        return err
    }

    // Define plugin-specific models
    for model, info := range supportedModels {
        if _, err := p.DefineModel(g, p.Provider, model, info); err != nil {
            return err
        }
    }

    // Define embedders, if applicable

    return nil
}

func (p *MyPlugin) Name() string {
    return p.Provider
}

See the openai and anthropic directories for complete implementations.

Running Tests

Set your API keys:

export OPENAI_API_KEY=<your-openai-key>
export ANTHROPIC_API_KEY=<your-anthropic-key>

Run all tests:

go test -v ./...

Run specific plugin tests:

# OpenAI tests
go test -v ./openai

# Anthropic tests
go test -v ./anthropic

Note: Tests will be skipped if the required API keys are not set.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BasicText describes model capabilities for text-only GPT models.
	BasicText = ai.ModelSupports{
		Multiturn:  true,
		Tools:      true,
		SystemRole: true,
		Media:      false,
	}

	// Multimodal describes model capabilities for multimodal GPT models.
	Multimodal = ai.ModelSupports{
		Multiturn:  true,
		Tools:      true,
		SystemRole: true,
		Media:      true,
		ToolChoice: true,
	}
)

Functions

This section is empty.

Types

type ModelGenerator

type ModelGenerator struct {
	// contains filtered or unexported fields
}

ModelGenerator handles OpenAI generation requests

func NewModelGenerator

func NewModelGenerator(client *openai.Client, modelName string) *ModelGenerator

NewModelGenerator creates a new ModelGenerator instance

func (*ModelGenerator) Generate

func (g *ModelGenerator) Generate(ctx context.Context, req *ai.ModelRequest, handleChunk func(context.Context, *ai.ModelResponseChunk) error) (*ai.ModelResponse, error)

Generate executes the generation request

func (*ModelGenerator) GetRequest

func (*ModelGenerator) WithConfig

func (g *ModelGenerator) WithConfig(config any) *ModelGenerator

WithConfig adds configuration parameters from the model request see https://platform.openai.com/docs/api-reference/responses/create for more details on openai's request fields

func (*ModelGenerator) WithMessages

func (g *ModelGenerator) WithMessages(messages []*ai.Message) *ModelGenerator

WithMessages adds messages to the request

func (*ModelGenerator) WithTools

func (g *ModelGenerator) WithTools(tools []*ai.ToolDefinition) *ModelGenerator

WithTools adds tools to the request

type OpenAICompatible

type OpenAICompatible struct {

	// Opts contains request options for the OpenAI client.
	// Required: Must include at least WithAPIKey for authentication.
	// Optional: Can include other options like WithOrganization, WithBaseURL, etc.
	Opts []option.RequestOption

	// Provider is a unique identifier for the plugin.
	// This will be used as a prefix for model names (e.g., "myprovider/model-name").
	// Should be lowercase and match the plugin's Name() method.
	Provider string

	// API key to use with the desired plugin.
	APIKey string

	// Base URL to use for custom endpoints.
	// This should be used if you are running through a proxy or
	// using a non-official endpoint
	BaseURL string
	// contains filtered or unexported fields
}

OpenAICompatible is a plugin that provides compatibility with OpenAI's Compatible APIs. It allows defining models and embedders that can be used with Genkit.

func (*OpenAICompatible) DefineEmbedder

func (o *OpenAICompatible) DefineEmbedder(provider, name string, embedOpts *ai.EmbedderOptions) ai.Embedder

DefineEmbedder defines an embedder with a given name.

func (*OpenAICompatible) DefineModel

func (o *OpenAICompatible) DefineModel(provider, id string, opts ai.ModelOptions) ai.Model

DefineModel defines a model in the registry

func (*OpenAICompatible) Embedder

func (o *OpenAICompatible) Embedder(g *genkit.Genkit, name string) ai.Embedder

Embedder returns the ai.Embedder with the given name. It returns nil if the embedder was not defined.

func (*OpenAICompatible) Init

func (o *OpenAICompatible) Init(ctx context.Context) []api.Action

Init implements genkit.Plugin.

func (*OpenAICompatible) IsDefinedEmbedder

func (o *OpenAICompatible) IsDefinedEmbedder(g *genkit.Genkit, name string) bool

IsDefinedEmbedder reports whether the named [Embedder] is defined by this plugin.

func (*OpenAICompatible) IsDefinedModel

func (o *OpenAICompatible) IsDefinedModel(g *genkit.Genkit, name string) bool

IsDefinedModel reports whether the named [Model] is defined by this plugin.

func (*OpenAICompatible) ListActions added in v0.6.1

func (o *OpenAICompatible) ListActions(ctx context.Context) []api.ActionDesc

func (*OpenAICompatible) Model

func (o *OpenAICompatible) Model(g *genkit.Genkit, name string) ai.Model

Model returns the ai.Model with the given name. It returns nil if the model was not defined.

func (*OpenAICompatible) Name

func (o *OpenAICompatible) Name() string

Name implements genkit.Plugin.

func (*OpenAICompatible) ResolveAction added in v0.6.1

func (o *OpenAICompatible) ResolveAction(atype api.ActionType, name string) api.Action

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL