openai

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const OneMillion = 1_000_000

Variables

View Source
var ErrUnexpectedStatusCode = errors.New("unexpected status code")

ErrUnexpectedStatusCode is an error for unexpected status code.

Functions

func CalculateInputTokensCost

func CalculateInputTokensCost(model string, inputTokens float64) (bool, float64)

func CalculateOutputTokensCost

func CalculateOutputTokensCost(model string, outputTokens float64) (bool, float64)

Types

type APIKey

type APIKey string

type ChatClient

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

ChatClient represents an interface for chat client operations.

func New

func New(apikey APIKey, model, logLevel string, maxTokens *int) *ChatClient

New creates a new ChatClient instance.

func (*ChatClient) MakeCreateChatCompletion

func (c *ChatClient) MakeCreateChatCompletion(prompt string) *CreateChatCompletion

MakeCreateChatCompletion creates a new CreateChatCompletion.

func (*ChatClient) RequestCreateChatCompletion

func (c *ChatClient) RequestCreateChatCompletion(ctx context.Context, ccc *CreateChatCompletion) (*ChatCompletion, error)

RequestCreateChatCompletion requests the AI to create chat completion based on the given prompt.

type ChatCompletion

type ChatCompletion struct {
	ID                string         `json:"id"`
	Object            string         `json:"object"`
	Created           int            `json:"created"`
	Model             string         `json:"model"`
	ResponseFormat    ResponseFormat `json:"response_format"`
	SystemFingerprint string         `json:"system_fingerprint"`
	Choices           []struct {
		FinishReason string      `json:"finish_reason"`
		Index        int         `json:"index"`
		Message      ChatMessage `json:"message"`
	} `json:"choices"`
	Usage struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
}

ChatCompletion represents the JSON structure for the completion response.

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type CreateChatCompletion

type CreateChatCompletion struct {
	Messages         []ChatMessage      `json:"messages"`
	Model            string             `json:"model"`
	ResponseFormat   *ResponseFormat    `json:"response_format,omitempty"`
	MaxTokens        *int               `json:"max_tokens,omitempty"`
	N                *int               `json:"n,omitempty"`
	Temperature      *float64           `json:"temperature,omitempty"`
	Seed             *int               `json:"seed,omitempty"`
	TopP             *float64           `json:"top_p,omitempty"`
	Stop             []string           `json:"stop,omitempty"`
	FrequencyPenalty *float64           `json:"frequency_penalty,omitempty"`
	LogitBias        map[string]float64 `json:"logit_bias,omitempty"`
	User             *string            `json:"user,omitempty"`
	PresencePenalty  *float64           `json:"presence_penalty,omitempty"`
}

CreateChatCompletion represents the structure of a request to the OpenAI API Chat endpoint.

type ErrorDetail

type ErrorDetail struct {
	Message string      `json:"message"`
	Type    string      `json:"type"`
	Param   interface{} `json:"param"` // Param can be of any type, hence using interface{}.
	Code    string      `json:"code"`
}

ErrorDetail represents the details of the error.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse represents the JSON structure for the error response.

type GenerativeAIClient

type GenerativeAIClient interface {
	RequestCreateChatCompletion(context.Context, *CreateChatCompletion) (*ChatCompletion, error)
	MakeCreateChatCompletion(string) *CreateChatCompletion
}

GenerativeAIClient represents an interface for generating AI client operations.

type Pricing

type Pricing struct {
	Model                  string
	InputTokensCostDollar  float64
	InputTokens            float64
	OutputTokensCostDollar float64
	OutputTokens           float64
}

func GetPricing

func GetPricing(model string) *Pricing

type ResponseFormat

type ResponseFormat struct {
	Type string `json:"type"`
}

type TotalUsageCost

type TotalUsageCost struct {
	UsageCosts []*UsageCost
}

func NewTotalUsageCost

func NewTotalUsageCost(usageCosts []*UsageCost) *TotalUsageCost

NewTotalUsageCost creates a new TotalUsageCost instance.

func (*TotalUsageCost) TotalCompletionTokens

func (tuc *TotalUsageCost) TotalCompletionTokens() int

TotalCompletionTokens calculates the total number of completion tokens.

func (*TotalUsageCost) TotalCompletionTokensCost

func (tuc *TotalUsageCost) TotalCompletionTokensCost() (bool, float64)

TotalCompletionTokensCost calculates the total cost of completion tokens.

func (*TotalUsageCost) TotalPromptTokens

func (tuc *TotalUsageCost) TotalPromptTokens() int

TotalPromptTokens calculates the total number of prompt tokens.

func (*TotalUsageCost) TotalPromptTokensCost

func (tuc *TotalUsageCost) TotalPromptTokensCost() (bool, float64)

TotalPromptTokensCost calculates the total cost of prompt tokens.

func (*TotalUsageCost) TotalTotalTokens

func (tuc *TotalUsageCost) TotalTotalTokens() int

TotalTotalTokens calculates the total number of tokens.

func (*TotalUsageCost) TotalTotalTokensCost

func (tuc *TotalUsageCost) TotalTotalTokensCost() (bool, float64)

TotalTotalTokensCost calculates the total cost of tokens.

type UsageCost

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

func NewUsageCost

func NewUsageCost(chatCompletion *ChatCompletion) *UsageCost

func (*UsageCost) CompletionTokens

func (uc *UsageCost) CompletionTokens() int

func (*UsageCost) CompletionTokensCost

func (uc *UsageCost) CompletionTokensCost() (bool, float64)

func (*UsageCost) ModelName

func (uc *UsageCost) ModelName() string

func (*UsageCost) PromptTokens

func (uc *UsageCost) PromptTokens() int

func (*UsageCost) PromptTokensCost

func (uc *UsageCost) PromptTokensCost() (bool, float64)

func (*UsageCost) TotalTokens

func (uc *UsageCost) TotalTokens() int

func (*UsageCost) TotalTokensCost

func (uc *UsageCost) TotalTokensCost() (bool, float64)

Jump to

Keyboard shortcuts

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