Documentation ¶
Index ¶
- Constants
- type APIError
- type ChatChoice
- type ChatMessage
- type ChatReq
- type ChatRes
- type ChatSession
- type ChatStreamChoce
- type ChatStreamRes
- type Client
- func (c Client) ChatComplete(ctx context.Context, r ChatReq) (*ChatRes, error)
- func (c Client) ChatStream(ctx context.Context, r ChatReq) (<-chan ChatStreamRes, error)
- func (c Client) Complete(ctx context.Context, req CompleteReq) (*CompleteRes, error)
- func (c Client) DeleteFile(ctx context.Context, id string) error
- func (c Client) DownloadFile(ctx context.Context, id string) (io.ReadCloser, error)
- func (c *Client) Edit(ctx context.Context, r EditReq) (*EditRes, error)
- func (c *Client) GenerateImage(ctx context.Context, p ImgReq) (*ImageRes, error)
- func (c Client) GetFileDetails(ctx context.Context, id string) (*FileRes, error)
- func (c Client) ListFiles(ctx context.Context) (*FileListRes, error)
- func (c *Client) Models(ctx context.Context) (ModelList, error)
- func (c Client) Moderation(ctx context.Context, req ModerationReq) (*ModerationRes, error)
- func (c *Client) NewChatSession(prompt string) ChatSession
- func (c Client) Transcription(ctx context.Context, v TranscriptionReq) (*TranscriptionRes, error)
- func (c Client) Translation(ctx context.Context, v TranscriptionReq) (*TranscriptionRes, error)
- func (c Client) Upload(ctx context.Context, v FileUploadReq) (*FileRes, error)
- func (c Client) Variation(ctx context.Context, v VariationReq) (*ImageRes, error)
- type CompleteChoice
- type CompleteReq
- type CompleteRes
- type EditData
- type EditReq
- type EditRes
- type FileListRes
- type FileRes
- type FileUploadReq
- type ImageRes
- type Images
- type ImgReq
- type ModelList
- type ModelsRes
- type ModerationReq
- type ModerationRes
- type ModerationResult
- type Opt
- type Permission
- type Time
- type TranscriptionReq
- type TranscriptionRes
- type Usage
- type VariationReq
Constants ¶
const ( TranscriptionModelWhisper1 = "whisper-1" ResponseFormatJSON = "json" ResponseFormatText = "text" ResponseFormatSRT = "srt" ResponseFormatVerboseJSON = "verbose_json" ResponseFormatVTT = "vtt" )
const ( ChatRoleSystem = "system" ChatRoleAssistant = "assistant" ChatRoleUser = "user" ChatModelGPT35Turbo = "gpt-3.5-turbo" ChatModelGPT35Turbo0301 = "gpt-3.5-turbo-0301" ChatModelGPT35Turbo16K = "gpt-3.5-turbo-16k" ChatModelGPT35Turbo0613 = "gpt-3.5-turbo-0613" ChatModelGPT35Turbo1106 = "gpt-3.5-turbo-1106" ChatModelGPT35Turbo0215 = "gpt-3.5-turbo-0215" ChatModelGPT4 = "gpt-4" ChatModelGPT4O = "gpt-4o" ChatModelGPT4020240513 = "gpt-4o-2024-05-13" ChatModelGPT40409 = "gpt-4-2024-04-09" ChatModelGPT40314 = "gpt-4-0314" ChatModelGPT40613 = "gpt-4-0613" ChatModelGPT432K = "gpt-4-32k" ChatModelGPT432K0314 = "gpt-4-32k-0314" ChatModelGPT4TurboPreview = "gpt-4-turbo-preview" ChatModelGPT4Turbo = "gpt-4-turbo" ChatModelGPT41106TurboPreview = "gpt-4-1106-preview" ChatModelGPT40215TurboPreview = "gpt-4-0215-preview" )
Well-known Chat constants
const ( CompletionModelDavinci3 = "text-davinci-003" CompletionModelDavinci2 = "text-davinci-002" CompletionModelCurie1 = "text-curie-001" CompletionModelBabbage1 = "text-babbage-001" CompletionModelAda1 = "text-ada-001" CompletionModelCodeDavinci2 = "code-davinci-002" )
Well-known completion models
const ( EditModelDavinci1 = "text-davinci-edit-001" EditModelDavinciCode1 = "code-davinci-edit-001" )
const ( ImageResponseFormatURL = "url" ImageResponseFormatB64JSON = "b64_json" ImageSize256 = "256x256" ImageSize512 = "512x512" ImageSize1024 = "1024x1024" ImageSize1024x1792 = "1024x1792" ImageSize1792x1024 = "1792x1024" ImageQualityStandard = "standard" ImageQualityHD = "hd" ImageModelDallE2 = "dall-e-2" ImageModelDallE3 = "dall-e-3" )
Well-known responseformats
const ( ModerationModelStable = "text-moderation-stable" ModerationModelLatest = "text-moderation-latest" ModerationModel007 = "text-moderation-007" )
const (
PurposeFineTune = "fine-tune"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct { Data struct { Message string `json:"message"` Type string `json:"type"` Param any `json:"param"` Code any `json:"code"` } `json:"error"` }
APIError is a representation of the error body returned by OpenAI apis, and will be returned by Client calls if encountered.
type ChatChoice ¶ added in v0.4.0
type ChatChoice struct { FinishReason string `json:"finish_reason"` Index int `json:"index"` Message ChatMessage `json:"message"` }
ChatChoice is one of the outputs from chatgpt (and sometimes others)
type ChatMessage ¶
ChatMessage is the structure of the messages array in the request body.
type ChatReq ¶
type ChatReq struct { Model string `json:"model"` Messages []ChatMessage `json:"messages"` Temperature *float64 `json:"temperature,omitempty"` TopP *int `json:"top_p,omitempty"` N *int `json:"n,omitempty"` Stream *bool `json:"stream,omitempty"` MaxTokens *int `json:"max_tokens,omitempty"` PresencePenalty *float64 `json:"presence_penalty,omitempty"` FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` LogitBias map[string]float64 `json:"logit_bias,omitempty"` User *string `json:"user,omitempty"` }
ChatReq is a Request to the chat/completions endpoints.
type ChatRes ¶
type ChatRes struct { Choices []ChatChoice `json:"choices"` Created int `json:"created"` ID string `json:"id"` Model string `json:"model"` Object string `json:"object"` Usage Usage `json:"usage"` }
ChatRes represents the return response from OpenAI chat/completions
type ChatSession ¶
type ChatSession struct { Messages []ChatMessage Model string Tpl ChatReq // contains filtered or unexported fields }
ChatSession is an abstraction that provides simple tracking of ChatMessages and sends the entire chat "session" to the OpenAI APIs for proper contextual chat.
type ChatStreamChoce ¶ added in v0.16.0
type ChatStreamChoce struct { Delta ChatMessage `json:"delta,omitempty"` Index int `json:"index,omitempty"` LogProbs int `json:"logprobs,omitempty"` FinishReason string `json:"finish_reason,omitempty"` }
type ChatStreamRes ¶ added in v0.16.0
type ChatStreamRes struct { ID string `json:"id,omitempty"` Object string `json:"object,omitempty"` CreatedAt int64 `json:"created_at,omitempty"` Choices []*ChatStreamChoce `json:"choices,omitempty"` }
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds the base rester.Client and has methods for communicating with OpenAI.
func (Client) ChatComplete ¶
ChatComplete is the raw chat/completions endpoint exposed for callers.
func (Client) ChatStream ¶ added in v0.16.0
ChatStream takes a request and streams the response from OpenAI.
func (Client) Complete ¶
func (c Client) Complete(ctx context.Context, req CompleteReq) (*CompleteRes, error)
Complete calls the non-chat Completion endpoints for non-chatgpt completion models.
func (Client) DeleteFile ¶ added in v0.11.0
DeleteFile deletes the file specified.
func (Client) DownloadFile ¶ added in v0.11.0
DownloadFile returns a file as an io.ReadCloser. For now, any API errors will be returned in the io.Read method, though this will likely change, hence the error return.
func (*Client) Edit ¶
Edit calls the openai edits endpoint with the given input. https://platform.openai.com/docs/api-reference/edits
func (*Client) GenerateImage ¶
GenerateImage calls the images/generations API and returns the API response or any error.
func (Client) GetFileDetails ¶ added in v0.11.0
GetFileDetails returns the File details for an individual file managed by the OpenAI APIs.
func (Client) ListFiles ¶ added in v0.11.0
func (c Client) ListFiles(ctx context.Context) (*FileListRes, error)
ListFiles returns the list of files known to OpenAI
func (*Client) Models ¶ added in v0.19.0
Models calls the openai models endpoint and returns the results, as a slice but with some convenience methods on a slice type. https://platform.openai.com/docs/api-reference/models
func (Client) Moderation ¶ added in v0.12.0
func (c Client) Moderation(ctx context.Context, req ModerationReq) (*ModerationRes, error)
Moderation returns whether or not OpenAI considers the input text to be rule-breaking, and exactly how rule-breaking.
func (*Client) NewChatSession ¶
func (c *Client) NewChatSession(prompt string) ChatSession
NewChatSession returns a ChatSession object with the given prompt as a starting point (with the `system` role).
func (Client) Transcription ¶ added in v0.5.0
func (c Client) Transcription(ctx context.Context, v TranscriptionReq) (*TranscriptionRes, error)
Transcription returns a Transcription of an image. Convenience methods exist on images already returned from Client calls to easily vary those images.
func (Client) Translation ¶ added in v0.12.0
func (c Client) Translation(ctx context.Context, v TranscriptionReq) (*TranscriptionRes, error)
Translation uses the OpenAI Translation endpoints to translate audio to english. When using this endpoint, the Language parameter of the Req struct is always ignored.
type CompleteChoice ¶
type CompleteChoice struct { Text string `json:"text"` Index int `json:"index"` LogProbs any `json:"log_probs"` FinishReason string `json:"finish_reason"` }
CompleteChoice is the representation of the individual choices returned by OpenAI.
type CompleteReq ¶
type CompleteReq struct { Model string `json:"model"` Prompt string `json:"prompt"` Suffix *string `json:"suffix,omitempty"` MaxTokens *int `json:"max_tokens,omitempty"` Temperature *float64 `json:"temperature,omitempty"` TopP *int `json:"top_p,omitempty"` N *int `json:"n,omitempty"` Logprobs *int `json:"logprobs,omitempty"` Echo *bool `json:"echo,omitempty"` Stop *string `json:"stop,omitempty"` PresencePentalty *float64 `json:"presence_pentalty,omitempty"` FrequencyPentalty *float64 `json:"frequency_pentalty,omitempty"` BestOf *int `json:"best_of,omitempty"` LogitBias *map[string]any `json:"logit_bias,omitempty"` User *string `json:"user,omitempty"` }
CompleteReq holds all the inputs for a completion request.
type CompleteRes ¶
type CompleteRes struct { Choices []CompleteChoice `json:"choices"` Created int `json:"created"` ID string `json:"id"` Model string `json:"model"` Object string `json:"object"` Usage Usage `json:"usage"` }
CompleteRes represents the final completion(s) from OpenAI.
type EditReq ¶
type EditReq struct { Model string `json:"model,omitempty"` Input *string `json:"input,omitempty"` Instruction string `json:"instruction,omitempty"` N *int `json:"n,omitempty"` Temperature *float64 `json:"temperature,omitempty"` TopP *float64 `json:"top_p,omitempty"` }
EditReq represents an edit on the OpenAI APIs.
type EditRes ¶
type EditRes struct { Object string `json:"object"` Created int `json:"created"` Choices []EditData `json:"choices"` Usage Usage `json:"usage"` }
EditRes contains the results from the edit invocation.
type FileListRes ¶ added in v0.11.0
FileListRes represents the list of files managed by OpenAI
type FileUploadReq ¶ added in v0.11.0
type Images ¶ added in v0.4.0
type Images struct { URL string `json:"url"` Image []byte `json:"b64_json"` // contains filtered or unexported fields }
Images are returned as apart of the Image generation calls, and will contain either a URL to the image generated, or the bytes as `.Image` if the input specified `b64_json` as the ResponseFormat.
type ImgReq ¶ added in v0.8.0
type ImgReq struct { Prompt string `json:"prompt,omitempty"` Model string `json:"model,omitempty"` N *int `json:"n,omitempty"` Size *string `json:"size,omitempty"` ResponseFormat *string `json:"response_format,omitempty"` User *string `json:"user,omitempty"` Quality *string `json:"quality,omitempty"` }
ImgReq is the input type for image generation.
type ModelList ¶ added in v0.19.0
type ModelList []ModelsRes
ModelList is a simple convenience abstraction over a slice of ModelsRes.
type ModelsRes ¶ added in v0.19.0
type ModelsRes struct { Created int `json:"created"` ID string `json:"id"` Object string `json:"object"` OwnedBy string `json:"owned_by"` Parent *string `json:"parent"` Permissions []Permission `json:"permission"` Root string `json:"root"` }
ModelsRes returns the models currently available to the user, per the models API on OpenAI.
type ModerationReq ¶ added in v0.12.0
ModerationReq details a request for moderation to the OpenAI endpoint.
type ModerationRes ¶ added in v0.12.0
type ModerationRes struct { ID string `json:"id"` Model string `json:"model"` Results []ModerationResult `json:"results"` }
ModerationRes holds the resulst of the OpenAI moderation call.
type ModerationResult ¶ added in v0.12.0
type ModerationResult struct { Flagged bool `json:"flagged"` Categories map[string]bool `json:"categories"` CategoryScores map[string]float64 `json:"category_scores"` }
ModerationResult is the specific results of the input.
type Opt ¶ added in v0.6.0
type Opt func(*optStruct)
func WithAPIVersion ¶ added in v0.22.0
WithAPIVersion sets the API Version header of the API service to use. This can be used for alternative hosted versions of the OpenAI API, like MS Azure.
func WithBaseURL ¶ added in v0.21.0
WithBaseURL sets the base URL of the API service to use. This can be used for alternative hosted versions of the OpenAI API, like MS Azure.
type Permission ¶ added in v0.19.0
type Permission struct { AllowCreateEngine bool `json:"allow_create_engine"` AllowFineTuning bool `json:"allow_fine_tuning"` AllowLogprobs bool `json:"allow_logprobs"` AllowSampling bool `json:"allow_sampling"` AllowSearchIndices bool `json:"allow_search_indices"` AllowView bool `json:"allow_view"` Created int `json:"created"` Group *string `json:"group"` ID string `json:"id"` IsBlocking bool `json:"is_blocking"` Object string `json:"object"` Organization string `json:"organization"` }
Permissions are the return response from OpenAI
type Time ¶ added in v0.11.0
func (*Time) UnmarshalJSON ¶ added in v0.11.0
type TranscriptionReq ¶ added in v0.7.0
type TranscriptionReq struct { File []byte Model string Prompt *string ResponseFormat *string Temperature *float64 Language *string }
TranscriptionReq hold the data needed for image variation.
type TranscriptionRes ¶ added in v0.5.0
type TranscriptionRes struct {
Text string
}
type Usage ¶
type Usage struct { CompletionTokens int `json:"completion_tokens"` PromptTokens int `json:"prompt_tokens"` TotalTokens int `json:"total_tokens"` }
Usage is a record type returned from many different openai endpoints letting the user know how many tokens were used processing their request.
type VariationReq ¶ added in v0.7.0
type VariationReq struct { Image []byte `json:"image,omitempty"` Model string `json:"model,omitempty"` N *int `json:"n,omitempty"` Size *string `json:"size,omitempty"` ResponseFormat *string `json:"response_format,omitempty"` User *string `json:"user,omitempty"` }
VariationReq hold the data needed for image variation.