compat_oai

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: May 13, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

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.ModelInfo{
		Supports: &ai.ModelSupports{
			Multiturn:  true,
			Tools:      true,
			SystemRole: true,
			Media:      false,
		},
	}

	// Multimodal describes model capabilities for multimodal GPT models.
	Multimodal = ai.ModelInfo{
		Supports: &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, handleChunk func(context.Context, *ai.ModelResponseChunk) error) (*ai.ModelResponse, error)

Generate executes the generation request

func (*ModelGenerator) GetRequest

func (g *ModelGenerator) GetRequest() *openai.ChatCompletionNewParams

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, choice ai.ToolChoice) *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
	// 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(g *genkit.Genkit, provider, name string) (ai.Embedder, error)

DefineEmbedder defines an embedder with a given name.

func (*OpenAICompatible) DefineModel

func (o *OpenAICompatible) DefineModel(g *genkit.Genkit, provider, name string, info ai.ModelInfo) (ai.Model, error)

DefineModel defines a model in the registry

func (*OpenAICompatible) Embedder

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

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

func (*OpenAICompatible) Init

Init implements genkit.Plugin.

func (*OpenAICompatible) IsDefinedEmbedder

func (o *OpenAICompatible) IsDefinedEmbedder(g *genkit.Genkit, name string, provider 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, provider string) bool

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

func (*OpenAICompatible) Model

func (o *OpenAICompatible) Model(g *genkit.Genkit, name string, provider 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.

type OpenAIConfig

type OpenAIConfig struct {
	// Maximum number of tokens to generate
	MaxOutputTokens int `json:"max_output_tokens,omitempty"`
	// Temperature for sampling
	Temperature float64 `json:"temperature,omitempty"`
	// Top-p value for nucleus sampling
	TopP float64 `json:"top_p,omitempty"`
	// List of sequences where the model will stop generating
	StopSequences []string `json:"stop_sequences,omitempty"`
}

OpenaiConfig mirrors the OpenAI API configuration fields

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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