llms

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

https://github.com/sashabaranov/go-openai/blob/master/chat.go

Index

Constants

View Source
const (
	ChatMessageRoleSystem    = "system"
	ChatMessageRoleUser      = "user"
	ChatMessageRoleAssistant = "assistant"
	ChatMessageRoleFunction  = "function"
	ChatMessageRoleTool      = "tool"
	ChatMessageRoleDeveloper = "developer"
)

Chat message role defined by the OpenAI API.

Variables

View Source
var (
	ErrContentFieldsMisused = errors.New("can't use both Content and MultiContent properties simultaneously")
)

Functions

func IsModelCacheValid added in v0.2.0

func IsModelCacheValid(cache ModelCache) bool

IsValid checks if the cache is valid (less than 1 hour old)

func SaveModelCache added in v0.2.0

func SaveModelCache(providerName string, cache ModelCache) error

Save saves the model cache to the file

func StreamingSSEResponse added in v0.1.3

func StreamingSSEResponse(respBody io.ReadCloser, streamingFunc func(content StreamingChatCompletionResponse))

StreamingSSEResponse handles streaming responses from OpenAI's API. It reads the response body line by line, processes each chunk of data, and calls the provided streaming function with the processed content. respBody: The response body from the HTTP request streamingFunc: The callback function to handle each chunk of streaming data

Types

type ChatCompletionChoice

type ChatCompletionChoice struct {
	Index int `json:"index"`
	// Message for non-streaming API
	Message *ChatCompletionMessage `json:"message"`
	// Delta for streaming API
	Delta *ChatCompletionMessage `json:"delta"`
	// FinishReason
	// stop: API returned complete message,
	// or a message terminated by one of the stop sequences provided via the stop parameter
	// length: Incomplete model output due to max_tokens parameter or token limit
	// function_call: The model decided to call a function
	// content_filter: Omitted content due to a flag from our content filters
	// null: API response still in progress or incomplete
	FinishReason         FinishReason         `json:"finish_reason"`
	LogProbs             *LogProbs            `json:"logprobs,omitempty"`
	ContentFilterResults ContentFilterResults `json:"content_filter_results"`
}

type ChatCompletionMessage

type ChatCompletionMessage struct {
	Role         string `json:"role"`
	Content      string `json:"content,omitempty"`
	Refusal      string `json:"refusal,omitempty"`
	MultiContent []ChatMessagePart

	// This property isn't in the official documentation, but it's in
	// the documentation for the official library for python:
	// - https://github.com/openai/openai-python/blob/main/chatml.md
	// - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
	Name string `json:"name,omitempty"`

	FunctionCall *FunctionCall `json:"function_call,omitempty"`

	// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`

	// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
	ToolCallID string `json:"tool_call_id,omitempty"`
}

func (ChatCompletionMessage) MarshalJSON

func (m ChatCompletionMessage) MarshalJSON() ([]byte, error)

func (*ChatCompletionMessage) UnmarshalJSON

func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model    string                  `json:"model"`
	Messages []ChatCompletionMessage `json:"messages"`
	// MaxTokens The maximum number of tokens that can be generated in the chat completion.
	// This value can be used to control costs for text generated via API.
	// This value is now deprecated in favor of max_completion_tokens, and is not compatible with o1 series models.
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat-create-max_tokens
	MaxTokens int `json:"max_tokens,omitempty"`
	// MaxCompletionTokens An upper bound for the number of tokens that can be generated for a completion,
	// including visible output tokens and reasoning tokens https://platform.openai.com/docs/guides/reasoning
	MaxCompletionTokens int                           `json:"max_completion_tokens,omitempty"`
	Temperature         float32                       `json:"temperature,omitempty"`
	TopP                float32                       `json:"top_p,omitempty"`
	N                   int                           `json:"n,omitempty"`
	Stream              bool                          `json:"stream,omitempty"`
	Stop                []string                      `json:"stop,omitempty"`
	PresencePenalty     float32                       `json:"presence_penalty,omitempty"`
	ResponseFormat      *ChatCompletionResponseFormat `json:"response_format,omitempty"`
	Seed                *int                          `json:"seed,omitempty"`
	FrequencyPenalty    float32                       `json:"frequency_penalty,omitempty"`
	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
	LogitBias map[string]int `json:"logit_bias,omitempty"`
	// LogProbs indicates whether to return log probabilities of the output tokens or not.
	// If true, returns the log probabilities of each output token returned in the content of message.
	// This option is currently not available on the gpt-4-vision-preview model.
	LogProbs bool `json:"logprobs,omitempty"`
	// TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each
	// token position, each with an associated log probability.
	// logprobs must be set to true if this parameter is used.
	TopLogProbs int    `json:"top_logprobs,omitempty"`
	User        string `json:"user,omitempty"`
	// Deprecated: use Tools instead.
	Functions []FunctionDefinition `json:"functions,omitempty"`
	// Deprecated: use ToolChoice instead.
	FunctionCall any    `json:"function_call,omitempty"`
	Tools        []Tool `json:"tools,omitempty"`
	// This can be either a string or an ToolChoice object.
	ToolChoice any `json:"tool_choice,omitempty"`
	// Options for streaming response. Only set this when you set stream: true.
	StreamOptions *StreamOptions `json:"stream_options,omitempty"`
	// Disable the default behavior of parallel tool calls by setting it: false.
	ParallelToolCalls any `json:"parallel_tool_calls,omitempty"`
	// Store can be set to true to store the output of this completion request for use in distillations and evals.
	// https://platform.openai.com/docs/api-reference/chat/create#chat-create-store
	Store bool `json:"store,omitempty"`
	// Controls effort on reasoning for reasoning models. It can be set to "low", "medium", or "high".
	ReasoningEffort string `json:"reasoning_effort,omitempty"`
	// Metadata to store with the completion.
	Metadata map[string]string `json:"metadata,omitempty"`

	// ExtraHeaders are headers that will be sent with every request.
	ExtraHeaders map[string]string `json:"-,omitempty"`
}

