gpt3

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 11 Imported by: 0

README

go-gpt3

An OpenAI GPT-3 API client enabling Go/Golang programs to interact with the gpt3 APIs.

Supports using the completion APIs with or without streaming.

PkgGoDev

Usage

Simple usage to call the main gpt-3 API, completion:

client := gpt3.NewClient(apiKey)
resp, err := client.Completion(ctx, gpt3.CompletionRequest{
    Prompt: []string{"2, 3, 5, 7, 11,"},
})

fmt.Print(resp.Choices[0].Text)
// prints " 13, 17, 19, 23, 29, 31", etc

Documentation

Check out the go docs for more detailed documentation on the types and methods provided: https://pkg.go.dev/github.com/PullRequestInc/go-gpt3

Full Examples

Try out any of these examples with putting the contents in a main.go and running go run main.go. I would recommend using go modules in which case you will also need to run go mod init within your test repo. Alternatively you can clone this repo and run the test script with go run cmd/test/main.go.

You will also need to have a .env file that looks like this to use these examples:

API_KEY=<openAI API Key>
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/PullRequestInc/go-gpt3"
	"github.com/joho/godotenv"
)

func main() {
	godotenv.Load()

	apiKey := os.Getenv("API_KEY")
	if apiKey == "" {
		log.Fatalln("Missing API KEY")
	}

	ctx := context.Background()
	client := gpt3.NewClient(apiKey)

	resp, err := client.Completion(ctx, gpt3.CompletionRequest{
		Prompt:    []string{"The first thing you should know about javascript is"},
		MaxTokens: gpt3.IntPtr(30),
		Stop:      []string{"."},
		Echo:      true,
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(resp.Choices[0].Text)
}

Support

  • List Engines API
  • Get Engine API
  • Completion API (this is the main gpt-3 API)
  • Streaming support for the Completion API
  • Document Search API
  • Overriding default url, user-agent, timeout, and other options

Powered by

Documentation

Index

Constants

View Source
const (
	TextAda001Engine     = "text-ada-001"
	TextBabbage001Engine = "text-babbage-001"
	TextCurie001Engine   = "text-curie-001"
	TextDavinci001Engine = "text-davinci-001"
	AdaEngine            = "ada"
	BabbageEngine        = "babbage"
	CurieEngine          = "curie"
	DavinciEngine        = "davinci"
	DefaultEngine        = DavinciEngine
)

Engine Types

View Source
const (
	FineTunePurpose        = "fine-tune"
	SearchPurpose          = "search"
	AnswersPurpose         = "answers"
	ClassificationsPurpose = "classifications"
)
View Source
const (
	TextSimilarityAda001     = "text-similarity-ada-001"
	TextSimilarityBabbage001 = "text-similarity-babbage-001"
	TextSimilarityCurie001   = "text-similarity-curie-001"
	TextSimilarityDavinci001 = "text-similarity-davinci-001"
)
View Source
const (
	TextSearchAdaDoc001       = "text-search-ada-doc-001"
	TextSearchAdaQuery001     = "text-search-ada-query-001"
	TextSearchBabbageDoc001   = "text-search-babbage-doc-001"
	TextSearchBabbageQuery001 = "text-search-babbage-query-001"
	TextSearchCurieDoc001     = "text-search-curie-doc-001"
	TextSearchCurieQuery001   = "text-search-curie-query-001"
	TextSearchDavinciDoc001   = "text-search-davinci-doc-001"
	TextSearchDavinciQuery001 = "text-search-davinci-query-001"
)
View Source
const (
	CodeSearchAdaCode001     = "code-search-ada-code-001"
	CodeSearchAdaText001     = "code-search-ada-text-001"
	CodeSearchBabbageCode001 = "code-search-babbage-code-001"
	CodeSearchBabbageText001 = "code-search-babbage-text-001"
)

Variables

This section is empty.

Functions

func Float32Ptr

func Float32Ptr(f float32) *float32

Float32Ptr converts a float32 to a *float32 as a convenience

func IntPtr

func IntPtr(i int) *int

IntPtr converts an integer to an *int as a convenience

Types

type APIError

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

func (APIError) Error

func (e APIError) Error() string

type APIErrorResponse

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

	// Given a prompt and an instruction, the model will return an edited version of the prompt.
	Edits(ctx context.Context, request EditsRequest) (*EditsResponse, 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)

	//UploadFile Uploads a file that contains document(s) to be used across various endpoints/features.
	UploadFile(ctx context.Context, filename string, purpose string) (*FileUploadResponse, error)

	//DeleteFile deletes a file from the server-side storage identified by id
	DeleteFile(ctx context.Context, fileId string) (*FileDeleteResponse, error)

	//CreateFineTune Creates a job that fine-tunes a specified model from a given dataset.
	CreateFineTune(ctx context.Context, fileId string) (*FineTuneResponse, error)

	//CreateFineTuneWithOptions Creates a job that fine-tunes a specified model from a given dataset.
	CreateFineTuneWithOptions(ctx context.Context, fineTuneOptions FineTuneOptions) (*FineTuneResponse, error)

	//GetFineTune Gets info about the fine-tune job.
	GetFineTune(ctx context.Context, id string) (*FineTuneResponse, error)

	CreateEmbeddings(ctx context.Context, model string, input []string) (*EmbeddingsResponse, 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

func WithHTTPClient(httpClient *http.Client) ClientOption

WithHTTPClient allows you to override the internal http.Client used

func WithOrg

func WithOrg(id string) ClientOption

WithOrg is a client option that allows you to override the organization ID

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 {
	// 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"`
	//for edits
	Suffix *string `json:"suffix"`
	// 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 *int `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     LogprobResult `json:"logprobs"`
	FinishReason string        `json:"finish_reason"`
}

CompletionResponseChoice is one of the choices returned in the response to the Completions API

type EditsRequest

type EditsRequest struct {
	// 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 string `json:"model"`
	// The input text to use as a starting point for the edit.
	Input string `json:"input"`
	// The instruction that tells the model how to edit the prompt.
	Instruction string `json:"instruction"`
	// Sampling temperature to use
	Temperature *float32 `json:"temperature,omitempty"`
	// Alternative to temperature for nucleus sampling
	TopP *float32 `json:"top_p,omitempty"`
	// How many edits to generate for the input and instruction. Defaults to 1
	N *int `json:"n"`
}

EditsRequest is a request for the edits API

type EditsResponse

type EditsResponse struct {
	Object  string                `json:"object"`
	Created int                   `json:"created"`
	Choices []EditsResponseChoice `json:"choices"`
	Usage   EditsResponseUsage    `json:"usage"`
}

EditsResponse is the full response from a request to the edits API

type EditsResponseChoice

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

EditsResponseChoice is one of the choices returned in the response to the Edits API

type EditsResponseUsage

type EditsResponseUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

EditsResponseUsage is a structure used in the response from a request to the edits API

type Embedding

type Embedding struct {
	Object    string    `json:"object"`
	Embedding []float64 `json:"embedding"`
	Index     int       `json:"index"`
}

type EmbeddingsRequest

type EmbeddingsRequest struct {
	Model string   `json:"model"`
	Input []string `json:"input"`
	User  string   `json:"user"`
}

type EmbeddingsResponse

type EmbeddingsResponse struct {
	Object string                  `json:"object"`
	Usage  EmbeddingsResponseUsage `json:"usage"`
	Data   []Embedding             `json:"data"`
}

type EmbeddingsResponseUsage

type EmbeddingsResponseUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

EmbeddingsResponseUsage is a structure used in the response from a request to the edits 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 Event

type Event struct {
	Object    string `json:"object"`
	CreatedAt int    `json:"created_at"`
	Level     string `json:"level"`
	Message   string `json:"message"`
}

type File

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

type FileDeleteResponse

type FileDeleteResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
}

type FileUploadResponse

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

type FineTuneOptions

type FineTuneOptions struct {
	TrainingFile                 string     `json:"training_file"`
	BatchSize                    int        `json:"batch_size"`
	LearningRateMultiplier       float64    `json:"learning_rate_multiplier"`
	NEpochs                      int        `json:"n_epochs"`
	PromptLessWeight             float64    `json:"prompt_loss_weight"`
	ComputeClassificatioNMetrics bool       `json:"compute_classification_metrics"`
	ClassificationNClasses       *int       `json:"classification_n_classes,omitempty"`
	ClassificationPositiveClass  *string    `json:"classification_positive_class,omitempty"`
	ClassificationBetas          *[]float64 `json:"classification_betas,omitempty"`
	Suffix                       *string    `json:"suffix,omitempty"`
}

type FineTuneResponse

type FineTuneResponse struct {
	ID              string  `json:"id"`
	Object          string  `json:"object"`
	Model           string  `json:"model"`
	CreatedAt       int     `json:"created_at"`
	Events          []Event `json:"events"`
	TrainingFiles   []File  `json:"training_files"`
	ResultFiles     []File  `json:"result_files"`
	ValidationFiles []File  `json:"validation_files"`
	UpdatedAt       int     `json:"updated_at"`
	Status          string  `json:"status"`
	OrganizationID  string  `json:"organization_id"`
	HyperParams     HyperParams
	FineTunedModel  *string `json:"fine_tuned_model"`
}

type HyperParams

type HyperParams struct {
	BatchSize              int     `json:"batch_size"`
	LearningRateMultiplier float64 `json:"learning_rate_multiplier"`
	NEpochs                int     `json:"n_epochs"`
	PromptLessWeight       float64 `json:"prompt_loss_weight"`
}

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 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 {
	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

Directories

Path Synopsis
cmd
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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