Documentation ¶
Index ¶
Constants ¶
const ( AdaEngine = "ada" BabbageEngine = "babbage" CurieEngine = "curie" DavinciEngine = "davinci" DefaultEngine = DavinciEngine )
Engine Types
Variables ¶
This section is empty.
Functions ¶
Types ¶
type APIError ¶ added in v1.1.2
type APIError struct { StatusCode int `json:"status_code"` Message string `json:"message"` Type string `json:"type"` }
APIError represents an error that occured on an API
type APIErrorResponse ¶ added in v1.1.2
type APIErrorResponse struct {
Error APIError `json:"error"`
}
APIErrorResponse is the full error respnose that has been returned by an API.
type Client ¶
type Client interface { // Engines lists the currently available engines, and provides basic information about each // option such as the owner and availability. Engines(ctx context.Context) (*EnginesResponse, error) // Engine retrieves an engine instance, providing basic information about the engine such // as the owner and availability. Engine(ctx context.Context, engine string) (*EngineObject, error) // Completion creates a completion with the default engine. This is the main endpoint of the API // which auto-completes based on the given prompt. Completion(ctx context.Context, request CompletionRequest) (*CompletionResponse, error) // CompletionStream creates a completion with the default engine and streams the results through // multiple calls to onData. CompletionStream(ctx context.Context, request CompletionRequest, onData func(*CompletionResponse)) error // CompletionWithEngine is the same as Completion except allows overriding the default engine on the client CompletionWithEngine(ctx context.Context, engine string, request CompletionRequest) (*CompletionResponse, error) // CompletionStreamWithEngine is the same as CompletionStream except allows overriding the default engine on the client CompletionStreamWithEngine(ctx context.Context, engine string, request CompletionRequest, onData func(*CompletionResponse)) error // Search performs a semantic search over a list of documents with the default engine. Search(ctx context.Context, request SearchRequest) (*SearchResponse, error) // SearchWithEngine performs a semantic search over a list of documents with the specified engine. SearchWithEngine(ctx context.Context, engine string, request SearchRequest) (*SearchResponse, error) }
A Client is an API client to communicate with the OpenAI gpt-3 APIs
func NewClient ¶
func NewClient(apiKey string, options ...ClientOption) Client
NewClient returns a new OpenAI GPT-3 API client. An apiKey is required to use the client
type ClientOption ¶
type ClientOption func(*client) error
ClientOption are options that can be passed when creating a new client
func WithBaseURL ¶
func WithBaseURL(baseURL string) ClientOption
WithBaseURL is a client option that allows you to override the default base url of the client. The default base url is "https://api.openai.com/v1"
func WithDefaultEngine ¶
func WithDefaultEngine(engine string) ClientOption
WithDefaultEngine is a client option that allows you to override the default engine of the client
func WithHTTPClient ¶ added in v1.1.2
func WithHTTPClient(httpClient *http.Client) ClientOption
WithHTTPClient allows you to override the internal http.Client used
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout is a client option that allows you to override the default timeout duration of requests for the client. The default is 30 seconds. If you are overriding the http client as well, just include the timeout there.
func WithUserAgent ¶
func WithUserAgent(userAgent string) ClientOption
WithUserAgent is a client option that allows you to override the default user agent of the client
type CompletionRequest ¶
type CompletionRequest struct { // The engine ID EngineID string `json:"engine_id,omitempty"` // A list of string prompts to use. // TODO there are other prompt types here for using token integers that we could add support for. Prompt []string `json:"prompt"` // How many tokens to complete up to. Max of 512 MaxTokens *int `json:"max_tokens,omitempty"` // Sampling temperature to use Temperature *float32 `json:"temperature,omitempty"` // Alternative to temperature for nucleus sampling TopP *float32 `json:"top_p,omitempty"` // How many choice to create for each prompt N *float32 `json:"n"` // Include the probabilities of most likely tokens LogProbs *int `json:"logprobs"` // Echo back the prompt in addition to the completion Echo bool `json:"echo"` // Up to 4 sequences where the API will stop generating tokens. Response will not contain the stop sequence. Stop []string `json:"stop,omitempty"` // PresencePenalty number between 0 and 1 that penalizes tokens that have already appeared in the text so far. PresencePenalty float32 `json:"presence_penalty"` // FrequencyPenalty number between 0 and 1 that penalizes tokens on existing frequency in the text so far. FrequencyPenalty float32 `json:"frequency_penalty"` // Whether to stream back results or not. Don't set this value in the request yourself // as it will be overriden depending on if you use CompletionStream or Completion methods. Stream bool `json:"stream,omitempty"` }
CompletionRequest is a request for the completions API
type CompletionResponse ¶
type CompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int `json:"created"` Model string `json:"model"` Choices []CompletionResponseChoice `json:"choices"` }
CompletionResponse is the full response from a request to the completions API
type CompletionResponseChoice ¶
type CompletionResponseChoice struct { Text string `json:"text"` Index int `json:"index"` LogProbs *int `json:"logprobs"` FinishReason string `json:"finish_reason"` }
CompletionResponseChoice is one of the choices returned in the response to the Completions API
type EngineObject ¶
type EngineObject struct { ID string `json:"id"` Object string `json:"object"` Owner string `json:"owner"` Ready bool `json:"ready"` }
EngineObject contained in an engine reponse
type EnginesResponse ¶
type EnginesResponse struct { Data []EngineObject `json:"data"` Object string `json:"object"` }
EnginesResponse is returned from the Engines API
type SearchData ¶
type SearchData struct { Document int `json:"document"` Object string `json:"object"` Score float64 `json:"score"` }
SearchData is a single search result from the document search API
type SearchRequest ¶
type SearchRequest struct { EngineID string `json:"engine_id"` Documents []string `json:"documents"` Query string `json:"query"` }
SearchRequest is a request for the document search API
type SearchResponse ¶
type SearchResponse struct { Data []SearchData `json:"data"` Object string `json:"object"` }
SearchResponse is the full response from a request to the document search API