ChatCompletionRequest represents a request structure for chat completion API.

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID                string                 `json:"id"`
	Choices           []ChatCompletionChoice `json:"choices"`
	Created           int64                  `json:"created"`
	Model             string                 `json:"model"`
	ServiceTier       string                 `json:"service_tier"`
	SystemFingerprint string                 `json:"system_fingerprint"`
	Object            string                 `json:"object"`
	Usage             Usage                  `json:"usage"`
}

ChatCompletionResponse represents a response structure for chat completion API.

type ChatCompletionResponseFormat

type ChatCompletionResponseFormat struct {
	Type       ChatCompletionResponseFormatType        `json:"type,omitempty"`
	JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"`
}

type ChatCompletionResponseFormatJSONSchema

type ChatCompletionResponseFormatJSONSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Schema      json.Marshaler `json:"schema"`
	Strict      bool           `json:"strict"`
}

type ChatCompletionResponseFormatType

type ChatCompletionResponseFormatType string
const (
	ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object"
	ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema"
	ChatCompletionResponseFormatTypeText       ChatCompletionResponseFormatType = "text"
)

type ChatMessageImageURL

type ChatMessageImageURL struct {
	URL    string         `json:"url,omitempty"`
	Detail ImageURLDetail `json:"detail,omitempty"`
}

type ChatMessagePart

type ChatMessagePart struct {
	Type     ChatMessagePartType  `json:"type,omitempty"`
	Text     string               `json:"text,omitempty"`
	ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
}

type ChatMessagePartType

type ChatMessagePartType string
const (
	ChatMessagePartTypeText     ChatMessagePartType = "text"
	ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
)

type CompletionTokensDetails

type CompletionTokensDetails struct {
	AudioTokens     int `json:"audio_tokens"`
	ReasoningTokens int `json:"reasoning_tokens"`
}

CompletionTokensDetails Breakdown of tokens used in a completion.

type ContentFilterResults

type ContentFilterResults struct {
	Hate      Hate      `json:"hate,omitempty"`
	SelfHarm  SelfHarm  `json:"self_harm,omitempty"`
	Sexual    Sexual    `json:"sexual,omitempty"`
	Violence  Violence  `json:"violence,omitempty"`
	JailBreak JailBreak `json:"jailbreak,omitempty"`
	Profanity Profanity `json:"profanity,omitempty"`
}

type FinishReason

type FinishReason string
const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonLength        FinishReason = "length"
	FinishReasonFunctionCall  FinishReason = "function_call"
	FinishReasonToolCalls     FinishReason = "tool_calls"
	FinishReasonContentFilter FinishReason = "content_filter"
	FinishReasonNull          FinishReason = "null"
)

func (FinishReason) MarshalJSON

func (r FinishReason) MarshalJSON() ([]byte, error)

type FunctionCall

type FunctionCall struct {
	Name string `json:"name,omitempty"`
	// call function with arguments in JSON format
	Arguments string `json:"arguments,omitempty"`
}

type FunctionDefine deprecated

type FunctionDefine = FunctionDefinition

Deprecated: use FunctionDefinition instead.

type FunctionDefinition

type FunctionDefinition struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Strict      bool   `json:"strict,omitempty"`
	// Parameters is an object describing the function.
	// You can pass json.RawMessage to describe the schema,
	// or you can pass in a struct which serializes to the proper JSON schema.
	// The jsonschema package is provided for convenience, but you should
	// consider another specialized library if you require more complex schemas.
	Parameters any `json:"parameters"`
}

