gogpt

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

README

gogpt

GoDoc Go Report Card


GoGPT is a Go library which let users use their ChatGPT accounts through Go. With GoGPT you can do everything that you can do with your ChatGPT account in the Web interface.

Installation

To install, you can use the following command:

go get -u https://github.com/Makepad-fr/gogpt

Usage

To use the ChatGPT from your Go application, you need to create a new instance by passing the file path for browser context, a boolean for headless, a boolean indicating the debug mode and your current timezone offset.

IMPORTANT: Please note that, the timezone offset is an integer in minutes. The value is negative for timezones ahead of UTC and positive for timezones behind UTC.

package main
import (
	"github.com/Makepad-fr/gogpt"
	"log"
)
func main() {
	var timeout float64 = 1000
	var debug bool = true
	gpt, err := gogpt.New(gogpt.Options{
		BrowserContextPath: "./gogpt.json",
		Headless:           false,
		TimeZoneOffset:     -120,
		Debug:              &debug,
		Timeout:            &timeout,
	})
	if err != nil {
		log.Fatal(err)
	}
}   
Login

To do any operation on your ChatGPT account, you need to login to your account first.

Interactive Login

TBD

Headless login

IMPORTANT: Headless login is only available if you're using email and password login.

You can simply call the Login method with your email and password on the gpt instance that you've previously created.

