openai

package module
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 11 Imported by: 3

README

A go wrapper for OpenAI Platform APIs

This is a wrapper around OpenAI APIs that means to simplify the creation of tooling that interacts with OpenAI and other Go code in fun, novel ways. There is also a generic fun cli in cmd/openai that exposes library code to the CLI as well as providing some usage examples for the library.

If you need a token, head over to https://platform.openai.com/account/api-keys, and sign up if you need to. If you haven't signed up already, you usually get $20ish in credit, which should get you pretty far.

Go Reference Go Go Report Card

You may also notice that there are plenty of pointers for optional fields in Request structs, but none of the common helper funcs to return pointers. I created one module to rule them all (there may be others, but it's 3 lines and I honestly didn't want to spend the time looking). I suggest github.com/andrewstuart/p for your "pointer to anything, even a literal or const" needs.

go install github.com/andrewstuart/openai/cmd/openai@latest

echo "token: $MY_TOKEN" >> $HOME/.config/openai.yaml
# Or export TOKEN in your environment variables.
# add org: <your org id> if you are a member of multiple organizations.

openai chat --personality "Lady Gaga"
openai chat --prompt "You answer in the style of a super chill surfer from southern california."
openai image "Marvin the Paranoid Android giving a speech." -f marvin.jpg
openai variations -n 5 marvin-001.jpg
openai complete 'A description of a painting of a perfect day' | openai image -f self.jpg -

openai files upload *json # must be linejson format
openai files list

For the best current go examples, see the CLI files.

Current support:

  • Chat
  • Completion (code, text, etc)
  • Edit
  • Images
  • Image variations
  • Audio transcription
  • Upload/manage Files (for fine tuning)
    • Convenience methods for jsonlines
  • Fine tune models
  • Moderations (check for OpenAI content policy violations)
  • List models
  • More soon

Documentation

Index

Constants

View Source
const (
	TranscriptionModelWhisper1 = "whisper-1"

	ResponseFormatJSON        = "json"
	ResponseFormatText        = "text"
	ResponseFormatSRT         = "srt"
	ResponseFormatVerboseJSON = "verbose_json"
	ResponseFormatVTT         = "vtt"
)
View Source
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"
	ChatModelGPT40314             = "gpt-4-0314"
	ChatModelGPT40613             = "gpt-4-0613"
	ChatModelGPT432K              = "gpt-4-32k"
	ChatModelGPT432K0314          = "gpt-4-32k-0314"
	ChatModelGPT4TurboPreview     = "gpt-4-turbo-preview"
	ChatModelGPT41106TurboPreview = "gpt-4-1106-preview"
	ChatModelGPT40215TurboPreview = "gpt-4-0215-preview"
)

Well-known Chat constants

View Source
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

View Source
const (
	EditModelDavinci1     = "text-davinci-edit-001"
	EditModelDavinciCode1 = "code-davinci-edit-001"
)
View Source
const (
	ImageResponseFormatURL     = "url"
	ImageResponseFormatB64JSON = "b64_json"

	ImageSize256  = "256x256"
	ImageSize512  = "512x512"
	ImageSize1024 = "1024x1024"

	ImageModelDallE2 = "dall-e-2"
	ImageModelDallE3 = "dall-e-3"
)

Well-known responseformats

View Source
const (
	ModerationModelStable = "text-moderation-stable"
	ModerationModelLatest = "text-moderation-latest"
	ModerationModel007    = "text-moderation-007"
)
View Source
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.

func (*APIError) Error

func (c *APIError) Error() string

Error implements error

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

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

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.

func (*ChatSession) Complete

func (s *ChatSession) Complete(ctx context.Context, msg string) (string, error)

Complete takes a message from the `user` and sends that, along with the Session context, to the OpenAI endpoints for completion.

func (*ChatSession) Stream added in v0.16.0

func (s *ChatSession) Stream(ctx context.Context, msg string) (<-chan string, error)

Stream takes a message from the `user` and sends that, along with the Session context, to the OpenAI endpoints for completion. The response will be streamed.

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 NewClient

func NewClient(tok string, opts ...Opt) (*Client, error)

NewClient returns an OpenAI base client with the token.

func (Client) ChatComplete

func (c Client) ChatComplete(ctx context.Context, r ChatReq) (*ChatRes, error)

ChatComplete is the raw chat/completions endpoint exposed for callers.

func (Client) ChatStream added in v0.16.0

func (c Client) ChatStream(ctx context.Context, r ChatReq) (<-chan ChatStreamRes, error)

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

func (c Client) DeleteFile(ctx context.Context, id string) error

DeleteFile deletes the file specified.

func (Client) DownloadFile added in v0.11.0

func (c Client) DownloadFile(ctx context.Context, id string) (io.ReadCloser, error)

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

func (c *Client) Edit(ctx context.Context, r EditReq) (*EditRes, error)

Edit calls the openai edits endpoint with the given input. https://platform.openai.com/docs/api-reference/edits

func (*Client) GenerateImage

func (c *Client) GenerateImage(ctx context.Context, p ImgReq) (*ImageRes, error)

GenerateImage calls the images/generations API and returns the API response or any error.

func (Client) GetFileDetails added in v0.11.0

func (c Client) GetFileDetails(ctx context.Context, id string) (*FileRes, error)

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

func (c *Client) Models(ctx context.Context) (ModelList, error)

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.

func (Client) Upload added in v0.11.0

func (c Client) Upload(ctx context.Context, v FileUploadReq) (*FileRes, error)

Upload uploads a file to be used within the OpenAI plaform. Most often for fine-tuning models.

func (Client) Variation added in v0.4.0

func (c Client) Variation(ctx context.Context, v VariationReq) (*ImageRes, error)

Variation returns a Variation of an image. Convenience methods exist on images already returned from Client calls to easily vary those images.

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 EditData

type EditData struct {
	Text  string `json:"text"`
	Index int    `json:"index"`
}

EditData holds the specific Choices returned by 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

type FileListRes struct {
	Data   []FileRes `json:"data"`
	Object string    `json:"object"`
}

FileListRes represents the list of files managed by OpenAI

type FileRes added in v0.11.0

type FileRes struct {
	ID        string `json:"id"`
	Object    string `json:"object"`
	Bytes     int    `json:"bytes"`
	CreatedAt Time   `json:"created_at"`
	Filename  string `json:"filename"`
	Purpose   string `json:"purpose"`
}

type FileUploadReq added in v0.11.0

type FileUploadReq struct {
	Filename string
	File     io.Reader
	Purpose  string
}

type ImageRes

type ImageRes struct {
	Created int      `json:"created"`
	Data    []Images `json:"data"`
}

ImageRes is returned by the Image generation.

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.

func (Images) Variation added in v0.4.0

func (i Images) Variation(ctx context.Context, v VariationReq) (*ImageRes, error)

Create a variation on an image already returned.

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

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.

func (ModelList) Has added in v0.19.0

func (ml ModelList) Has(s string) bool

Has returns whether or not the model ID is present in the list.

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

type ModerationReq struct {
	Input string  `json:"input"`
	Model *string `json:"model,omitempty"`
}

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

func WithAPIVersion(base string) Opt

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

func WithBaseURL(base string) Opt

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.

func WithOrg added in v0.6.0

func WithOrg(o string) Opt

Withorg sets the identifier for a specific org, for the case where a user may be part of more than one organization.

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

type Time struct {
	time.Time
}

func (*Time) UnmarshalJSON added in v0.11.0

func (t *Time) UnmarshalJSON(bs []byte) error

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.

Jump to

Keyboard shortcuts

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