type Hate

type Hate struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type ImageURLDetail

type ImageURLDetail string
const (
	ImageURLDetailHigh ImageURLDetail = "high"
	ImageURLDetailLow  ImageURLDetail = "low"
	ImageURLDetailAuto ImageURLDetail = "auto"
)

type JailBreak

type JailBreak struct {
	Filtered bool `json:"filtered"`
	Detected bool `json:"detected"`
}

type LogProb

type LogProb struct {
	Token   string  `json:"token"`
	LogProb float64 `json:"logprob"`
	Bytes   []byte  `json:"bytes,omitempty"` // Omitting the field if it is null
	// TopLogProbs is a list of the most likely tokens and their log probability, at this token position.
	// In rare cases, there may be fewer than the number of requested top_logprobs returned.
	TopLogProbs []TopLogProbs `json:"top_logprobs"`
}

LogProb represents the probability information for a token.

type LogProbs

type LogProbs struct {
	// Content is a list of message content tokens with log probability information.
	Content []LogProb `json:"content"`
}

LogProbs is the top-level structure containing the log probability information.

type Model

type Model struct {
	// ID is the unique identifier for the model
	ID string `json:"id"`
	// Created is the timestamp when the model was created
	Created int64 `json:"created,omitempty"`
	// Object is the type of object (typically 'model')
	Object string `json:"object,omitempty"`
	// Ownedby indicates the owner or organization that created the model
	Ownedby string `json:"ownedby,omitempty"`

	// Name is the display name of the model
	Name string `json:"name,omitempty"`
	// Description provides additional information about the model
	Description string `json:"description,omitempty"`
}

Model represents a language model in the LLM system. It contains metadata and identification information for a specific model.

type ModelCache added in v0.2.0

type ModelCache struct {
	Models    []Model   `json:"models"`
	Timestamp time.Time `json:"timestamp"`
}

ModelCache represents the cache of models

func LoadModelCache added in v0.2.0

func LoadModelCache(providerName string) (ModelCache, error)

Load loads the model cache from the file

type Option added in v0.2.0

type Option func(*Provider)

func WithAPIKey

func WithAPIKey(apiKey string) Option

func WithBaseURL

func WithBaseURL(url string) Option

func WithEnvPrefix added in v0.2.0

func WithEnvPrefix(envPrefix string) Option

func WithHttpClient

func WithHttpClient(client *http.Client) Option

func WithHttpTimeout added in v0.2.0

func WithHttpTimeout(timeout time.Duration) Option

func WithModelAlias added in v0.2.0

func WithModelAlias(alias map[string]string) Option

func WithModelPrefix added in v0.2.0

func WithModelPrefix(prefix string) Option

func WithModels added in v0.2.0

func WithModels(models []Model) Option

func WithName added in v0.2.0

func WithName(name string) Option

type Profanity

type Profanity struct {
	Filtered bool `json:"filtered"`
	Detected bool `json:"detected"`
}

type PromptTokensDetails

type PromptTokensDetails struct {
	AudioTokens  int `json:"audio_tokens"`
	CachedTokens int `json:"cached_tokens"`
}

PromptTokensDetails Breakdown of tokens used in the prompt.

type Provider added in v0.2.0

type Provider struct {
	// Type is the type of the provider.
	Type ProviderType `json:"type"`
	// Name is the name of the provider.
	Name string `json:"name" `
	// BaseURL is the base URL of the provider's API.
	BaseURL string `json:"base_url,omitempty"`
	// APIKey is the API key for authentication.
	APIKey string `json:"api_key,omitempty"`
	// EnvPrefix is the environment variable name prefix for the API key.
	EnvPrefix string `json:"env_prefix,omitempty"`

	// ModelPrefix is the prefix of the provider's model name.
	ModelPrefix string `json:"model_prefix,omitempty"`
	// Models is a list of model ids.
	// In env it should be set as a comma separated string: "model1,model2"
	Models []Model `json:"models,omitempty" `
	// ModelAlias is a map of model aliases to model ids.
	// In env it should be set as a key value value: "alias1=model1,alias2=model2"
	ModelAlias map[string]string `json:"model_alias,omitempty"`

	// HttpTimeout is the timeout for the HTTP client.
	HttpTimeout time.Duration `json:"timeout,omitempty"`
	// HttpClient is the HTTP client to use.
	HttpClient *http.Client `json:"-"`
}

Provider represents a provider of LLM services.

func New added in v0.2.0

func New(opts ...Option) *Provider

func (*Provider) GetModelList added in v0.2.0

func (p *Provider) GetModelList(ctx context.Context) []Model

func (*Provider) GetRealModel added in v0.2.0

func (p *Provider) GetRealModel(model string) string