package main
import (
	"github.com/Makepad-fr/gogpt"
	"log"
)
func main() {
        var timeout float64 = 1000
	var debug bool = true
	gpt, err := gogpt.New(gogpt.Options{
		BrowserContextPath: "./gogpt.json",
		Headless:           false,
		TimeZoneOffset:     -120,
		Debug:              &debug,
		Timeout:            &timeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	err = gpt.Login("<YOUR_CHATGPT_EMAIL>", "<YOU_CHATGPT_PASSWORD>")
	if err != nil {
		log.Fatal(err)
	}
}
Sending a message (prompt)
By creating a new conversation

You can create a new conversation with an initial prompt using the CreateConversation method on the gpt instance that you previously created

package main
import (
	"github.com/Makepad-fr/gogpt"
	"log"
)
func main() {
	...
	conversation, err := gpt.CreateConversation("Hello", "text-davinci-002-render-sha", func(response gogpt.ConversationResponse) {
		log.Printf("Received response %+v", response)
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Created conversation %+v\n", conversation)
}
To an existing conversation

TBD

Generate title

You can generate conversation title using GenerateTitle. To achieve this you need to pass the UUID of the conversation and the uuid of the message used to generate the title.

Getting conversation history

You can get your conversation history using the History method on the gpt instance that you previously created

package main
... 
func main() {
	...
	conversations, err := gpt.History()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Number of conversations in history is %d", len(conversations))
}
Loading a conversation

You can load the conversation details using LoadConversation method and the ID of the conversation that you want to load

package main
...
func main() {
	...
	const conversationId = "<CONVERSATION_ID>"
	conversationDetails, err := gpt.LoadConversation(conversationId)
	if err != nil {
		log.Fatal(err)
	}
}
Get available models

You can get the list of the available models for account using the Models method.

package main
...
func main() {
	...
	models, err := gpt.Models()
	if err != nil {
		log.Fatal(err)
	}
}
Get text moderation

You can get text moderation using Moderation method by passing the UUID of the conversation, UUID of the message and the text of the message to get the moderation.

package main

import "log"

...
func main() {
	...
	moderation, err := gpt.Moderation("<CONVERSATION_UUID>", "<MESSAGE_UUID>", "<MESSAGE_TEXT>")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Moderation response %+v", moderation)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpCookie

func DumpCookie(browserContextPath string) error

DumpCookie lets session login to the ChatGPT with headless mode disabled and dumps the browser context to the given browserContextPath string passed in parameters

Types

type AccountPlan

type AccountPlan struct {
	IsPaidSubscriptionActive       bool   `json:"is_paid_subscription_active"`
	SubscriptionPlan               string `json:"subscription_plan"`
	AccountUserRole                string `json:"account_user_role"`
	WasPaidCustomer                bool   `json:"was_paid_customer"`
	HasCustomerObject              bool   `json:"has_customer_object"`
	SubscriptionExpiresAtTimestamp int64  `json:"subscription_expires_at_timestamp"`
}

type Conversation

type Conversation struct {
	Title             string                          `json:"title"`
	CreateTime        float64                         `json:"create_time"`
	UpdateTime        float64                         `json:"update_time"`
	Mapping           map[string]internal.MappingNode `json:"mapping"`
	ModerationResults []interface{}                   `json:"moderation_results"`
	CurrentNode       string                          `json:"current_node"`
}

type ConversationHistoryItem

type ConversationHistoryItem struct {
	ID         string `json:"id"`
	Title      string `json:"title"`
	CreateTime string `json:"create_time"`
	UpdateTime string `json:"update_time"`
	// contains filtered or unexported fields
}

type ConversationHistoryResponse

type ConversationHistoryResponse struct {
	Items                   []ConversationHistoryItem `json:"items"`
	Total                   int                       `json:"total"`
	Limit                   int                       `json:"limit"`
	Offset                  int                       `json:"offset"`
	HasMissingConversations bool                      `json:"has_missing_conversations"`
}

type ConversationResponse added in v0.0.3

type ConversationResponse struct {
	Message        internal.Message `json:"message"`
	ConversationID string           `json:"conversation_id"`
	Error          *string          `json:"error"`
}

type GenerateConversationTitleResponse added in v0.0.3

type GenerateConversationTitleResponse struct {
	Title string `json:"title"`
}

type GoGPT

type GoGPT interface {
	Login(username, password string) error
	Ask(question string, version Version)
	History() ([]ConversationHistoryItem, error)
	AccountInfo() UserAccountInfo
	LoadConversation(uuid string) (*Conversation, error)
	Close() error
	NewChat()
	Session() Session
	Models() ([]ModelInfo, error)
	Debug()
	CreateConversation(message, model string, onResponseCallback conversationResponseConsumer) (*Conversation, error)
	GenerateTitle(conversationId, messageId string) ([]byte, error)
	Moderation(conversationId, messageId, messageText string) (*TextModerationResponse, error)
}

func New

func New(options Options) (GoGPT, error)

New creates a new instance of GoGPT with given Options

type ModelInfo

type ModelInfo struct {
	Slug                  string                `json:"slug"`
	MaxTokens             int                   `json:"max_tokens"`
	Title                 string                `json:"title"`
	Description           string                `json:"description"`
	Tags                  []string              `json:"tags"`
	QualitativeProperties QualitativeProperties `json:"qualitative_properties"`
}

type ModelsResponse

type ModelsResponse struct {
	Models []ModelInfo `json:"models"`
}

type Options added in v0.0.3

type Options struct {
	BrowserContextPath string
	Headless           bool
	Debug              *bool
	TimeZoneOffset     int
	Timeout            *float64
}

type QualitativeProperties

type QualitativeProperties struct {
	Reasoning   []int `json:"reasoning"`
	Speed       []int `json:"speed"`
	Conciseness []int `json:"conciseness"`
}

type Session

type Session struct {
	User        User   `json:"user"`
	Expires     string `json:"expires"`
	AccessToken string `json:"accessToken"`
}

type TextModerationResponse added in v0.0.4

type TextModerationResponse struct {
	Blocked      bool   `json:"blocked"`
	Flagged      bool   `json:"flagged"`
	ModerationId string `json:"moderation_id"`
}

type User

type User struct {
	ID      string   `json:"id"`
	Name    string   `json:"name"`
	Email   string   `json:"email"`
	Image   string   `json:"image"`
	Picture string   `json:"picture"`
	Groups  []string `json:"groups"`
}

type UserAccountInfo

type UserAccountInfo struct {
	AccountPlan AccountPlan `json:"account_plan"`
	UserCountry string      `json:"user_country"`
	Features    []string    `json:"features"`
}

func UnmarshalUserAccountInfo

func UnmarshalUserAccountInfo(jsonData []byte) (UserAccountInfo, error)

type Version

type Version int64
const (
	V4       Version = 0
	V3_5     Version = 1
	V3Legacy Version = 2
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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