openai

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

OpenAI Plugin

This plugin provides a simple interface for using OpenAI's services.

Prerequisites

  • Go installed on your system
  • An OpenAI API key

Usage

Here's a simple example of how to use the OpenAI plugin:

// import "github.com/firebase/genkit/go/plugins/compat_oai/openai"
// Initialize the OpenAI plugin with your API key
oai := &openai.OpenAI{APIKey: apiKey,}

// Initialize Genkit with the OpenAI plugin
g, err := genkit.Init(ctx,
    genkit.WithDefaultModel("openai/gpt-4o-mini"),
    genkit.WithPlugins(oai),
)
if err != nil {
    // handle errors
}

config := &ai.GenerationCommonConfig{
    // define optional config fields
}

resp, err = genkit.Generate(ctx, g,
    ai.WithPromptText("Write a short sentence about artificial intelligence."),
    ai.WithConfig(config),
)

Running Tests

First, set your OpenAI API key as an environment variable:

export OPENAI_API_KEY=<your-api-key>
Running All Tests

To run all tests in the directory:

go test -v .
Running Tests from Specific Files

To run tests from a specific file:

# Run only generate_live_test.go tests
go test -run "^TestGenerator"

# Run only openai_live_test.go tests
go test -run "^TestPlugin"
Running Individual Tests

To run a specific test case:

# Run only the streaming test from openai_live_test.go
go test -run "TestPlugin/streaming"

# Run only the Complete test from generate_live_test.go
go test -run "TestGenerator_Complete"

# Run only the Stream test from generate_live_test.go
go test -run "TestGenerator_Stream"
Test Output Verbosity

Add the -v flag for verbose output:

go test -v -run "TestPlugin/streaming"

Note: All live tests require the OPENAI_API_KEY environment variable to be set. Tests will be skipped if the API key is not provided.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenAI

type OpenAI struct {
	// APIKey is the API key for the OpenAI API. If empty, the values of the environment variable "OPENAI_API_KEY" will be consulted.
	// Request a key at https://platform.openai.com/api-keys
	APIKey string
	// Optional: Opts are additional options for the OpenAI client.
	// Can include other options like WithOrganization, WithBaseURL, etc.
	Opts []option.RequestOption
	// contains filtered or unexported fields
}

func (*OpenAI) DefineEmbedder

func (o *OpenAI) DefineEmbedder(g *genkit.Genkit, name string) (ai.Embedder, error)

func (*OpenAI) DefineModel

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

func (*OpenAI) Embedder

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

func (*OpenAI) Init

func (o *OpenAI) Init(ctx context.Context, g *genkit.Genkit) error

Init implements genkit.Plugin.

func (*OpenAI) ListActions added in v0.6.1

func (o *OpenAI) ListActions(ctx context.Context) []core.ActionDesc

func (*OpenAI) Model

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

func (*OpenAI) Name

func (o *OpenAI) Name() string

Name implements genkit.Plugin.

func (*OpenAI) ResolveAction added in v0.6.1

func (o *OpenAI) ResolveAction(g *genkit.Genkit, atype core.ActionType, name string) error

Jump to

Keyboard shortcuts

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