openai

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleSystem    = "system"
	RoleUser      = "user"
	RoleAssistant = "assistant"
	RoleTool      = "tool"
	RoleFunction  = "function"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AzureOpenAI added in v1.0.12

type AzureOpenAI struct {
	Domain     string
	Apikey     string
	Apiver     string
	Deployment string

	Transport http.RoundTripper
	Timeout   time.Duration
	Logger    log.Logger

	MaxRetries    int
	RetryAfter    time.Duration
	AbortOnRetry  func() bool
	AbortInterval time.Duration
}

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	Logprobs     any         `json:"logprobs,omitempty"`
	FinishReason string      `json:"finish_reason"`
}

type ChatCompletionRequest added in v1.0.15

type ChatCompletionRequest struct {
	// A list of messages comprising the conversation so far.
	Messages []*ChatMessage `json:"messages,omitempty"`

	// ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
	Model string `json:"model,omitempty"`

	// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
	// Defaults to 0
	FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`

	// Modify the likelihood of specified tokens appearing in the completion.
	// Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
	// Defaults to null
	LogitBias any `json:"logit_bias,omitempty"`

	// 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"`

	// An integer between 0 and 20 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"`

	// The maximum number of tokens to generate in the chat completion.
	// The total length of input tokens and generated tokens is limited by the model's context length.
	MaxTokens int `json:"max_tokens,omitempty"`

	// How many chat completion choices to generate for each input message.
	// Defaults to 1
	N int `json:"n,omitempty"`

	// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
	// Defaults to 0
	PresencePenalty float64 `json:"presence_penalty,omitempty"`

	// An object specifying the format that the model must output.
	// Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.
	// Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
	// Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.
	// Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit,
	// resulting in a long-running and seemingly "stuck" request.
	// Also note that the message content may be partially cut off if finish_reason="length",
	// which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
	ResponseFormat map[string]any `json:"response_format,omitempty"`

	// This feature is in Beta. If specified, our system will make a best effort to sample deterministically,
	// such that repeated requests with the same seed and parameters should return the same result.
	// Determinism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.
	Seed int `json:"seed,omitempty"`

	// Up to 4 sequences where the API will stop generating further tokens.
	Stop any `json:"stop,omitempty"`

	// If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
	// Defaults to false
	Stream bool `json:"stream,omitempty"`

	// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
	// We generally recommend altering this or top_p but not both.
	// Defaults to 1
	Temperature float64 `json:"temperature,omitempty"`

	// A list of tools the model may call.
	// Currently, only functions are supported as a tool.
	// Use this to provide a list of functions the model may generate JSON inputs for.
	Tools []any `json:"tools,omitempty"`

	// Controls which (if any) function is called by the model.
	// none means the model will not call a function and instead generates a message.
	// auto means the model can pick between generating a message or calling a function.
	// Specifying a particular function via {"type": "function", "function": {"name": "my_function"}} forces the model to call that function.
	// none is the default when no functions are present. auto is the default if functions are present.
	ToolChoice any `json:"tool_choice,omitempty"`

	// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
	// We generally recommend altering this or temperature but not both.
	// Defaults to 1
	TopP float64 `json:"top_p,omitempty"`

	// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
	User string `json:"user,omitempty"`

	// Deprecated in favor of tools. A list of functions the model may generate JSON inputs for.
	Functions []*ChatFunction `json:"functions,omitempty"`

	// Deprecated in favor of tool_choice. Controls how the model responds to function calls. "none" means the model does not call a function, and responds to the end-user. "auto" means the model can pick between an end-user or calling a function. Specifying a particular function via {"name":\ "my_function"} forces the model to call that function. "none" is the default when no functions are present. "auto" is the default if functions are present.
	FunctionCall string `json:"function_call,omitempty"`
}

func (*ChatCompletionRequest) AddMessage added in v1.0.15

func (cc *ChatCompletionRequest) AddMessage(cm *ChatMessage)

func (*ChatCompletionRequest) MessageRuneCount added in v1.0.15

func (cc *ChatCompletionRequest) MessageRuneCount() int

func (*ChatCompletionRequest) String added in v1.0.15

func (cc *ChatCompletionRequest) String() string

type ChatCompletionResponse added in v1.0.15

type ChatCompletionResponse struct {
	ID                string        `json:"id,omitempty"`
	Object            string        `json:"object,omitempty"`
	Created           int64         `json:"created,omitempty"`
	Model             string        `json:"model,omitempty"`
	SystemFingerprint string        `json:"system_fingerprint,omitempty"`
	Choices           []*ChatChoice `json:"choices,omitempty"`
	Usage             ChatUsage     `json:"usage,omitempty"`
}

