chatgpt package - github.com/chatgp/chatgpt-go - Go Packages

chatgpt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2022 License: MIT Imports: 9 Imported by: 1

README

chatgpt-go

chatgpt sdk writen with golang.

Rrecondition

you should login ChatGPT website, and get your session-token

Quick Start

  1. install chatgpt-go sdk
go get -u github.com/chatgp/chatgpt-go
  1. chat in independent conversation
package main 

import (
    "fmt"
	"time"

	chatgpt "github.com/chatgp/chatgpt-go"
)

func main() {
    // new chatgpt client
    token := `YOUR-SESSION-TOKEN`
	cli = chatgpt.NewClient(
		chatgpt.WithToken(token),
		chatgpt.WithTimeout(30*time.Second),
	)

    // chat in independent conversation
    message := "hello"
	text, err := cli.GetChatText(message)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}
	fmt.Printf("q: %s, a: %s\n", message, text.Content)
}
  1. chat in continuous conversation
package main 

import (
    "fmt"
	"time"

	chatgpt "github.com/chatgp/chatgpt-go"
)

func main() {
    // new chatgpt client
    token := `YOUR-SESSION-TOKEN`
	cli = chatgpt.NewClient(
		chatgpt.WithToken(token),
		chatgpt.WithTimeout(30*time.Second),
	)

    // chat in continuous conversation

	// first message
    message := "what's golang"
	text, err := cli.GetChatText(message)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}
	fmt.Printf("q: %s, a: %s\n", message, text.Content)

	// continue conversation with new message
	conversationID := text.ConversationID
	parentMessage := text.MessageID
	newMessage := "use it to write hello world"

	newText, err := cli.GetChatText(newMessage, conversationID, parentMessage)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}
	fmt.Printf("q: %s, a: %s\n", newMessage, newText.Content)
}

if you want to start a new conversation out of current conversation, you don't need to reset the client. just remove the conversationIDparentMessage arguments in GetChatText method and use it to get a new text reply.

Communication

Documentation

Index

Constants

View Source
const (
	BASE_URI         = "https://chat.openai.com"
	AUTH_SESSION_URI = "https://chat.openai.com/api/auth/session"
	CONVERSATION_URI = "https://chat.openai.com/backend-api/conversation"
	USER_AGENT       = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatStream

type ChatStream struct {
}

ChatStream chat reply with sream

type ChatText

type ChatText struct {
	Content        string // text content
	ConversationID string // conversation context id
	MessageID      string // current message id, can used as next chat's parent_message_id
}

ChatText chat reply with text format

type Client

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

Client is a ChatGPT request client

func NewClient

func NewClient(options ...Option) *Client

NewClient will return a ChatGPT request client

func (*Client) GetChatText

func (c *Client) GetChatText(message string, args ...string) (*ChatText, error)

GetChatText will return reply text

type MixMap

type MixMap = map[string]interface{}

MixMap is a type alias for map[string]interface{}

type Option

type Option func(*Client)

Option is used to set custom option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout is used to set request timeout

func WithToken

func WithToken(token string) Option

WithToken is used to set token option

type Options

type Options struct {
	// Timeout is used to end http request after timeout duration
	Timeout time.Duration
	// Token is session token for each api
	Token string
}

Options can set custom options for ChatGPT request client

Jump to

Keyboard shortcuts

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