gogpt

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 7 Imported by: 0

README

gogpt

This is a simple module for accessing ChatGPT from golang.

Using

Run a basic query in one line...

generated, err := NewGoGPTQuery(OPENAI_KEY).AddMessage(gogpt.ROLE_SYSTEM, "You are a detective.").AddMessage(gogpt.ROLE_USER, "Solve the Great Train Mystery").Generate()
fmt.Printf("Result: %s\n",generated.Choices[0].Message.Content)

Just keep adding messages to make a simple chat...

gpt := NewGoGPTQuery(OPENAI_KEY)
gpt.AddMessage(gogpt.ROLE_SYSTEM, "You are a detective.").AddMessage(gogpt.ROLE_USER, "Solve the Great Train Mystery.").AddMessage(gogpt.ROLE_ASSISTANT,"Ok! I got this.").AddMessage(gogpt.ROLE_USER,"And hurry!").Generate()

Testing

If you want to test this module, copy the file testconfig-sample.json to testconfig.json and replace the org id and api key with your settings. You can change anything else as well, but you'll need a working API key.

Run go test -v to see verbose output.

BE AWARE TESTS ARE ON THE LIVE API. You will be using tokens, although max_tokens is set to 100 for each query. Total usage for the test suite is around 1,000 tokens.

Documentation

Index

Constants

View Source
const (
	API_ENDPOINT        = "https://api.openai.com/v1/chat/completions"
	EMBEDDINGS_ENDPOINT = "https://api.openai.com/v1/embeddings"
	MODEL_35_TURBO      = "gpt-3.5-turbo-1106"
	MODEL_4_TURBO       = "gpt-4-1106-preview"
	MODEL_4             = "gpt-4"
	MODEL_EMBEDDING_ADA = "text-embedding-ada-002"
	ROLE_SYSTEM         = "system"
	ROLE_USER           = "user"
	ROLE_ASSISTANT      = "assistant"
	ROLE_FUNCTION       = "function"
	RETRIES             = 3
)
View Source
const (
	// Since token estimates are inexact, how much of a buffer should we leave?
	BUFF_MARGIN = 48
)

Variables

This section is empty.

Functions

func MaxQueryTokens added in v0.0.2

func MaxQueryTokens(model string) int

func TokenEstimator added in v0.0.2

func TokenEstimator(msg GoGPTMessage, model string) int

This is an estimate of the number of tokens in a string.

Types

type EmbeddingData added in v0.0.5

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

type GoGPTChat added in v0.0.2

type GoGPTChat struct {
	Query        *GoGPTQuery
	Summary      string
	MessageQueue []GoGPTMessage
	// contains filtered or unexported fields
}

func NewGoGPTChat added in v0.0.2

func NewGoGPTChat(key string) *GoGPTChat

func (*GoGPTChat) AddMessage added in v0.0.2

func (c *GoGPTChat) AddMessage(role string, name string, content string) *GoGPTChat

A convenience function for method chaining

func (*GoGPTChat) Generate added in v0.0.2

func (g *GoGPTChat) Generate() (*GoGPTResponse, error)

A function that encapsulates the query generation method and handles summariation.

type GoGPTChoice

type GoGPTChoice struct {
	Index        int          `json:"index"`
	Message      GoGPTMessage `json:"message"`
	FinishReason string       `json:"finish_reason"`
}

type GoGPTEmbeddings added in v0.0.5

type GoGPTEmbeddings struct {
	Model  string          `json:"model"`
	Object string          `json:"object"`
	Data   []EmbeddingData `json:"data"`
	Usage  GoGPTUsage      `json:"usage"`
}

func GetEmbedding added in v0.0.5

func GetEmbedding(input string, key string) (*GoGPTEmbeddings, error)

type GoGPTEmbeddingsRequest added in v0.0.5

type GoGPTEmbeddingsRequest struct {
	Input string `json:"input"`
	Model string `json:"model"`
}

type GoGPTError

type GoGPTError struct {
	Message string      `json:"message"`
	ErrType string      `json:"type"`
	Param   string      `json:"param"`
	Code    interface{} `json:"code"`
}

type GoGPTFunction

type GoGPTFunction struct {
	Name        string             `json:"name"`
	Description string             `json:"description,omitempty"`
	Parameters  *jsonschema.Schema `json:"parameters"`
}

type GoGPTFunctionCall

type GoGPTFunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type GoGPTMessage

type GoGPTMessage struct {
	Role         string             `json:"role"`
	Content      string             `json:"content"`
	Name         string             `json:"name,omitempty"`
	FunctionCall *GoGPTFunctionCall `json:"function_call,omitempty"`
}

type GoGPTQuery

type GoGPTQuery struct {
	Model           string             `json:"model"`
	Messages        []GoGPTMessage     `json:"messages"`
	Functions       []GoGPTFunction    `json:"functions,omitempty"`
	FunctionCall    string             `json:"function_call,omitempty"`
	Temperature     float32            `json:"temperature,omitempty"`
	TopP            float32            `json:"top_p,omitempty"`
	N               int                `json:"n,omitempty"`
	Stream          bool               `json:"stream,omitempty"`
	Stop            string             `json:"stop,omitempty"`
	MaxTokens       int                `json:"max_tokens,omitempty"`
	PresencePenalty float32            `json:"presence_penalty,omitempty"`
	LogitBias       map[string]float32 `json:"logit_bias,omitempty"`
	User            string             `json:"user,omitempty"`
	Key             string             `json:"-"`
	OrgName         string             `json:"-"`
	OrgId           string             `json:"-"`
	Endpoint        string             `json:"-"`
	Timeout         time.Duration      `json:"-"`
}

func NewGoGPTQuery

func NewGoGPTQuery(key string) *GoGPTQuery

func (*GoGPTQuery) AddFunction

func (g *GoGPTQuery) AddFunction(name string, desc string, obj interface{}) (*GoGPTQuery, error)

func (*GoGPTQuery) AddMessage

func (g *GoGPTQuery) AddMessage(role string, name string, content string) *GoGPTQuery

func (*GoGPTQuery) Generate

func (g *GoGPTQuery) Generate() (*GoGPTResponse, error)

type GoGPTResponse

type GoGPTResponse struct {
	Error   *GoGPTError   `json:"error,omitempty"`
	Id      string        `json:"id"`
	Object  string        `json:"object"`
	Created int32         `json:"created"`
	Model   string        `json:"model"`
	Choices []GoGPTChoice `json:"choices"`
	Usage   GoGPTUsage    `json:"usage"`
}

type GoGPTUsage

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

Jump to

Keyboard shortcuts

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