openai

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 18 Imported by: 5

Documentation

Overview

Package openai provides a wrapper around the OpenAI API.

Index

Constants

View Source
const (
	DefaultOpenAIMaxTokens   = 256
	DefaultOpenAITemperature = 0.7
	DefaultOpenAINumResults  = 1
	DefaultOpenAITopP        = 1.0
	DefaultMaxIterations     = 3
)
View Source
const (
	EOS = "\x00"
)

Variables

View Source
var (
	ErrOpenAICompletion = fmt.Errorf("openai completion error")
	ErrOpenAIChat       = fmt.Errorf("openai chat error")
)

Functions

This section is empty.

Types

type Function added in v0.0.8

type Function struct {
	Name        string
	Description string
	Parameters  map[string]interface{}
	Fn          interface{}
}

type FunctionParameterOption added in v0.0.8

type FunctionParameterOption func(map[string]interface{}) error

type Legacy added in v0.1.0

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

func NewChat added in v0.0.5

func NewChat() *Legacy

func NewCompletion added in v0.0.5

func NewCompletion() *Legacy

func NewLegacy added in v0.1.0

func NewLegacy(model Model, temperature float32, maxTokens int, verbose bool) *Legacy

func (*Legacy) BatchCompletion added in v0.1.0

func (o *Legacy) BatchCompletion(ctx context.Context, prompts []string) ([]string, error)

BatchCompletion returns multiple completions for the given prompts.

func (*Legacy) BatchCompletionStream added in v0.1.0

func (o *Legacy) BatchCompletionStream(ctx context.Context, callbackFn []StreamCallback, prompts []string) error

BatchCompletionStream returns multiple completion streams for the given prompts.

func (*Legacy) BindFunction added in v0.1.0

func (o *Legacy) BindFunction(
	fn interface{},
	name string,
	description string,
	functionParameterOptions ...FunctionParameterOption,
) error

func (*Legacy) CalledFunctionName added in v0.1.0

func (o *Legacy) CalledFunctionName() *string

CalledFunctionName returns the name of the function that was called.

func (*Legacy) Chat added in v0.1.0

func (o *Legacy) Chat(ctx context.Context, prompt *chat.Chat) (string, error)

Chat returns a single chat completion for the given prompt.

func (*Legacy) ChatStream added in v0.1.0

func (o *Legacy) ChatStream(ctx context.Context, callbackFn StreamCallback, prompt *chat.Chat) error

ChatStream returns a single chat stream for the given prompt.

func (*Legacy) Completion added in v0.1.0

func (o *Legacy) Completion(ctx context.Context, prompt string) (string, error)

Completion returns a single completion for the given prompt.

func (*Legacy) CompletionStream added in v0.1.0

func (o *Legacy) CompletionStream(ctx context.Context, callbackFn StreamCallback, prompt string) error

CompletionStream returns a single completion stream for the given prompt.

func (*Legacy) FinishReason added in v0.1.0

func (o *Legacy) FinishReason() string

FinishReason returns the LLM finish reason.

func (*Legacy) SetStop added in v0.1.0

func (o *Legacy) SetStop(stop []string)

SetStop sets the stop sequences for the completion.

func (*Legacy) WithCallback added in v0.1.0

func (o *Legacy) WithCallback(callback UsageCallback) *Legacy

WithUsageCallback sets the usage callback to use for the OpenAI instance.

func (*Legacy) WithClient added in v0.1.0

func (o *Legacy) WithClient(client *openai.Client) *Legacy

WithClient sets the client to use for the OpenAI instance.

func (*Legacy) WithCompletionCache added in v0.1.0

func (o *Legacy) WithCompletionCache(cache *cache.Cache) *Legacy

WithCache sets the cache to use for the OpenAI instance.

func (*Legacy) WithMaxTokens added in v0.1.0

func (o *Legacy) WithMaxTokens(maxTokens int) *Legacy

WithMaxTokens sets the max tokens to use for the OpenAI instance.

func (*Legacy) WithModel added in v0.1.0

func (o *Legacy) WithModel(model Model) *Legacy

WithModel sets the model to use for the OpenAI instance.

func (*Legacy) WithStop added in v0.1.0

func (o *Legacy) WithStop(stop []string) *Legacy

WithStop sets the stop sequences to use for the OpenAI instance.

func (*Legacy) WithTemperature added in v0.1.0

func (o *Legacy) WithTemperature(temperature float32) *Legacy

WithTemperature sets the temperature to use for the OpenAI instance.

func (*Legacy) WithVerbose added in v0.1.0

func (o *Legacy) WithVerbose(verbose bool) *Legacy

WithVerbose sets the verbose flag to use for the OpenAI instance.

type Model

