openai

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultModel is the default language model used for generating text when no
	// specific model is set during the client creation.
	DefaultModel = openai.GPT3Dot5Turbo

	// DefaultTemperature is the default value for the temperature parameter in the
	// AI model. It affects the randomness of the model's output.
	DefaultTemperature = 0.3

	// DefaultTopP is the default value for the "Top P" parameter used in OpenAI's
	// language models.
	DefaultTopP = 0.3

	// DefaultTimeout specifies the default duration to wait before timing out
	// requests to the OpenAI API. This value can be changed by using the Timeout
	// option when creating a new client.
	DefaultTimeout = 3 * time.Minute

	// DefaultChunkTimeout specifies the default duration for waiting on a chunk of
	// data during streaming operations before timing out. This value can be
	// adjusted to control how long the system will wait for a chunk before
	// considering the operation timed out.
	DefaultChunkTimeout = 5 * time.Second
)

Variables

This section is empty.

Functions

func ChatTokens

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 PromptTokens

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 Client

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

Client is a configurable interface to the OpenAI API. It allows for the generation of text completions using various models, with adjustable parameters for token count, temperature, and topP. A specified timeout can be set for API requests.

func New

func New(apiToken string, opts ...Option) *Client

New creates a new Client instance with the specified API token and optional configuration options. The Client allows for the generation of text completions using various models, with adjustable parameters for token count, temperature, and topP. The default values for these parameters are used if not explicitly set. The Client also supports setting a timeout duration for API requests.

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, prompt string) (string, error)

Chat is a method of the Client type that generates a text completion based on the provided prompt. The generated text completion is returned as a string.

type Option

type Option func(*Client)

Option is a function type used to configure a Client. It allows for setting various parameters such as the model, maximum tokens, temperature, topP, and timeout. These options are applied to a Client instance during its creation with the New function.

func ChunkTimeout added in v0.9.2

func ChunkTimeout(timeout time.Duration) Option

ChunkTimeout sets the maximum duration a Client should wait for a chunk of data during streaming operations before timing out. This is configured as an Option that modifies the chunkTimeout field of a Client instance.

func MaxTokens

func MaxTokens(maxTokens int) Option

MaxTokens configures the maximum number of tokens that the Client can use for generating text completions. It accepts an integer value and returns an Option to modify a Client instance.

func Model

func Model(model string) Option

Model is a function that returns an Option which sets the model string field of a Client object when called. The model string represents the specific OpenAI model to be used for text generation tasks.

func ResponseFormat added in v0.6.3

func ResponseFormat[Format string | openai.ChatCompletionResponseFormatType](format Format) Option

ResponseFormat configures the format of the response received from the OpenAI API when generating text completions. It specifies how the response should be structured, which can be either plain text or a structured format that includes additional metadata. This option is passed to a Client instance during its creation and influences how the Client processes and returns generated content. The function accepts format types that can be either a string or an openai.ChatCompletionResponseFormatType.

func Stream added in v0.5.5

func Stream(stream io.Writer) Option

Stream is an option function that sets the writer to which the generated text completions will be streamed. This allows for real-time processing and display of the generated text.

func Temperature

func Temperature(temperature float32) Option

Temperature sets the temperature parameter for the Client. The temperature affects the randomness of the model's output during text generation tasks.

func Timeout

func Timeout(timeout time.Duration) Option

Timeout is a function that sets the timeout duration for the Client. It returns an Option that, when provided to the New function, modifies the timeout duration of the created Client instance. The timeout duration determines how long the Client waits for a response before it cancels the request.

func TopP

func TopP(topP float32) Option

TopP sets the topP parameter for the Client.

func Verbose

func Verbose(verbose bool) Option

Verbose sets the verbosity level of the Client instance. If set to true, debug logs will be printed during API requests.

Jump to

Keyboard shortcuts

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