Documentation ¶
Overview ¶
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content)
Output:
Example (Chatbot) ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) req := openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "you are a helpful chatbot", }, }, } fmt.Println("Conversation") fmt.Println("---------------------") fmt.Print("> ") s := bufio.NewScanner(os.Stdin) for s.Scan() { req.Messages = append(req.Messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, Content: s.Text(), }) resp, err := client.CreateChatCompletion(context.Background(), req) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) continue } fmt.Printf("%s\n\n", resp.Choices[0].Message.Content) req.Messages = append(req.Messages, resp.Choices[0].Message) fmt.Print("> ") }
Output:
Index ¶
- Constants
- Variables
- type APIError
- type APIType
- type AudioRequest
- type AudioResponse
- type AudioResponseFormat
- type ChatCompletionChoice
- type ChatCompletionMessage
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStream
- type ChatCompletionStreamChoice
- type ChatCompletionStreamChoiceDelta
- type ChatCompletionStreamResponse
- type Client
- func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error)
- func (c *Client) CreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (response ChatCompletionResponse, err error)
- func (c *Client) CreateChatCompletionStream(ctx context.Context, request ChatCompletionRequest) (stream *ChatCompletionStream, err error)
- func (c *Client) CreateCompletion(ctx context.Context, request CompletionRequest) (response CompletionResponse, err error)
- func (c *Client) CreateCompletionStream(ctx context.Context, request CompletionRequest) (stream *CompletionStream, err error)
- func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error)
- func (c *Client) CreateEmbeddings(ctx context.Context, conv EmbeddingRequestConverter) (res EmbeddingResponse, err error)
- func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error)
- func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (response FineTune, err error)
- func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error)
- func (c *Client) CreateTranscription(ctx context.Context, request AudioRequest) (response AudioResponse, err error)
- func (c *Client) CreateTranslation(ctx context.Context, request AudioRequest) (response AudioResponse, err error)
- func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error)
- func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error)
- func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (response FineTuneDeleteResponse, err error)
- func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error)
- func (c *Client) GetEngine(ctx context.Context, engineID string) (engine Engine, err error)
- func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error)
- func (c *Client) GetFileContent(ctx context.Context, fileID string) (content io.ReadCloser, err error)
- func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error)
- func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err error)
- func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error)
- func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error)
- func (c *Client) ListFineTuneEvents(ctx context.Context, fineTuneID string) (response FineTuneEventList, err error)
- func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error)
- func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error)
- func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error)
- type ClientConfig
- type CompletionChoice
- type CompletionRequest
- type CompletionResponse
- type CompletionStream
- type EditsChoice
- type EditsRequest
- type EditsResponse
- type Embedding
- type EmbeddingModel
- type EmbeddingRequest
- type EmbeddingRequestConverter
- type EmbeddingRequestStrings
- type EmbeddingRequestTokens
- type EmbeddingResponse
- type Engine
- type EnginesList
- type ErrorResponse
- type File
- type FileRequest
- type FilesList
- type FineTune
- type FineTuneDeleteResponse
- type FineTuneEvent
- type FineTuneEventList
- type FineTuneHyperParams
- type FineTuneList
- type FineTuneRequest
- type FinishReason
- type FunctionCall
- type FunctionDefinedeprecated
- type FunctionDefinition
- type ImageEditRequest
- type ImageRequest
- type ImageResponse
- type ImageResponseDataInner
- type ImageVariRequest
- type LogprobResult
- type Model
- type ModelsList
- type ModerationRequest
- type ModerationResponse
- type Permission
- type RequestError
- type Result
- type ResultCategories
- type ResultCategoryScores
- type Usage
Examples ¶
- Package
- Package (Chatbot)
- APIError
- Client.CreateChatCompletionStream
- Client.CreateCompletion
- Client.CreateCompletionStream
- Client.CreateImage
- Client.CreateImage (Base64)
- Client.CreateTranscription
- Client.CreateTranscription (Captions)
- Client.CreateTranslation
- ClientConfig (ClientWithProxy)
- DefaultAzureConfig
Constants ¶
const ( ChatMessageRoleSystem = "system" ChatMessageRoleUser = "user" ChatMessageRoleAssistant = "assistant" ChatMessageRoleFunction = "function" )
Chat message role defined by the OpenAI API.
const ( GPT432K0613 = "gpt-4-32k-0613" GPT432K0314 = "gpt-4-32k-0314" GPT432K = "gpt-4-32k" GPT40613 = "gpt-4-0613" GPT40314 = "gpt-4-0314" GPT4 = "gpt-4" GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613" GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301" GPT3Dot5Turbo16K = "gpt-3.5-turbo-16k" GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613" GPT3Dot5Turbo = "gpt-3.5-turbo" GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci003 = "text-davinci-003" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci002 = "text-davinci-002" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextCurie001 = "text-curie-001" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextBabbage001 = "text-babbage-001" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextAda001 = "text-ada-001" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci001 = "text-davinci-001" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3DavinciInstructBeta = "davinci-instruct-beta" GPT3Davinci = "davinci" GPT3Davinci002 = "davinci-002" // Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead. GPT3CurieInstructBeta = "curie-instruct-beta" GPT3Curie = "curie" GPT3Curie002 = "curie-002" GPT3Ada = "ada" GPT3Ada002 = "ada-002" GPT3Babbage = "babbage" GPT3Babbage002 = "babbage-002" )
GPT3 Defines the models provided by OpenAI to use when generating completions from OpenAI. GPT3 Models are designed for text-based tasks. For code-specific tasks, please refer to the Codex series of models.
const ( CodexCodeDavinci002 = "code-davinci-002" CodexCodeCushman001 = "code-cushman-001" CodexCodeDavinci001 = "code-davinci-001" )
Codex Defines the models provided by OpenAI. These models are designed for code-specific tasks, and use a different tokenizer which optimizes for whitespace.
const ( CreateImageSize256x256 = "256x256" CreateImageSize512x512 = "512x512" CreateImageSize1024x1024 = "1024x1024" )
Image sizes defined by the OpenAI API.
const ( CreateImageResponseFormatURL = "url" CreateImageResponseFormatB64JSON = "b64_json" )
const ( ModerationTextStable = "text-moderation-stable" ModerationTextLatest = "text-moderation-latest" ModerationText001 = "text-moderation-001" )
The default is text-moderation-latest which will be automatically upgraded over time. This ensures you are always using our most accurate model. If you use text-moderation-stable, we will provide advanced notice before updating the model. Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest.
const AzureAPIKeyHeader = "api-key"
const (
Whisper1 = "whisper-1"
)
Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI.
Variables ¶
var ( ErrChatCompletionInvalidModel = errors.New("this model is not supported with this method, please use CreateCompletion client method instead") //nolint:lll ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll )
var ( ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll ErrCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateCompletionStream") //nolint:lll ErrCompletionRequestPromptTypeNotSupported = errors.New("the type of CompletionRequest.Prompt only supports string and []string") //nolint:lll )
var (
ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct { Code any `json:"code,omitempty"` Message string `json:"message"` Param *string `json:"param,omitempty"` Type string `json:"type"` HTTPStatusCode int `json:"-"` }
APIError provides error information returned by the OpenAI API.
Example ¶
Open-AI maintains clear documentation on how to handle API errors.
see: https://platform.openai.com/docs/guides/error-codes/api-errors
var err error // Assume this is the error you are checking. e := &openai.APIError{} if errors.As(err, &e) { switch e.HTTPStatusCode { case 401: // invalid auth or key (do not retry) case 429: // rate limiting or engine overload (wait and retry) case 500: // openai server error (retry) default: // unhandled } }
Output:
func (*APIError) UnmarshalJSON ¶
type AudioRequest ¶
type AudioRequest struct { Model string // FilePath is either an existing file in your filesystem or a filename representing the contents of Reader. FilePath string // Reader is an optional io.Reader when you do not want to use an existing file. Reader io.Reader Prompt string // For translation, it should be in English Temperature float32 Language string // For translation, just do not use it. It seems "en" works, not confirmed... Format AudioResponseFormat }
AudioRequest represents a request structure for audio API. ResponseFormat is not supported for now. We only return JSON text, which may be sufficient.
func (AudioRequest) HasJSONResponse ¶
func (r AudioRequest) HasJSONResponse() bool
HasJSONResponse returns true if the response format is JSON.
type AudioResponse ¶
type AudioResponse struct { Task string `json:"task"` Language string `json:"language"` Duration float64 `json:"duration"` Segments []struct { ID int `json:"id"` Seek int `json:"seek"` Start float64 `json:"start"` End float64 `json:"end"` Text string `json:"text"` Tokens []int `json:"tokens"` Temperature float64 `json:"temperature"` AvgLogprob float64 `json:"avg_logprob"` CompressionRatio float64 `json:"compression_ratio"` NoSpeechProb float64 `json:"no_speech_prob"` Transient bool `json:"transient"` } `json:"segments"` Text string `json:"text"` }
AudioResponse represents a response structure for audio API.
type AudioResponseFormat ¶
type AudioResponseFormat string
Response formats; Whisper uses AudioResponseFormatJSON by default.
const ( AudioResponseFormatJSON AudioResponseFormat = "json" AudioResponseFormatText AudioResponseFormat = "text" AudioResponseFormatSRT AudioResponseFormat = "srt" AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" AudioResponseFormatVTT AudioResponseFormat = "vtt" )
type ChatCompletionChoice ¶
type ChatCompletionChoice struct { Index int `json:"index"` Message ChatCompletionMessage `json:"message"` // 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"` }
type ChatCompletionMessage ¶
type ChatCompletionMessage struct { Role string `json:"role"` Content string `json:"content"` // 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"` }
type ChatCompletionRequest ¶
type ChatCompletionRequest struct { Model string `json:"model"` Messages []ChatCompletionMessage `json:"messages"` MaxTokens int `json:"max_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"` 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"` User string `json:"user,omitempty"` Functions []FunctionDefinition `json:"functions,omitempty"` FunctionCall any `json:"function_call,omitempty"` }
ChatCompletionRequest represents a request structure for chat completion API.
type ChatCompletionResponse ¶
type ChatCompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []ChatCompletionChoice `json:"choices"` Usage Usage `json:"usage"` }
ChatCompletionResponse represents a response structure for chat completion API.
type ChatCompletionStream ¶
type ChatCompletionStream struct {
// contains filtered or unexported fields
}
ChatCompletionStream Note: Perhaps it is more elegant to abstract Stream using generics.
type ChatCompletionStreamChoice ¶
type ChatCompletionStreamChoice struct { Index int `json:"index"` Delta ChatCompletionStreamChoiceDelta `json:"delta"` FinishReason FinishReason `json:"finish_reason"` }
type ChatCompletionStreamChoiceDelta ¶
type ChatCompletionStreamChoiceDelta struct { Content string `json:"content,omitempty"` Role string `json:"role,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` }
type ChatCompletionStreamResponse ¶
type ChatCompletionStreamResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []ChatCompletionStreamChoice `json:"choices"` }
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is OpenAI GPT-3 API client.
func NewClientWithConfig ¶
func NewClientWithConfig(config ClientConfig) *Client
NewClientWithConfig creates new OpenAI API client for specified config.
func NewOrgClient
deprecated
func (*Client) CancelFineTune ¶
func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error)
CancelFineTune cancel a fine-tune job.
func (*Client) CreateChatCompletion ¶
func (c *Client) CreateChatCompletion( ctx context.Context, request ChatCompletionRequest, ) (response ChatCompletionResponse, err error)
CreateChatCompletion — API call to Create a completion for the chat message.
func (*Client) CreateChatCompletionStream ¶
func (c *Client) CreateChatCompletionStream( ctx context.Context, request ChatCompletionRequest, ) (stream *ChatCompletionStream, err error)
CreateChatCompletionStream — API call to create a chat completion w/ streaming support. It sets whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) stream, err := client.CreateChatCompletionStream( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, MaxTokens: 20, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Lorem ipsum", }, }, Stream: true, }, ) if err != nil { fmt.Printf("ChatCompletionStream error: %v\n", err) return } defer stream.Close() fmt.Printf("Stream response: ") for { var response openai.ChatCompletionStreamResponse response, err = stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("\nStream finished") return } if err != nil { fmt.Printf("\nStream error: %v\n", err) return } fmt.Printf(response.Choices[0].Delta.Content) }
Output:
func (*Client) CreateCompletion ¶
func (c *Client) CreateCompletion( ctx context.Context, request CompletionRequest, ) (response CompletionResponse, err error)
CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well as, if requested, the probabilities over each alternative token at each position.
If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object, and the server will use the model's parameters to generate the completion.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ Model: openai.GPT3Ada, MaxTokens: 5, Prompt: "Lorem ipsum", }, ) if err != nil { fmt.Printf("Completion error: %v\n", err) return } fmt.Println(resp.Choices[0].Text)
Output:
func (*Client) CreateCompletionStream ¶
func (c *Client) CreateCompletionStream( ctx context.Context, request CompletionRequest, ) (stream *CompletionStream, err error)
CreateCompletionStream — API call to create a completion w/ streaming support. It sets whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) stream, err := client.CreateCompletionStream( context.Background(), openai.CompletionRequest{ Model: openai.GPT3Ada, MaxTokens: 5, Prompt: "Lorem ipsum", Stream: true, }, ) if err != nil { fmt.Printf("CompletionStream error: %v\n", err) return } defer stream.Close() for { var response openai.CompletionResponse response, err = stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("Stream finished") return } if err != nil { fmt.Printf("Stream error: %v\n", err) return } fmt.Printf("Stream response: %#v\n", response) }
Output:
func (*Client) CreateEditImage ¶
func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error)
CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API.
func (*Client) CreateEmbeddings ¶
func (c *Client) CreateEmbeddings(ctx context.Context, conv EmbeddingRequestConverter) (res EmbeddingResponse, err error)
CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |body.Input|. https://beta.openai.com/docs/api-reference/embeddings/create
Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens for embedding groups of text already converted to tokens.
func (*Client) CreateFile ¶
CreateFile uploads a jsonl file to GPT3 FilePath must be a local file path.
func (*Client) CreateFineTune ¶
func (*Client) CreateImage ¶
func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error)
CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) respURL, err := client.CreateImage( context.Background(), openai.ImageRequest{ Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail", Size: openai.CreateImageSize256x256, ResponseFormat: openai.CreateImageResponseFormatURL, N: 1, }, ) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } fmt.Println(respURL.Data[0].URL)
Output:
Example (Base64) ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateImage( context.Background(), openai.ImageRequest{ Prompt: "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine", Size: openai.CreateImageSize512x512, ResponseFormat: openai.CreateImageResponseFormatB64JSON, N: 1, }, ) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } b, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON) if err != nil { fmt.Printf("Base64 decode error: %v\n", err) return } f, err := os.Create("example.png") if err != nil { fmt.Printf("File creation error: %v\n", err) return } defer f.Close() _, err = f.Write(b) if err != nil { fmt.Printf("File write error: %v\n", err) return } fmt.Println("The image was saved as example.png")
Output:
func (*Client) CreateTranscription ¶
func (c *Client) CreateTranscription( ctx context.Context, request AudioRequest, ) (response AudioResponse, err error)
CreateTranscription — API call to create a transcription. Returns transcribed text.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: "recording.mp3", }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } fmt.Println(resp.Text)
Output:
Example (Captions) ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: os.Args[1], Format: openai.AudioResponseFormatSRT, }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } f, err := os.Create(os.Args[1] + ".srt") if err != nil { fmt.Printf("Could not open file: %v\n", err) return } defer f.Close() if _, err = f.WriteString(resp.Text); err != nil { fmt.Printf("Error writing to file: %v\n", err) return }
Output:
func (*Client) CreateTranslation ¶
func (c *Client) CreateTranslation( ctx context.Context, request AudioRequest, ) (response AudioResponse, err error)
CreateTranslation — API call to translate audio into English.
Example ¶
client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranslation( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: "recording.mp3", }, ) if err != nil { fmt.Printf("Translation error: %v\n", err) return } fmt.Println(resp.Text)
Output:
func (*Client) CreateVariImage ¶
func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error)
CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API. Use abbreviations(vari for variation) because ci-lint has a single-line length limit ...
func (*Client) DeleteFile ¶
DeleteFile deletes an existing file.
func (*Client) DeleteFineTune ¶
func (*Client) Edits ¶
func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error)
Edits Perform an API call to the Edits endpoint.
Deprecated: Users of the Edits API and its associated models (e.g., text-davinci-edit-001 or code-davinci-edit-001)
will need to migrate to GPT-3.5 Turbo by January 4, 2024. You can use CreateChatCompletion or CreateChatCompletionStream instead.
func (*Client) GetEngine ¶
GetEngine Retrieves an engine instance, providing basic information about the engine such as the owner and availability.
func (*Client) GetFile ¶
GetFile Retrieves a file instance, providing basic information about the file such as the file name and purpose.
func (*Client) GetFileContent ¶
func (*Client) GetFineTune ¶
func (*Client) GetModel ¶
GetModel Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
func (*Client) ListEngines ¶
func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error)
ListEngines Lists the currently available engines, and provides basic information about each option such as the owner and availability.
func (*Client) ListFiles ¶
ListFiles Lists the currently available files, and provides basic information about each file such as the file name and purpose.
func (*Client) ListFineTuneEvents ¶
func (*Client) ListFineTunes ¶
func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error)
func (*Client) ListModels ¶
func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error)
ListModels Lists the currently available models, and provides basic information about each model such as the model id and parent.
func (*Client) Moderations ¶
func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error)
Moderations — perform a moderation api call over a string. Input can be an array or slice but a string will reduce the complexity.
type ClientConfig ¶
type ClientConfig struct { BaseURL string OrgID string APIType APIType APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD AzureModelMapperFunc func(model string) string // replace model to azure deployment name func HTTPClient *http.Client EmptyMessagesLimit uint // contains filtered or unexported fields }
ClientConfig is a configuration of a client.
Example (ClientWithProxy) ¶
config := openai.DefaultConfig(os.Getenv("OPENAI_API_KEY")) port := os.Getenv("OPENAI_PROXY_PORT") proxyURL, err := url.Parse(fmt.Sprintf("http://localhost:%s", port)) if err != nil { panic(err) } transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), } config.HTTPClient = &http.Client{ Transport: transport, } client := openai.NewClientWithConfig(config) client.CreateChatCompletion( //nolint:errcheck // outside of the scope of this example. context.Background(), openai.ChatCompletionRequest{ // etc... }, )
Output:
func DefaultAzureConfig ¶
func DefaultAzureConfig(apiKey, baseURL string) ClientConfig
Example ¶
azureKey := os.Getenv("AZURE_OPENAI_API_KEY") // Your azure API key azureEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") // Your azure OpenAI endpoint config := openai.DefaultAzureConfig(azureKey, azureEndpoint) client := openai.NewClientWithConfig(config) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello Azure OpenAI!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content)
Output:
func DefaultConfig ¶
func DefaultConfig(authToken string) ClientConfig
func (ClientConfig) GetAzureDeploymentByModel ¶
func (c ClientConfig) GetAzureDeploymentByModel(model string) string
func (ClientConfig) String ¶
func (ClientConfig) String() string
type CompletionChoice ¶
type CompletionChoice struct { Text string `json:"text"` Index int `json:"index"` FinishReason string `json:"finish_reason"` LogProbs LogprobResult `json:"logprobs"` }
CompletionChoice represents one of possible completions.
type CompletionRequest ¶
type CompletionRequest struct { Model string `json:"model"` Prompt any `json:"prompt,omitempty"` Suffix string `json:"suffix,omitempty"` MaxTokens int `json:"max_tokens,omitempty"` Temperature float32 `json:"temperature,omitempty"` TopP float32 `json:"top_p,omitempty"` N int `json:"n,omitempty"` Stream bool `json:"stream,omitempty"` LogProbs int `json:"logprobs,omitempty"` Echo bool `json:"echo,omitempty"` Stop []string `json:"stop,omitempty"` PresencePenalty float32 `json:"presence_penalty,omitempty"` FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` BestOf int `json:"best_of,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/completions/create#completions/create-logit_bias LogitBias map[string]int `json:"logit_bias,omitempty"` User string `json:"user,omitempty"` }
CompletionRequest represents a request structure for completion API.
type CompletionResponse ¶
type CompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []CompletionChoice `json:"choices"` Usage Usage `json:"usage"` }
CompletionResponse represents a response structure for completion API.
type CompletionStream ¶
type CompletionStream struct {
// contains filtered or unexported fields
}
type EditsChoice ¶
EditsChoice represents one of possible edits.
type EditsRequest ¶
type EditsRequest struct { Model *string `json:"model,omitempty"` Input string `json:"input,omitempty"` Instruction string `json:"instruction,omitempty"` N int `json:"n,omitempty"` Temperature float32 `json:"temperature,omitempty"` TopP float32 `json:"top_p,omitempty"` }
EditsRequest represents a request structure for Edits API.
type EditsResponse ¶
type EditsResponse struct { Object string `json:"object"` Created int64 `json:"created"` Usage Usage `json:"usage"` Choices []EditsChoice `json:"choices"` }
EditsResponse represents a response structure for Edits API.
type Embedding ¶
type Embedding struct { Object string `json:"object"` Embedding []float32 `json:"embedding"` Index int `json:"index"` }
Embedding is a special format of data representation that can be easily utilized by machine learning models and algorithms. The embedding is an information dense representation of the semantic meaning of a piece of text. Each embedding is a vector of floating point numbers, such that the distance between two embeddings in the vector space is correlated with semantic similarity between two inputs in the original format. For example, if two texts are similar, then their vector representations should also be similar.
type EmbeddingModel ¶
type EmbeddingModel int
EmbeddingModel enumerates the models which can be used to generate Embedding vectors.
const ( Unknown EmbeddingModel = iota // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. AdaSimilarity // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. BabbageSimilarity // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. CurieSimilarity // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. DavinciSimilarity // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. AdaSearchDocument // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. AdaSearchQuery // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. BabbageSearchDocument // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. BabbageSearchQuery // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. CurieSearchDocument // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. CurieSearchQuery // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. DavinciSearchDocument // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. DavinciSearchQuery // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. AdaCodeSearchCode // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. AdaCodeSearchText // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. BabbageCodeSearchCode // Deprecated: Will be shut down on January 04, 2024. Use text-embedding-ada-002 instead. BabbageCodeSearchText AdaEmbeddingV2 )
func (EmbeddingModel) MarshalText ¶
func (e EmbeddingModel) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (EmbeddingModel) String ¶
func (e EmbeddingModel) String() string
String implements the fmt.Stringer interface.
func (*EmbeddingModel) UnmarshalText ¶
func (e *EmbeddingModel) UnmarshalText(b []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface. On unrecognized value, it sets |e| to Unknown.
type EmbeddingRequest ¶
type EmbeddingRequest struct { Input any `json:"input"` Model EmbeddingModel `json:"model"` User string `json:"user"` }
func (EmbeddingRequest) Convert ¶
func (r EmbeddingRequest) Convert() EmbeddingRequest
type EmbeddingRequestConverter ¶
type EmbeddingRequestConverter interface { // Needs to be of type EmbeddingRequestStrings or EmbeddingRequestTokens Convert() EmbeddingRequest }
type EmbeddingRequestStrings ¶
type EmbeddingRequestStrings struct { // Input is a slice of strings for which you want to generate an Embedding vector. // Each input must not exceed 8192 tokens in length. // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they // have observed inferior results when newlines are present. // E.g. // "The food was delicious and the waiter..." Input []string `json:"input"` // ID of the model to use. You can use the List models API to see all of your available models, // or see our Model overview for descriptions of them. Model EmbeddingModel `json:"model"` // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. User string `json:"user"` }
EmbeddingRequestStrings is the input to a create embeddings request with a slice of strings.
func (EmbeddingRequestStrings) Convert ¶
func (r EmbeddingRequestStrings) Convert() EmbeddingRequest
type EmbeddingRequestTokens ¶
type EmbeddingRequestTokens struct { // Input is a slice of slices of ints ([][]int) for which you want to generate an Embedding vector. // Each input must not exceed 8192 tokens in length. // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they // have observed inferior results when newlines are present. // E.g. // "The food was delicious and the waiter..." Input [][]int `json:"input"` // ID of the model to use. You can use the List models API to see all of your available models, // or see our Model overview for descriptions of them. Model EmbeddingModel `json:"model"` // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. User string `json:"user"` }
func (EmbeddingRequestTokens) Convert ¶
func (r EmbeddingRequestTokens) Convert() EmbeddingRequest
type EmbeddingResponse ¶
type EmbeddingResponse struct { Object string `json:"object"` Data []Embedding `json:"data"` Model EmbeddingModel `json:"model"` Usage Usage `json:"usage"` }
EmbeddingResponse is the response from a Create embeddings request.
type Engine ¶
type Engine struct { ID string `json:"id"` Object string `json:"object"` Owner string `json:"owner"` Ready bool `json:"ready"` }
Engine struct represents engine from OpenAPI API.
type EnginesList ¶
type EnginesList struct {
Engines []Engine `json:"data"`
}
EnginesList is a list of engines.
type ErrorResponse ¶
type ErrorResponse struct {
Error *APIError `json:"error,omitempty"`
}
type File ¶
type File struct { Bytes int `json:"bytes"` CreatedAt int64 `json:"created_at"` ID string `json:"id"` FileName string `json:"filename"` Object string `json:"object"` Owner string `json:"owner"` Purpose string `json:"purpose"` }
File struct represents an OpenAPI file.
type FileRequest ¶
type FilesList ¶
type FilesList struct {
Files []File `json:"data"`
}
FilesList is a list of files that belong to the user or organization.
type FineTune ¶
type FineTune struct { ID string `json:"id"` Object string `json:"object"` Model string `json:"model"` CreatedAt int64 `json:"created_at"` FineTuneEventList []FineTuneEvent `json:"events,omitempty"` FineTunedModel string `json:"fine_tuned_model"` HyperParams FineTuneHyperParams `json:"hyperparams"` OrganizationID string `json:"organization_id"` ResultFiles []File `json:"result_files"` Status string `json:"status"` ValidationFiles []File `json:"validation_files"` TrainingFiles []File `json:"training_files"` UpdatedAt int64 `json:"updated_at"` }
type FineTuneDeleteResponse ¶
type FineTuneEvent ¶
type FineTuneEventList ¶
type FineTuneEventList struct { Object string `json:"object"` Data []FineTuneEvent `json:"data"` }
type FineTuneHyperParams ¶
type FineTuneList ¶
type FineTuneRequest ¶
type FineTuneRequest struct { TrainingFile string `json:"training_file"` ValidationFile string `json:"validation_file,omitempty"` Model string `json:"model,omitempty"` Epochs int `json:"n_epochs,omitempty"` BatchSize int `json:"batch_size,omitempty"` LearningRateMultiplier float32 `json:"learning_rate_multiplier,omitempty"` PromptLossRate float32 `json:"prompt_loss_rate,omitempty"` ComputeClassificationMetrics bool `json:"compute_classification_metrics,omitempty"` ClassificationClasses int `json:"classification_n_classes,omitempty"` ClassificationPositiveClass string `json:"classification_positive_class,omitempty"` ClassificationBetas []float32 `json:"classification_betas,omitempty"` Suffix string `json:"suffix,omitempty"` }
type FinishReason ¶
type FinishReason string
const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonFunctionCall FinishReason = "function_call" FinishReasonContentFilter FinishReason = "content_filter" FinishReasonNull FinishReason = "null" )
type FunctionCall ¶
type FunctionDefine
deprecated
type FunctionDefine = FunctionDefinition
Deprecated: use FunctionDefinition instead.
type FunctionDefinition ¶
type FunctionDefinition struct { Name string `json:"name"` Description string `json:"description,omitempty"` // Parameters is an object describing the function. // You can pass a []byte describing 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 ImageEditRequest ¶
type ImageEditRequest struct { Image *os.File `json:"image,omitempty"` Mask *os.File `json:"mask,omitempty"` Prompt string `json:"prompt,omitempty"` N int `json:"n,omitempty"` Size string `json:"size,omitempty"` ResponseFormat string `json:"response_format,omitempty"` }
ImageEditRequest represents the request structure for the image API.
type ImageRequest ¶
type ImageRequest struct { Prompt string `json:"prompt,omitempty"` N int `json:"n,omitempty"` Size string `json:"size,omitempty"` ResponseFormat string `json:"response_format,omitempty"` User string `json:"user,omitempty"` }
ImageRequest represents the request structure for the image API.
type ImageResponse ¶
type ImageResponse struct { Created int64 `json:"created,omitempty"` Data []ImageResponseDataInner `json:"data,omitempty"` }
ImageResponse represents a response structure for image API.
type ImageResponseDataInner ¶
type ImageResponseDataInner struct { URL string `json:"url,omitempty"` B64JSON string `json:"b64_json,omitempty"` }
ImageResponseDataInner represents a response data structure for image API.
type ImageVariRequest ¶
type ImageVariRequest struct { Image *os.File `json:"image,omitempty"` N int `json:"n,omitempty"` Size string `json:"size,omitempty"` ResponseFormat string `json:"response_format,omitempty"` }
ImageVariRequest represents the request structure for the image API.
type LogprobResult ¶
type LogprobResult struct { Tokens []string `json:"tokens"` TokenLogprobs []float32 `json:"token_logprobs"` TopLogprobs []map[string]float32 `json:"top_logprobs"` TextOffset []int `json:"text_offset"` }
LogprobResult represents logprob result of Choice.
type Model ¶
type Model struct { CreatedAt int64 `json:"created"` ID string `json:"id"` Object string `json:"object"` OwnedBy string `json:"owned_by"` Permission []Permission `json:"permission"` Root string `json:"root"` Parent string `json:"parent"` }
Model struct represents an OpenAPI model.
type ModelsList ¶
type ModelsList struct {
Models []Model `json:"data"`
}
ModelsList is a list of models, including those that belong to the user or organization.
type ModerationRequest ¶
type ModerationRequest struct { Input string `json:"input,omitempty"` Model string `json:"model,omitempty"` }
ModerationRequest represents a request structure for moderation API.
type ModerationResponse ¶
type ModerationResponse struct { ID string `json:"id"` Model string `json:"model"` Results []Result `json:"results"` }
ModerationResponse represents a response structure for moderation API.
type Permission ¶
type Permission struct { CreatedAt int64 `json:"created"` ID string `json:"id"` Object string `json:"object"` AllowCreateEngine bool `json:"allow_create_engine"` AllowSampling bool `json:"allow_sampling"` AllowLogprobs bool `json:"allow_logprobs"` AllowSearchIndices bool `json:"allow_search_indices"` AllowView bool `json:"allow_view"` AllowFineTuning bool `json:"allow_fine_tuning"` Organization string `json:"organization"` Group interface{} `json:"group"` IsBlocking bool `json:"is_blocking"` }
Permission struct represents an OpenAPI permission.
type RequestError ¶
RequestError provides informations about generic request errors.
func (*RequestError) Error ¶
func (e *RequestError) Error() string
func (*RequestError) Unwrap ¶
func (e *RequestError) Unwrap() error
type Result ¶
type Result struct { Categories ResultCategories `json:"categories"` CategoryScores ResultCategoryScores `json:"category_scores"` Flagged bool `json:"flagged"` }
Result represents one of possible moderation results.
type ResultCategories ¶
type ResultCategories struct { Hate bool `json:"hate"` HateThreatening bool `json:"hate/threatening"` SelfHarm bool `json:"self-harm"` Sexual bool `json:"sexual"` SexualMinors bool `json:"sexual/minors"` Violence bool `json:"violence"` ViolenceGraphic bool `json:"violence/graphic"` }
ResultCategories represents Categories of Result.
type ResultCategoryScores ¶
type ResultCategoryScores struct { Hate float32 `json:"hate"` HateThreatening float32 `json:"hate/threatening"` SelfHarm float32 `json:"self-harm"` Sexual float32 `json:"sexual"` SexualMinors float32 `json:"sexual/minors"` Violence float32 `json:"violence"` ViolenceGraphic float32 `json:"violence/graphic"` }
ResultCategoryScores represents CategoryScores of Result.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct.
|
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct. |