GetRealModel returns the real model name based on the provider's prefix and model alias.

func (*Provider) Load added in v0.2.0

func (p *Provider) Load()

func (*Provider) SetHttpHeaders added in v0.2.0

func (p *Provider) SetHttpHeaders(req *http.Request, stream bool, extraHeaders map[string]string)

func (*Provider) ToOptions added in v0.2.0

func (p *Provider) ToOptions() []Option

type ProviderType added in v0.2.0

type ProviderType string

ProviderType is a string type for the names of supported LLM providers.

const (
	ProviderTypeOpenAICompatible ProviderType = "openai-compatible"
	ProviderTypeOpenAI           ProviderType = "openai"
	ProviderTypeDeepSeek         ProviderType = "deepseek"
	ProviderTypeQwen             ProviderType = "qwen"
	ProviderTypeGemini           ProviderType = "gemini"
	ProviderTypeOpenRouter       ProviderType = "openrouter"
	ProviderTypeVolcengine       ProviderType = "volcengine"
	ProviderTypeGroq             ProviderType = "groq"
	ProviderTypeXai              ProviderType = "xai"
	ProviderTypeSiliconflow      ProviderType = "siliconflow"
	ProviderTypeTogether         ProviderType = "together"
	ProviderTypeFireworks        ProviderType = "fireworks"
)

Provider constants for all supported providers

type RequestOption

type RequestOption func(*ChatCompletionRequest)

RequestOption is a function that configures a ChatCompletionRequest.

func WithExtraHeaders

func WithExtraHeaders(headers map[string]string) RequestOption

func WithMaxCompletionTokens

func WithMaxCompletionTokens(maxCompletionTokens int) RequestOption

WithMaxCompletionTokens specifies the max number of completion tokens to generate.

func WithMaxTokens

func WithMaxTokens(maxTokens int) RequestOption

WithMaxTokens specifies the max number of tokens to generate.

func WithModel

func WithModel(model string) RequestOption

WithModel specifies which model name to use.

func WithN

func WithN(n int) RequestOption

WithN will add an option to set how many chat completion choices to generate for each input message.

func WithSeed

func WithSeed(seed int) RequestOption

WithSeed will add an option to use deterministic sampling.

func WithStream

func WithStream(stream bool) RequestOption

func WithTemperature

func WithTemperature(temperature float32) RequestOption

WithTemperature specifies the model temperature, a hyperparameter that regulates the randomness, or creativity, of the AI's responses.

func WithTools added in v0.2.0

func WithTools(tools []Tool) RequestOption

func WithTopP

func WithTopP(topP float32) RequestOption

WithTopP will add an option to use top-p sampling.

type SelfHarm

type SelfHarm struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type Sexual

type Sexual struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

type StreamOptions

type StreamOptions struct {
	// If set, an additional chunk will be streamed before the data: [DONE] message.
	// The usage field on this chunk shows the token usage statistics for the entire request,
	// and the choices field will always be an empty array.
	// All other chunks will also include a usage field, but with a null value.
	IncludeUsage bool `json:"include_usage,omitempty"`
}

type StreamingChatCompletionResponse

type StreamingChatCompletionResponse struct {
	Response *ChatCompletionResponse
	Err      error
}

type StreamingChatCompletionText

type StreamingChatCompletionText struct {
	Content string
	Err     error
}

type Tool

type Tool struct {
	Type     ToolType            `json:"type"`
	Function *FunctionDefinition `json:"function,omitempty"`
}

type ToolCall

type ToolCall struct {
	// Index is not nil only in chat completion chunk object
	Index    *int         `json:"index,omitempty"`
	ID       string       `json:"id,omitempty"`
	Type     ToolType     `json:"type"`
	Function FunctionCall `json:"function"`
}

type ToolChoice

type ToolChoice struct {
	Type     ToolType     `json:"type"`
	Function ToolFunction `json:"function,omitempty"`
}

type ToolFunction

type ToolFunction struct {
	Name string `json:"name"`
}

type ToolType

type ToolType string
const (
	ToolTypeFunction ToolType = "function"
)

type TopLogProbs

type TopLogProbs struct {
	Token   string  `json:"token"`
	LogProb float64 `json:"logprob"`
	Bytes   []byte  `json:"bytes,omitempty"`
}

type Usage

type Usage struct {
	PromptTokens            int                      `json:"prompt_tokens"`
	CompletionTokens        int                      `json:"completion_tokens"`
	TotalTokens             int                      `json:"total_tokens"`
	PromptTokensDetails     *PromptTokensDetails     `json:"prompt_tokens_details"`
	CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details"`
}

Usage Represents the total token usage per request to OpenAI.

type Violence

type Violence struct {
	Filtered bool   `json:"filtered"`
	Severity string `json:"severity,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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