chatgpt

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 15 Imported by: 2

README

chatgpt-go

ChatGPT SDK 是用 Golang 编写的,可以用于快速集成 ChatGPT 的聊天机器人功能。

快速开始

  1. 安装 ChatGPT-Go SDK。
go get -u github.com/xyhelper/chatgpt-go
  1. 在独立的对话中进行聊天。
package main

import (
	"log"
	"time"

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

func main() {
	token := `random token`

	cli := chatgpt.NewClient(
		chatgpt.WithDebug(true),                            // 开启调试模式
		chatgpt.WithTimeout(120*time.Second),               // 设置超时时间为120秒
		chatgpt.WithAccessToken(token),                     // 设置token
		chatgpt.WithBaseURI("https://freechat.lidong.xin"), // 设置base uri
	)

	// chat in independent conversation
	message := "你好"
	text, err := cli.GetChatText(message)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}

	log.Printf("q: %s, a: %s\n", message, text.Content)
}

  1. 在连续的对话中进行聊天。
package main

import (
	"log"
	"time"

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

func main() {
	// new chatgpt client
	token := `random token`

	cli := chatgpt.NewClient(
		chatgpt.WithDebug(true),
		chatgpt.WithTimeout(60*time.Second),
		chatgpt.WithAccessToken(token),
		chatgpt.WithBaseURI("https://freechat.lidong.xin"),
	)

	// chat in continuous conversation

	// first message
	message := "对我说你好"
	text, err := cli.GetChatText(message)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}

	log.Printf("q: %s, a: %s\n", message, text.Content)

	// continue conversation with new message
	conversationID := text.ConversationID
	parentMessage := text.MessageID
	newMessage := "再说一次"

	newText, err := cli.GetChatText(newMessage, conversationID, parentMessage)
	if err != nil {
		log.Fatalf("get chat text failed: %v", err)
	}

	log.Printf("q: %s, a: %s\n", newMessage, newText.Content)
}

如果你想要在当前对话之外开始一个新的对话,你不需要重置客户端。只需在GetChatText方法中移除conversationIDparentMessage参数,即可获得一个新的文本回复,从而开始一个新的对话。

  1. 使用流(stream)获取聊天内容。
package main

import (
	"log"
	"time"

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

func main() {
	// new chatgpt client
	token := `random token`

	cli := chatgpt.NewClient(
		chatgpt.WithDebug(true),
		chatgpt.WithTimeout(120*time.Second),
		chatgpt.WithAccessToken(token),
		chatgpt.WithBaseURI("https://freechat.xyhelper.cn"),
	)

	message := "你好"
	stream, err := cli.GetChatStream(message)
	if err != nil {
		log.Fatalf("get chat stream failed: %v\n", err)
	}

	var answer string
	for text := range stream.Stream {
		log.Printf("stream text: %s\n", text.Content)

		answer = text.Content
	}

	if stream.Err != nil {
		log.Fatalf("stream closed with error: %v\n", stream.Err)
	}

	log.Printf("q: %s, a: %s\n", message, answer)
}

DEMO

cli/chatgpt-go 是一个使用 ChatGPT-Go SDK 的示例程序。 可以使用以下命令运行它:

cd cli/chatgpt-go
go run main.go

也可以使用 go install 命令安装它:

go install github.com/xyhelper/chatgpt-go/cli/chatgpt-go@latest

然后运行:

chatgpt-go

作品演示

https://xyhelper.cn

友情链接

  • CoolAdmin - 一个项目,用 COOL 就够了。AI 编码、物联网、函数云开发等更加智能和全面的功能,让应用开发紧跟时代的步伐,cool 绝不落后!!!

Documentation

Index

Constants

View Source
const (
	BASE_URI = "https://freechat.xyhelper.cn"
	// AUTH_SESSION_URI = "https://chat.openai.com/api/auth/session"
	// CONVERSATION_URI = "https://chat.openai.lidong.xin/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"
	EOF_TEXT   = "[DONE]"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatStream

type ChatStream struct {
	Stream chan *ChatText // chat text stream
	Err    error          // error message
}

ChatStream chat reply with sream

type ChatText

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

ChatText chat reply with text format

func (*ChatText) String

func (c *ChatText) String() string

ChatText format to string

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) GetChatStream

func (c *Client) GetChatStream(message string, args ...string) (*ChatStream, error)

GetChatStream will return text stream

func (*Client) GetChatText

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

GetChatText will return text message

func (*Client) SetModel added in v1.0.4

func (c *Client) SetModel(model string)

SetModel will set the chat model

func (*Client) SetProxy added in v1.0.4

func (c *Client) SetProxy(proxy string)

SetProxy will set the proxy

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 WithAccessToken

func WithAccessToken(accessToken string) Option

WithAccessToken is used to set accessToken

func WithBaseURI

func WithBaseURI(baseURI string) Option

WithBaseURI is used to set base uri

func WithCookie

func WithCookie(cookie string) Option

WithCookie is used to set request cookies in header

func WithCookies

func WithCookies(cookies []*http.Cookie) Option

WithCookies is used to set request cookies

func WithDebug

func WithDebug(debug bool) Option

WithDebug is used to output debug message

func WithModel

func WithModel(model string) Option

WithModel is used to set chat model

func WithProxy

func WithProxy(proxy string) Option

WithProxy is used to set request proxy

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout is used to set request timeout

func WithUserAgent

func WithUserAgent(userAgent string) Option

WithUserAgent is used to set request user-agent

type Options

type Options struct {
	// Debug is used to output debug message
	Debug bool
	// Timeout is used to end http request after timeout duration
	Timeout time.Duration
	// UserAgent is used for custom user-agent
	UserAgent string
	// Cookies is request cookies for each api
	Cookies []*http.Cookie
	// Cookie will set in request headers with string format
	Cookie string
	// Proxy is used to proxy request
	Proxy string
	// AccessToken is used to authorization
	AccessToken string
	// Model is the chat model
	Model string
}

Options can set custom options for ChatGPT request client

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

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