gogpt

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 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/sashabaranov/go-gpt3

Example usage:

package main

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

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

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

	searchReq := gogpt.SearchRequest{
		Documents: []string{"White House", "hospital", "school"},
		Query:     "the president",
	}
	searchResp, err := c.Search(ctx, "ada", searchReq)
	if err != nil {
		return
	}
	fmt.Println(searchResp.SearchResults)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnswerRequest

type AnswerRequest struct {
	Documents       []*string  `json:"documents,omitempty"`
	Examples        [][]string `json:"examples"`
	ExamplesContext string     `json:"examples_context"`
	Expand          *bool      `json:"expand,omitempty"`
	File            *string    `json:"file,omitempty"`
	LogProbs        *int       `json:"logprobs,omitempty"`
	MaxRerank       *int       `json:"max_rerank,omitempty"`
	MaxTokens       *int       `json:"max_tokens,omitempty"`
	Model           string     `json:"model"`
	N               *int       `json:"n,omitempty"`
	Question        string     `json:"question"`
	ReturnMetadata  *bool      `json:"return_metadata,omitempty"`
	ReturnPrompt    *bool      `json:"return_prompt,omitempty"`
	SearchModel     *string    `json:"search_model,omitempty"`
	Stop            []*string  `json:"stop,omitempty"`
	Temperature     *float64   `json:"temperature,omitempty"`
	UserID          *string    `json:"user,omitempty"`
}

type AnswerResponse

type AnswerResponse struct {
	Answers           []string `json:"answers"`
	Completion        string   `json:"completion"`
	Model             string   `json:"model"`
	Object            string   `json:"object"`
	Prompt            *string  `json:"prompt,omitempty"`
	SearchModel       string   `json:"search_model"`
	SelectedDocuments []struct {
		Document int     `json:"document"`
		Metadata *string `json:"metadata,omitempty"`
		Score    float64 `json:"score"`
		Text     string  `json:"text"`
	} `json:"selected_documents"`
}

type Choice

type Choice struct {
	Text         string        `json:"text"`
	Index        int           `json:"index"`
	FinishReason string        `json:"finish_reason"`
	LogProbs     LogprobResult `json:"logprobs"`
}

Choice represents one of possible completions

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, engineID string, 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.

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) DeleteFile

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

DeleteFile deletes an existing file

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) Search

func (c *Client) Search(ctx context.Context, engineID string, request SearchRequest) (response SearchResponse, err error)

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

type CompletionRequest

type CompletionRequest struct {
	Prompt    string `json:"prompt,omitempty"`
	MaxTokens int    `json:"max_tokens,omitempty"`

	Temperature float32 `json:"temperature,omitempty"`
	TopP        float32 `json:"top_p,omitempty"`

	N int `json:"n,omitempty"`

	LogProbs int `json:"logprobs,omitempty"`

	Model *string `json:"model,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"`
	UserID           *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 []Choice `json:"choices"`
}

CompletionResponse represents a response structure for completion API

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

type SearchRequest struct {
	Documents      []*string `json:"documents,omitempty"`
	File           *string   `json:"file,omitempty"`
	MaxRerank      *int      `json:"max_rerank,omitempty"`
	Query          string    `json:"query"`
	ReturnMetadata *bool     `json:"return_metadata,omitempty"`
	UserID         *string   `json:"user,omitempty"`
}

SearchRequest represents a request structure for search API

type SearchResponse

type SearchResponse struct {
	SearchResults []SearchResult `json:"data"`
}

SearchResponse represents a response structure for search API

type SearchResult

type SearchResult struct {
	Document int     `json:"document"`
	Metadata *string `json:"metadata,omitempty"`
	Object   string  `json:"object"`
	Score    float32 `json:"score"`
	Text     string  `json:"text"`
}

SearchResult represents single result from search API

Jump to

Keyboard shortcuts

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