gogpt

package module
v0.0.0-...-f66cba2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

go-gpt3

GoDoc Go Report Card

OpenAI GPT-3 API wrapper for Go

Installation:

go get github.com/aknuds1/go-gpt3

Example usage:

package main

import (
	"context"
	"fmt"
	gogpt "github.com/aknuds1/go-gpt3"
)

func main() {
	c := gogpt.NewClient("your token")
	ctx := context.Background()

	req := gogpt.CompletionRequest{
		Model: "ada",
		MaxTokens: 5,
		Prompt:    "Lorem ipsum",
	}
	resp, err := c.CreateCompletion(ctx, req)
	if err != nil {
		return
	}
	fmt.Println(resp.Choices[0].Text)
}

Documentation

Overview

common.go defines common types used throughout the OpenAI API.

Index

Constants

View Source
const (
	GPT3TextDavinci003      = "text-davinci-003"
	GPT3TextDavinci002      = "text-davinci-002"
	GPT3TextCurie001        = "text-curie-001"
	GPT3TextBabbage001      = "text-babbage-001"
	GPT3TextAda001          = "text-ada-001"
	GPT3TextDavinci001      = "text-davinci-001"
	GPT3DavinciInstructBeta = "davinci-instruct-beta"
	GPT3Davinci             = "davinci"
	GPT3CurieInstructBeta   = "curie-instruct-beta"
	GPT3Curie               = "curie"
	GPT3Ada                 = "ada"
	GPT3Babbage             = "babbage"
)

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.

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

View Source
const (
	CreateImageSize256x256   = "256x256"
	CreateImageSize512x512   = "512x512"
	CreateImageSize1024x1024 = "1024x1024"
)

Image sizes defined by the OpenAI API.

View Source
const (
	CreateImageResponseFormatURL     = "url"
	CreateImageResponseFormatB64JSON = "b64_json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnswerRequest

type AnswerRequest struct {
	Documents       []string   `json:"documents,omitempty"`
	File            string     `json:"file,omitempty"`
	Question        string     `json:"question"`
	SearchModel     string     `json:"search_model,omitempty"`
	Model           string     `json:"model"`
	ExamplesContext string     `json:"examples_context"`
	Examples        [][]string `json:"examples"`
	MaxTokens       int        `json:"max_tokens,omitempty"`
	Stop            []string   `json:"stop,omitempty"`
	Temperature     *float64   `json:"temperature,omitempty"`
}

type AnswerResponse

type AnswerResponse struct {
	Answers           []string `json:"answers"`
	Completion        string   `json:"completion"`
	Model             string   `json:"model"`
	Object            string   `json:"object"`
	SearchModel       string   `json:"search_model"`
	SelectedDocuments []struct {
		Document int    `json:"document"`
		Text     string `json:"text"`
	} `json:"selected_documents"`
}

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Client is OpenAI GPT-3 API client.

func NewClient

func NewClient(authToken string) *Client

NewClient creates new OpenAI API client.

func NewOrgClient

func NewOrgClient(authToken, org string) *Client

NewOrgClient creates new OpenAI API client for specified Organization ID.

func (*Client) Answers

func (c *Client) Answers(ctx context.Context, request AnswerRequest) (response AnswerResponse, err error)

Search — perform a semantic search api call over a list of documents.

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.

func (*Client) CreateEmbeddings

func (c *Client) CreateEmbeddings(ctx context.Context, request EmbeddingRequest) (resp EmbeddingResponse, err error)

CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |request.Input|. https://beta.openai.com/docs/api-reference/embeddings/create

func (*Client) CreateFile

func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error)

CreateFile uploads a jsonl file to GPT3 FilePath can be either a local file path or a URL.

func (*Client) CreateFineTune

func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (FineTuneResponse, error)

CreateFineTune requests creating an OpenAI fine-tune.

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.

func (*Client) DeleteFile

func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error)

DeleteFile deletes an existing file.

