openai

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultModel          = "gpt-5"
	DefaultEmbeddingModel = "text-embedding-3-small"
)

Variables

This section is empty.

Functions

func NewHistory

func NewHistory(messages []openai.ChatCompletionMessage) (*gollem.History, error)

NewHistory creates gollem.History from OpenAI messages

func ToMessages

func ToMessages(h *gollem.History) ([]openai.ChatCompletionMessage, error)

ToMessages converts gollem.History to OpenAI messages

Types

type Client

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

Client is a client for the OpenAI API. It provides methods to interact with OpenAI's OpenAI models.

func New

func New(ctx context.Context, apiKey string, options ...Option) (*Client, error)

New creates a new client for the OpenAI API. It requires an API key and can be configured with additional options.

func (*Client) GenerateEmbedding

func (c *Client) GenerateEmbedding(ctx context.Context, dimension int, input []string) ([][]float64, error)

GenerateEmbedding generates embeddings for the given input text.

func (*Client) NewSession

func (c *Client) NewSession(ctx context.Context, options ...gollem.SessionOption) (gollem.Session, error)

NewSession creates a new session for the OpenAI API. It converts the provided tools to OpenAI's tool format and initializes a new chat session.

type Option

type Option func(*Client)

Option is a function that configures a Client.

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL sets the custom base URL for the OpenAI API. Allows usage with compatible endpoints, proxies, or self-hosted instances. If empty, uses the default OpenAI API endpoints. Reference: Brain Memory c4705651-435d-4cca-95eb-d39d1ea69a9c

func WithContentType

func WithContentType(contentType gollem.ContentType) Option

WithContentType sets the content type for text generation. This determines the format of the generated content.

func WithEmbeddingModel

func WithEmbeddingModel(modelName string) Option

WithEmbeddingModel sets the embedding model to use for embeddings. The model name should be a valid OpenAI model identifier. See default embedding model in DefaultEmbeddingModel. Model list is at https://platform.openai.com/docs/guides/embeddings#embedding-models

func WithFrequencyPenalty

func WithFrequencyPenalty(penalty float32) Option

WithFrequencyPenalty sets the frequency penalty parameter. Decreases the model's likelihood to repeat the same line verbatim.

func WithMaxTokens

func WithMaxTokens(maxTokens int) Option

WithMaxTokens sets the maximum number of tokens to generate.

func WithModel

func WithModel(modelName string) Option

WithModel sets the default model to use for chat completions. The model name should be a valid OpenAI model identifier. See default model in DefaultModel.

func WithPresencePenalty

func WithPresencePenalty(penalty float32) Option

WithPresencePenalty sets the presence penalty parameter. Increases the model's likelihood to talk about new topics.

func WithReasoningEffort

func WithReasoningEffort(effort string) Option

WithReasoningEffort sets the reasoning_effort parameter for GPT-5 models. Supported values (as of 2025-10-04): "minimal", "medium", "high".

func WithSystemPrompt

func WithSystemPrompt(prompt string) Option

WithSystemPrompt sets the system prompt to use for chat completions.

func WithTemperature

func WithTemperature(temp float32) Option

WithTemperature sets the temperature parameter for text generation. Higher values make the output more random, lower values make it more focused. Range: 0.0 to 1.0 Default: 0.7

func WithTopP

func WithTopP(topP float32) Option

WithTopP sets the top_p parameter for text generation. Controls diversity via nucleus sampling.

func WithVerbosity

func WithVerbosity(verbosity string) Option

WithVerbosity sets the verbosity parameter for GPT-5 models. Supported values (as of 2025-10-04): "low", "medium", "high".

type Session

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

Session is a session for the OpenAI chat. It maintains the conversation state and handles message generation.

func (*Session) AppendHistory

func (s *Session) AppendHistory(h *gollem.History) error

func (*Session) CountToken

func (s *Session) CountToken(ctx context.Context, input ...gollem.Input) (int, error)

CountToken calculates the total number of tokens for the given inputs, including system prompt, history messages, and new inputs. This uses tiktoken library for local token counting without API calls.

func (*Session) Generate

func (s *Session) Generate(ctx context.Context, input []gollem.Input, opts ...gollem.GenerateOption) (*gollem.Response, error)

Generate processes the input and generates a response with optional per-call overrides. It handles both text messages and function responses.

func (*Session) GenerateContent deprecated

func (s *Session) GenerateContent(ctx context.Context, input ...gollem.Input) (*gollem.Response, error)

Deprecated: GenerateContent is deprecated. Use Generate instead.

func (*Session) GenerateStream deprecated

func (s *Session) GenerateStream(ctx context.Context, input ...gollem.Input) (<-chan *gollem.Response, error)

Deprecated: GenerateStream is deprecated. Use Stream instead.

func (*Session) History

func (s *Session) History() (*gollem.History, error)

func (*Session) Stream

func (s *Session) Stream(ctx context.Context, input []gollem.Input, opts ...gollem.GenerateOption) (<-chan *gollem.Response, error)

Stream processes the input and generates a response stream with optional per-call overrides. It handles both text messages and function responses, and returns a channel for streaming responses.

Jump to

Keyboard shortcuts

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