type Model string
const (
	GPT432K0613           Model = openai.GPT432K0613
	GPT432K0314           Model = openai.GPT432K0314
	GPT432K               Model = openai.GPT432K
	GPT40613              Model = openai.GPT40613
	GPT40314              Model = openai.GPT40314
	GPT4o                 Model = openai.GPT4o
	GPT4o20240513         Model = openai.GPT4o20240513
	GPT4Turbo             Model = openai.GPT4Turbo
	GPT4Turbo20240409     Model = openai.GPT4Turbo20240409
	GPT4Turbo0125         Model = openai.GPT4Turbo0125
	GPT4Turbo1106         Model = openai.GPT4Turbo1106
	GPT4TurboPreview      Model = openai.GPT4TurboPreview
	GPT4VisionPreview     Model = openai.GPT4VisionPreview
	GPT4                  Model = openai.GPT4
	GPT3Dot5Turbo0125     Model = openai.GPT3Dot5Turbo0125
	GPT3Dot5Turbo1106     Model = openai.GPT3Dot5Turbo1106
	GPT3Dot5Turbo0613     Model = openai.GPT3Dot5Turbo0613
	GPT3Dot5Turbo0301     Model = openai.GPT3Dot5Turbo0301
	GPT3Dot5Turbo16K      Model = openai.GPT3Dot5Turbo16K
	GPT3Dot5Turbo16K0613  Model = openai.GPT3Dot5Turbo16K0613
	GPT3Dot5Turbo         Model = openai.GPT3Dot5Turbo
	GPT3Dot5TurboInstruct Model = openai.GPT3Dot5TurboInstruct
	GPT3Davinci           Model = openai.GPT3Davinci
	GPT3Davinci002        Model = openai.GPT3Davinci002
	GPT3Curie             Model = openai.GPT3Curie
	GPT3Curie002          Model = openai.GPT3Curie002
	GPT3Ada               Model = openai.GPT3Ada
	GPT3Ada002            Model = openai.GPT3Ada002
	GPT3Babbage           Model = openai.GPT3Babbage
	GPT3Babbage002        Model = openai.GPT3Babbage002
)

type OpenAI

type OpenAI struct {
	Name string
	// contains filtered or unexported fields
}

func New

func New() *OpenAI

func (*OpenAI) BindFunction added in v0.0.9

func (o *OpenAI) BindFunction(
	fn interface{},
	name string,
	description string,
	functionParameterOptions ...FunctionParameterOption,
) error

func (*OpenAI) Generate added in v0.1.0

func (o *OpenAI) Generate(ctx context.Context, t *thread.Thread) error

func (*OpenAI) SetStop added in v0.0.9

func (o *OpenAI) SetStop(stop []string)

SetStop sets the stop sequences for the completion.

func (*OpenAI) WithCache added in v0.1.0

func (o *OpenAI) WithCache(cache *cache.Cache) *OpenAI

func (*OpenAI) WithClient added in v0.0.9

func (o *OpenAI) WithClient(client *openai.Client) *OpenAI

WithClient sets the client to use for the OpenAI instance.

func (*OpenAI) WithMaxTokens added in v0.0.9

func (o *OpenAI) WithMaxTokens(maxTokens int) *OpenAI

WithMaxTokens sets the max tokens to use for the OpenAI instance.

func (*OpenAI) WithModel added in v0.0.9

func (o *OpenAI) WithModel(model Model) *OpenAI

WithModel sets the model to use for the OpenAI instance.

func (*OpenAI) WithResponseFormat added in v0.2.0

func (o *OpenAI) WithResponseFormat(responseFormat ResponseFormat) *OpenAI

func (*OpenAI) WithStop added in v0.0.9

func (o *OpenAI) WithStop(stop []string) *OpenAI

WithStop sets the stop sequences to use for the OpenAI instance.

func (*OpenAI) WithStream added in v0.1.0

func (o *OpenAI) WithStream(enable bool, callbackFn StreamCallback) *OpenAI

func (*OpenAI) WithTemperature added in v0.0.9

func (o *OpenAI) WithTemperature(temperature float32) *OpenAI

WithTemperature sets the temperature to use for the OpenAI instance.

func (*OpenAI) WithToolChoice added in v0.1.0

func (o *OpenAI) WithToolChoice(toolChoice *string) *OpenAI

func (*OpenAI) WithTools added in v0.3.0

func (o *OpenAI) WithTools(tools ...Tool) *OpenAI

func (*OpenAI) WithUsageCallback added in v0.1.0

func (o *OpenAI) WithUsageCallback(callback UsageCallback) *OpenAI

WithUsageCallback sets the usage callback to use for the OpenAI instance.

type ResponseFormat added in v0.2.0

type ResponseFormat = openai.ChatCompletionResponseFormatType
const (
	ResponseFormatJSONObject ResponseFormat = openai.ChatCompletionResponseFormatTypeJSONObject
	ResponseFormatText       ResponseFormat = openai.ChatCompletionResponseFormatTypeText
)

type StreamCallback added in v0.0.11

type StreamCallback func(string)

type Tool added in v0.3.0

type Tool interface {
	Description() string
	Name() string
	Fn() any
}

type UsageCallback added in v0.0.11

type UsageCallback func(types.Meta)

Jump to

Keyboard shortcuts

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