openai

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultModel is the default AI model used for generating text when no model
	// is explicitly provided.
	DefaultModel = openai.GPT3Dot5Turbo

	// DefaultMaxTokens is the default maximum number of tokens to be generated by
	// the Service instance when no specific value is provided.
	DefaultMaxTokens = 512
)

Variables

This section is empty.

Functions

func ChatTokens added in v0.0.4

func ChatTokens(model string, messages []openai.ChatCompletionMessage) (int, error)

ChatTokens computes the total number of tokens in a list of ChatCompletionMessages for a given model. It returns an integer representing the total token count and an error if any issues are encountered during tokenization or if the model is unsupported.

func MaxTokensForModel added in v0.0.4

func MaxTokensForModel(model string) int

MaxTokensForModel returns the maximum number of tokens allowed for the specified model. If the model is not recognized, it returns a default value.

func PromptTokens added in v0.0.4

func PromptTokens(model string, prompt string) (int, error)

PromptTokens computes the number of tokens in the given prompt string for the specified model. It returns the token count and any error encountered during tokenization.

Types

type Option

type Option func(*Service)

Option is a function type used to configure a Service instance. It takes a pointer to a Service and modifies its fields as needed. This allows for flexible and customizable configuration of the Service through the use of various Option functions.

func Client added in v0.0.4

func Client(c *openai.Client) Option

Client sets the OpenAI client for the service to use when making API requests. It is an option function for configuring a new Service instance.

func MaxTokens

func MaxTokens(max int) Option

MaxTokens sets the maximum number of tokens to be generated by the Service instance. It is an option function for configuring a new Service instance.

func Model

func Model(model string) Option

Model is an Option function that sets the model string for the Service struct. It returns an Option that modifies the model field of a given Service instance.

func WithLogger

func WithLogger(h slog.Handler) Option

WithLogger sets the logger for the Service instance using the provided slog.Handler.

type Service

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

Service is a struct that represents an OpenAI service, providing functionality to generate documents using the OpenAI API. It contains configurable options such as client, model, maxTokens, codec, and logger. It also provides methods to generate documents based on provided context and request configurations.

func New

func New(apiKey string, opts ...Option) (*Service, error)

New creates a new instance of the Service with the specified API key and options. It initializes the OpenAI client, sets the default model, and configures the tokenizer for the selected model. If no model is provided, it uses the default model.

func (*Service) GenerateDoc

func (svc *Service) GenerateDoc(ctx generate.Context) (string, error)

GenerateDoc generates a document using the specified generate.Context, invoking the OpenAI API with the appropriate model and options. The resulting document is returned as a string.

Jump to

Keyboard shortcuts

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