func (*Client) Edits

func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error)

Perform an API call to the Edits endpoint.

func (*Client) GetEngine

func (c *Client) GetEngine(
	ctx context.Context,
	engineID string,
) (engine Engine, err error)

GetEngine Retrieves an engine instance, providing basic information about the engine such as the owner and availability.

func (*Client) GetFile

func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error)

GetFile Retrieves a file instance, providing basic information about the file such as the file name and purpose.

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

func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error)

ListFiles Lists the currently available files, and provides basic information about each file such as the file name and purpose.

func (*Client) ListFineTunes

func (c *Client) ListFineTunes(ctx context.Context) ([]FineTuneJob, error)

ListFineTunes lists the organization's OpenAI fine-tune jobs.

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.

func (*Client) RetrieveFineTune

func (c *Client) RetrieveFineTune(ctx context.Context, id string) (FineTuneJob, error)

RetrieveFineTune requests the retrieval of an OpenAI fine-tune.

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           string         `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        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 uint64             `json:"created"`
	Model   string             `json:"model"`
	Choices []CompletionChoice `json:"choices"`
	Usage   Usage              `json:"usage"`
}

CompletionResponse represents a response structure for completion API.

type EditsChoice

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

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 uint64        `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 []float64 `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
	AdaSimilarity
	BabbageSimilarity
	CurieSimilarity
	DavinciSimilarity
	AdaSearchDocument
	AdaSearchQuery
	BabbageSearchDocument
	BabbageSearchQuery
	CurieSearchDocument
	CurieSearchQuery
	DavinciSearchDocument
	DavinciSearchQuery
	AdaCodeSearchCode
	AdaCodeSearchText
	BabbageCodeSearchCode
	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 is a slice of strings for which you want to generate an Embedding vector.
	// Each input must not exceed 2048 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"`
}

EmbeddingRequest is the input to a Create embeddings request.

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 *struct {
		Code    *int    `json:"code,omitempty"`
		Message string  `json:"message"`
		Param   *string `json:"param,omitempty"`
		Type    string  `json:"type"`
	} `json:"error,omitempty"`
}

type File

type File struct {
	Bytes     int    `json:"bytes"`
	CreatedAt int    `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 FileRequest struct {
	FileName string `json:"file"`
	FilePath string `json:"-"`
	Purpose  string `json:"purpose"`
}

type FilesList

type FilesList struct {
	Files []File `json:"data"`
}

FilesList is a list of files that belong to the user or organization.

type FineTuneEvent

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

type FineTuneJob

type FineTuneJob struct {
	ID              string          `json:"id"`
	Object          string          `json:"object"`
	Model           string          `json:"model"`
	CreatedAt       int64           `json:"created_at"`
	Events          []FineTuneEvent `json:"events"`
	FineTunedModel  string          `json:"fine_tuned_model"`
	OrganizationID  string          `json:"organization_id"`
	ResultFiles     []File          `json:"result_files"`
	Status          string          `json:"status"`
	ValidationFiles []File          `json:"validation_files"`
	TrainingFiles   []File          `json:"training_files"`
}

FineTuneJob represents an OpenAI fine-tune job.

type FineTuneRequest

type FineTuneRequest struct {
	TrainingFile string `json:"training_file"`
	Model        string `json:"model,omitempty"`
	Suffix       string `json:"suffix,omitempty"`
}

FineTuneRequest represents a request to create an OpenAI fine-tune.

type FineTuneResponse

type FineTuneResponse struct {
	ID     string          `json:"id"`
	Model  string          `json:"model"`
	Status string          `json:"status"`
	Events []FineTuneEvent `json:"events"`
}

FineTuneResponse represents a response to a request for creating an OpenAI fine-tune.

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

ImageResponseData represents a response data structure for 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 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 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.

type Usage

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

Usage Represents the total token usage per request to OpenAI.

Jump to

Keyboard shortcuts

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