openai

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: Apache-2.0 Imports: 8 Imported by: 2

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 (
  // ignoring Genkit imports
  oai "github.com/firebase/genkit/go/plugins/compat_oai/openai"
  "github.com/openai/openai-go"
)
// Initialize the OpenAI plugin with your API key
oai := &oai.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 := &openai.ChatCompletionNewParams{
    // 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 EmbedderRef added in v0.7.0

type EmbedderRef struct {
	Name         string
	ConfigSchema TextEmbeddingConfig // Represents the schema, can be used for default config
	Label        string
	Supports     *ai.EmbedderSupports
	Dimensions   int
}

EmbedderRef represents the main structure for an embedding model's definition.

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(id string, opts *ai.EmbedderOptions) ai.Embedder

func (*OpenAI) DefineModel

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

func (*OpenAI) Embedder

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

func (*OpenAI) Init

func (o *OpenAI) Init(ctx context.Context) []core.Action

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(atype core.ActionType, name string) core.Action

type TextEmbeddingConfig added in v0.7.0

type TextEmbeddingConfig struct {
	Dimensions     int                                       `json:"dimensions,omitempty"`
	EncodingFormat openaiGo.EmbeddingNewParamsEncodingFormat `json:"encodingFormat,omitempty"`
}

Jump to

Keyboard shortcuts

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