chatgpt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 6 Imported by: 13

README

Go-ChatGPT

Go-ChatGPT is an open-source GoLang client for ChatGPT, a large language model trained by OpenAI. With Go-ChatGPT, you can quickly and easily integrate ChatGPT's language processing capabilities into your Go applications.

Features

  • Easy-to-use GoLang client for ChatGPT
  • Sends text to ChatGPT and receives a response
  • Support custom model and parameters
  • Supports GPT3.5 and GPT4 models

Installation

You can install ChatGPT-Go using Go modules:

go get github.com/ayush6624/go-chatgpt

Getting Started

Get your API key from the OpenAI Dashboard - https://platform.openai.com/account/api-keys and export this either as an environment variable, or put this your .bashrc or .zshrc

export OPENAI_KEY=sk...

  1. In your Go code, import the ChatGPT-Go package:

    import (
        "github.com/ayush6624/go-chatgpt"
    )
    
  2. Create a new ChatGPT client with your API key

    key := os.Getenv("OPENAI_KEY")
    
    client, err := chatgpt.NewClient(key)
    if err != nil {
    	log.Fatal(err)
    }
    
  3. Use the SimpleSend API to send text to ChatGPT and get a response.

     ctx := context.Background()
    
     res, err := c.SimpleSend(ctx, "Hello, how are you?")
     if err != nil {
     	// handle error
     }
    

    The SimpleSend method sends the specified text to ChatGPT and returns a response. If an error occurs, it returns an error message.

  4. To use a custom model/parameters, use the Send API.

     ctx := context.Background()
    
     res, err = c.Send(ctx, &chatgpt.ChatCompletionRequest{
     	Model: chatgpt.GPT35Turbo,
     	Messages: []chatgpt.ChatMessage{
     		{
     			Role: chatgpt.ChatGPTModelRoleSystem,
     			Content: "Hey, Explain GoLang to me in 2 sentences.",
     		},
     	},
     })
     if err != nil {
     	// handle error
     }
    

Contribute

If you want to contribute to this project, feel free to open a PR or an issue.

License

This package is licensed under MIT license. See LICENSE for details.


codecov Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatCompletionRequest

type ChatCompletionRequest struct {
	// (Required)
	// ID of the model to use.
	Model ChatGPTModel `json:"model"`

	// Required
	// The messages to generate chat completions for
	Messages []ChatMessage `json:"messages"`

	// (Optional - default: 1)
	// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
	// We generally recommend altering this or top_p but not both.
	Temperature float64 `json:"temperature,omitempty"`

	// (Optional - default: 1)
	// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
	// We generally recommend altering this or temperature but not both.
	Top_P float64 `json:"top_p,omitempty"`

	// (Optional - default: 1)
	// How many chat completion choices to generate for each input message.
	N int `json:"n,omitempty"`

	// (Optional - default: infinite)
	// The maximum number of tokens allowed for the generated answer. By default,
	// the number of tokens the model can return will be (4096 - prompt tokens).
	MaxTokens int `json:"max_tokens,omitempty"`

	// (Optional - default: 0)
	// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far,
	// increasing the model's likelihood to talk about new topics.
	PresencePenalty float64 `json:"presence_penalty,omitempty"`

	// (Optional - default: 0)
	// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far,
	// decreasing the model's likelihood to repeat the same line verbatim.
	FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`

	// (Optional)
	// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse
	User string `json:"user,omitempty"`
}

type ChatGPTModel

type ChatGPTModel string
const (
	GPT35Turbo ChatGPTModel = "gpt-3.5-turbo"

	// Deprecated: Use gpt-3.5-turbo-0613 instead, model will discontinue on 09/13/2023
	GPT35Turbo0301 ChatGPTModel = "gpt-3.5-turbo-0301"

	GPT35Turbo0613    ChatGPTModel = "gpt-3.5-turbo-0613"
	GPT35Turbo16k     ChatGPTModel = "gpt-3.5-turbo-16k"
	GPT35Turbo16k0613 ChatGPTModel = "gpt-3.5-turbo-16k-0613"
	GPT4              ChatGPTModel = "gpt-4"

	// Deprecated: Use gpt-4-0613 instead, model will discontinue on 09/13/2023
	GPT4_0314 ChatGPTModel = "gpt-4-0314"

	GPT4_0613 ChatGPTModel = "gpt-4-0613"
	GPT4_32k  ChatGPTModel = "gpt-4-32k"

	// Deprecated: Use gpt-4-32k-0613 instead, model will discontinue on 09/13/2023
	GPT4_32k_0314 ChatGPTModel = "gpt-4-32k-0314"

	GPT4_32k_0613 ChatGPTModel = "gpt-4-32k-0613"
)

type ChatGPTModelRole

type ChatGPTModelRole string
const (
	ChatGPTModelRoleUser      ChatGPTModelRole = "user"
	ChatGPTModelRoleSystem    ChatGPTModelRole = "system"
	ChatGPTModelRoleAssistant ChatGPTModelRole = "assistant"
)

type ChatMessage

type ChatMessage struct {
	Role    ChatGPTModelRole `json:"role"`
	Content string           `json:"content"`
}

type ChatResponse

type ChatResponse struct {
	ID        string               `json:"id"`
	Object    string               `json:"object"`
	CreatedAt int64                `json:"created_at"`
	Choices   []ChatResponseChoice `json:"choices"`
	Usage     ChatResponseUsage    `json:"usage"`
}

type ChatResponseChoice

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

type ChatResponseUsage

type ChatResponseUsage struct {
	Prompt_Tokens     int `json:"prompt_tokens"`
	Completion_Tokens int `json:"completion_tokens"`
	Total_Tokens      int `json:"total_tokens"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(apikey string) (*Client, error)

func NewClientWithConfig

func NewClientWithConfig(config *Config) (*Client, error)

func (*Client) Send

func (*Client) SimpleSend

func (c *Client) SimpleSend(ctx context.Context, message string) (*ChatResponse, error)

type Config

type Config struct {
	// Base URL for API requests.
	BaseURL string

	// API Key (Required)
	APIKey string

	// Organization ID (Optional)
	OrganizationID string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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