openai

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleSystem    = "system"
	RoleAssistant = "assistant"
	RoleUser      = "user"
	RoleFunction  = "function"
)
View Source
const (
	DefaultAPIVersion = "2023-05-15"
)

Variables

View Source
var (
	ErrEmptyResponse              = errors.New("no response")
	ErrMissingToken               = errors.New("missing the OpenAI API key, set it in the OPENAI_API_KEY environment variable") //nolint:lll
	ErrMissingAzureEmbeddingModel = errors.New("embeddings model needs to be provided when using Azure API")

	ErrUnexpectedResponseLength = errors.New("unexpected length of response")
)

Functions

This section is empty.

Types

type APIType

type APIType openaiclient.APIType

type Chat

type Chat struct {
	CallbacksHandler callbacks.Handler
	// contains filtered or unexported fields
}

func NewChat

func NewChat(opts ...Option) (*Chat, error)

NewChat returns a new OpenAI chat LLM.

func (*Chat) Call

func (o *Chat) Call(ctx context.Context, messages []schema.ChatMessage, options ...llms.CallOption) (*schema.AIChatMessage, error)

Call requests a chat response for the given messages.

func (*Chat) CreateEmbedding

func (o *Chat) CreateEmbedding(ctx context.Context, inputTexts []string) ([][]float64, error)

CreateEmbedding creates embeddings for the given input texts.

func (*Chat) Generate

func (o *Chat) Generate(ctx context.Context, messageSets [][]schema.ChatMessage, options ...llms.CallOption) ([]*llms.Generation, error)

func (*Chat) GeneratePrompt

func (o *Chat) GeneratePrompt(ctx context.Context, promptValues []schema.PromptValue, options ...llms.CallOption) (llms.LLMResult, error)

func (*Chat) GetNumTokens

func (o *Chat) GetNumTokens(text string) int

type ChatMessage

type ChatMessage = openaiclient.ChatMessage

type LLM

type LLM struct {
	CallbacksHandler callbacks.Handler
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (*LLM, error)

New returns a new OpenAI LLM.

func (*LLM) Call

func (o *LLM) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error)

Call requests a completion for the given prompt.

func (*LLM) CreateEmbedding

func (o *LLM) CreateEmbedding(ctx context.Context, inputTexts []string) ([][]float64, error)

CreateEmbedding creates embeddings for the given input texts.

func (*LLM) Generate

func (o *LLM) Generate(ctx context.Context, prompts []string, options ...llms.CallOption) ([]*llms.Generation, error)

func (*LLM) GeneratePrompt

func (o *LLM) GeneratePrompt(ctx context.Context, promptValues []schema.PromptValue, options ...llms.CallOption) (llms.LLMResult, error)

func (*LLM) GetNumTokens

func (o *LLM) GetNumTokens(text string) int

type Option

type Option func(*options)

func WithAPIType

func WithAPIType(apiType APIType) Option

WithAPIType passes the api type to the client. If not set, the default value is APITypeOpenAI.

func WithAPIVersion

func WithAPIVersion(apiVersion string) Option

WithAPIVersion passes the api version to the client. If not set, the default value is DefaultAPIVersion.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL passes the OpenAI base url to the client. If not set, the base url is read from the OPENAI_BASE_URL environment variable. If still not set in ENV VAR OPENAI_BASE_URL, then the default value is https://api.openai.com/v1 is used.

func WithEmbeddingModel

func WithEmbeddingModel(embeddingModel string) Option

WithEmbeddingModel passes the OpenAI model to the client. Required when ApiType is Azure.

func WithHTTPClient

func WithHTTPClient(client openaiclient.Doer) Option

WithHTTPClient allows setting a custom HTTP client. If not set, the default value is http.DefaultClient.

func WithModel

func WithModel(model string) Option

WithModel passes the OpenAI model to the client. If not set, the model is read from the OPENAI_MODEL environment variable.

func WithOrganization

func WithOrganization(organization string) Option

WithOrganization passes the OpenAI organization to the client. If not set, the organization is read from the OPENAI_ORGANIZATION.

func WithToken

func WithToken(token string) Option

WithToken passes the OpenAI API token to the client. If not set, the token is read from the OPENAI_API_KEY environment variable.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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