gpt3

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2020 License: MIT Imports: 7 Imported by: 106

README

go-gpt3

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

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
Full Examples

Try out any of these examples with putting the contents in a main.go and running GO111MODULE=on go run main.go. You will also need to have a .env file that looks like:

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)
  • Document Search API
  • Overriding default url, user-agent, timeout, and other options
  • TODO: Streaming on the Completion API

Documentation

Index

Constants

View Source
const (
	AdaEngine     = "ada"
	BabbageEngine = "babbage"
	CurieEngine   = "curie"
	DavinciEngine = "davinci"
	DefaultEngine = DavinciEngine
)

Engine Types

Variables

This section is empty.

Functions

func IntPtr

func IntPtr(i int) *int

IntPtr converts an integer to an *int as a convenience

Types

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.
	// Returns new text as well as, if requested, the probabilities over each alternative token at
	// each position.
	Completion(ctx context.Context, request CompletionRequest) (*CompletionResponse, error)

	// CompletionWithEngine creates a completion with the specified engine. 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.
	CompletionWithEngine(ctx context.Context, engine string, request CompletionRequest) (*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 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.

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

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:"search_resulet"`
	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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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