gpt

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

gpt Go Package

The gpt package is a Go library designed to facilitate easy communication with the GPT-3 API. This library enables you to perform text completion operations using OpenAI's GPT-3 model.

Installation

To include this package as a Go module in your project:

go get github.com/ssibrahimbas/gpt

Usage

Below is a basic usage example:

package main

import (
 "fmt"
 "github.com/ssibrahimbas/gpt"
)

func main() {
 client := gpt.New(gpt.Config{
  ApiKey: "YOUR_API_KEY",
 })
 res, err := client.Complete(gpt.CompletionRequest{
  Messages: []gpt.Message{
   {
    Role:    gpt.RoleUser,
    Content: "Hello, how are you?",
   },
  },
  Model:            gpt.ModelGpt3Turbo,
  Temperature:      0.7,
  MaxTokens:        64,
  TopP:             1,
  FrequencyPenalty: 0,
  PresencePenalty:  0,
 })
 if err != nil {
  panic(err)
 }
 fmt.Println(res.Choices[0].Message.Content)
}

In this example, we're sending a completion request to the GPT-3 API and printing the generated text response from the model.

Configuration

The gpt.New function takes a Config object containing your API key. You can obtain this key from OpenAI's official website.

Documentation

For detailed documentation and further usage examples, please refer to the Go documentation comments within the package. You can also view the documentation online at pkg.go.dev.

License

This project is licensed under the Apache 2.0 license. Please refer to the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BaseURL = "https://api.openai.com"

BaseURL is the base URL for the GPT API.

View Source
var Paths = paths{
	Completion: "/v1/chat/completions",
}

Paths contains the paths for the different GPT API endpoints. It is used to construct the full URL for each endpoint.

Functions

This section is empty.

Types

type Choice

type Choice struct {
	Index        int     `json:"index"`         // Index is the position of the choice in the list.
	Message      Message `json:"message"`       // Message contains the generated output.
	FinishReason string  `json:"finish_reason"` // FinishReason indicates why the generation was stopped.
}

Choice represents a single generated output.

type Client

type Client interface {
	// Complete sends a request to the GPT API to generate a completion based on the provided input.
	// It returns a response containing the model's output or an error if something went wrong.
	Complete(CompletionRequest) (*CompletionResponse, error)
}

Client defines the interface for interacting with the GPT API. It provides a method to generate completions based on the provided input.

func New

func New(cnf Config) Client

type Completion

type Completion struct {
	PromptTokens     int `json:"prompt_tokens"`     // PromptTokens is the number of tokens used in the input.
	CompletionTokens int `json:"completion_tokens"` // CompletionTokens is the number of tokens in the generated output.
	TotalTokens      int `json:"total_tokens"`      // TotalTokens is the total number of tokens used.
}

Completion provides details about token usage for a request.

type CompletionRequest

type CompletionRequest struct {
	Model            Model     `json:"model"`             // Model specifies the GPT model to be used for generating the completion.
	Messages         []Message `json:"messages"`          // Messages contains the conversation history.
	Temperature      float32   `json:"temperature"`       // Temperature controls the randomness of the model's output.
	MaxTokens        int       `json:"max_tokens"`        // MaxTokens limits the length of the generated output.
	TopP             float32   `json:"top_p"`             // TopP controls the diversity of the model's output.
	FrequencyPenalty float32   `json:"frequency_penalty"` // FrequencyPenalty penalizes more frequent tokens.
	PresencePenalty  float32   `json:"presence_penalty"`  // PresencePenalty penalizes new tokens.
}

CompletionRequest represents the input data required to generate completions using the GPT API.

type CompletionResponse

type CompletionResponse struct {
	Id      string     `json:"id"`      // Id is a unique identifier for the response.
	Object  string     `json:"object"`  // Object indicates the type of the response.
	Created int        `json:"created"` // Created is a timestamp of when the response was generated.
	Model   Model      `json:"model"`   // Model specifies which GPT model was used.
	Choices []Choice   `json:"choices"` // Choices contains the generated outputs.
	Usage   Completion `json:"usage"`   // Usage provides details about token usage for the request.
}

CompletionResponse represents the model's output for a given CompletionRequest.

type Config

type Config struct {
	ApiKey  string
	BaseURL string
}

type Message

type Message struct {
	Role    Role   `json:"role"`    // Role indicates who sent the message (e.g., user, bot).
	Content string `json:"content"` // Content contains the text of the message.
}

Message represents a single message in a conversation.

type Model

type Model string

Model represents the different GPT models available for generating completions.

const (
	ModelGpt3Turbo        Model = "gpt-3.5-turbo"          // ModelGpt3Turbo represents the GPT-3.5 Turbo model.
	ModelGpt3Turbo16K0613 Model = "gpt-3.5-turbo-16k-0613" // ModelGpt3Turbo16K0613 represents the GPT-3.5 Turbo 16K 0613 model.
	ModelGpt3Turbo16K     Model = "gpt-3.5-turbo-16k"      // ModelGpt3Turbo16K represents the GPT-3.5 Turbo 16K model.
	ModelGpt3Turbo0613    Model = "gpt-3.5-turbo-0613"     // ModelGpt3Turbo0613 represents the GPT-3.5 Turbo 0613 model.
	ModelGpt3Turbo0301    Model = "gpt-3.5-turbo-0301"     // ModelGpt3Turbo0301 represents the GPT-3.5 Turbo 0301 model.

	ModelGpt4     Model = "gpt-4"      // ModelGpt4 represents the GPT-4 model.
	ModelGpt40314 Model = "gpt-4-0314" // ModelGpt40314 represents the GPT-4 0314 model.
	ModelGpt40613 Model = "gpt-4-0613" // ModelGpt40613 represents the GPT-4 0613 model.
)

type Object

type Object string

Object represents the different types of responses that can be received from the GPT API.

const (
	ObjectChatCompletion Object = "chat.completion" // ObjectChatCompletion indicates a completion response.
)

type Role

type Role string

Role represents the different roles that can send messages in a conversation.

const (
	RoleUser   Role = "user"   // RoleUser represents a message sent by the user.
	RoleBot    Role = "bot"    // RoleBot represents a message sent by the bot.
	RoleSystem Role = "system" // RoleSystem represents a system-generated message.
)

Jump to

Keyboard shortcuts

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