func (*ChatCompletionResponse) Answer added in v1.0.15

func (cc *ChatCompletionResponse) Answer() string

Answer return first choice content

func (*ChatCompletionResponse) ChoiceRuneCount added in v1.0.15

func (cc *ChatCompletionResponse) ChoiceRuneCount() int

func (*ChatCompletionResponse) String added in v1.0.15

func (cc *ChatCompletionResponse) String() string

type ChatFunction

type ChatFunction struct {
	// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
	Name string `json:"name,omitempty"`

	// A description of what the function does, used by the model to choose when and how to call the function.
	Description string `json:"description,omitempty"`

	// The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
	// To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
	Parameters map[string]any `json:"parameters,omitempty"`
}

type ChatMessage

type ChatMessage struct {
	// The role of the messages author. One of system, user, assistant, or function.
	Role string `json:"role,omitempty"`

	// The contents of the message. content is required for all messages, and may be null for assistant messages with function calls.
	Content string `json:"content,omitempty"`

	// The name of the author of this message. name is required if role is function, and it should be the name of the function whose response is in the content. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
	Name string `json:"name,omitempty"`

	// Tool call that this message is responding to. required if role is tool.
	ToolCallID string `json:"tool_call_id,omitempty"`
}

type ChatUsage

type ChatUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

func (*ChatUsage) Add added in v1.0.12

func (cu *ChatUsage) Add(u *ChatUsage)

func (*ChatUsage) String added in v1.0.12

func (cu *ChatUsage) String() string

type EmbeddingData added in v1.0.12

type EmbeddingData struct {
	// The index of the embedding in the list of embeddings.
	Index int `json:"index"`

	// The object type, which is always "embedding".
	Object string `json:"object,omitempty"`

	// The embedding vector, which is a list of floats.
	Embedding []float64 `json:"embedding"`
}

type EmbeddingUsage added in v1.0.12

type EmbeddingUsage struct {
	PromptTokens int `json:"prompt_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

type ErrorDetail

type ErrorDetail struct {
	Type    string `json:"type,omitempty"`
	Code    any    `json:"code,omitempty"`
	Param   any    `json:"param,omitempty"`
	Message string `json:"message,omitempty"`
}

func (*ErrorDetail) String

func (ed *ErrorDetail) String() string

type ErrorResult

type ErrorResult struct {
	StatusCode int          `json:"-"` // http status code
	Status     string       `json:"-"` // http status
	Detail     *ErrorDetail `json:"error,omitempty"`
	RetryAfter time.Duration
}

func (*ErrorResult) Error

func (er *ErrorResult) Error() string

func (*ErrorResult) GetRetryAfter added in v1.0.16

func (er *ErrorResult) GetRetryAfter() time.Duration

type OpenAI

type OpenAI struct {
	Domain string
	Apikey string

	Transport http.RoundTripper
	Timeout   time.Duration
	Logger    log.Logger

	MaxRetries    int
	RetryAfter    time.Duration
	AbortOnRetry  func() bool
	AbortInterval time.Duration
}

type TextEmbeddingsRequest added in v1.0.12

type TextEmbeddingsRequest struct {
	// Input Input text to embed (required)
	Input []string `json:"input,omitempty"`

	// Model ID of the model to use (required)
	Model string `json:"model,omitempty"`

	// Dimensions (optional) The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
	Dimensions int `json:"dimensions,omitempty"`

	// User (optional) A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
	User string `json:"user,omitempty"`
}

func (*TextEmbeddingsRequest) InputRuneCount added in v1.0.13

func (te *TextEmbeddingsRequest) InputRuneCount() int

func (*TextEmbeddingsRequest) String added in v1.0.12

func (te *TextEmbeddingsRequest) String() string

type TextEmbeddingsResponse added in v1.0.12

type TextEmbeddingsResponse struct {
	Data   []*EmbeddingData `json:"data"`
	Model  string           `json:"model"`
	Object string           `json:"object"`
	Usage  ChatUsage        `json:"usage"`
}

func (*TextEmbeddingsResponse) Embedding added in v1.0.13

func (te *TextEmbeddingsResponse) Embedding() []float64

func (*TextEmbeddingsResponse) String added in v1.0.12

func (te *TextEmbeddingsResponse) String() string

Jump to

Keyboard shortcuts

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