chatgpt

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 9 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

chatgpt

chartgpt client for golang

Usege

Download the package first:

go get github.com/solywsh/chatgpt

Conversation without context:

package main

import (
	"fmt"
	"github.com/solywsh/chatgpt"
	"time"
)

func main() {
	// The timeout is used to control the situation that the session is in a long and multi session situation.
	// If it is set to 0, there will be no timeout. Note that a single request still has a timeout setting of 30s.
	chat := chatgpt.New("openai_key", "user_id(not required)", 30*time.Second)
	defer chat.Close()
	//
	//select {
	//case <-chat.GetDoneChan():
	//	fmt.Println("time out/finish")
	//}
	question := "你认为2022年世界杯的冠军是谁?"
	fmt.Printf("Q: %s\n", question)
	answer, err := chat.Chat(question)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("A: %s\n", answer)

	//Q: 你认为2022年世界杯的冠军是谁?
	//A: 这个问题很难回答,因为2022年世界杯还没有开始,所以没有人知道冠军是谁。
}

Conversation with context:

package main

import (
	"fmt"
	"github.com/solywsh/chatgpt"
	"time"
)

func main() {
	chat := chatgpt.New("openai_key", "user_id(not required)", 10*time.Second)
	defer chat.Close()
	//select {
	//case <-chat.GetDoneChan():
	//	fmt.Println("time out")
	//}
	question := "现在你是一只猫,接下来你只能用\"喵喵喵\"回答."
	fmt.Printf("Q: %s\n", question)
	answer, err := chat.ChatWithContext(question)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("A: %s\n", answer)
	question = "你是一只猫吗?"
	fmt.Printf("Q: %s\n", question)
	answer, err = chat.ChatWithContext(question)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("A: %s\n", answer)

	// Q: 现在你是一只猫,接下来你只能用"喵喵喵"回答.
	// A: 喵喵喵!
	// Q: 你是一只猫吗?
	// A: 喵喵~!
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAiRole    = "AI"
	DefaultHumanRole = "Human"

	DefaultCharacter  = []string{"helpful", "creative", "clever", "friendly", "lovely", "talkative"}
	DefaultBackground = "The following is a conversation with AI assistant. The assistant is %s"
	DefaultPreset     = "\n%s: 你好,让我们开始愉快的谈话!\n%s: 我是 AI assistant ,请问你有什么问题?"
)
View Source
var OverMaxQuestionLength = errors.New("maximum question length exceeded")

OverMaxQuestionLength 超过最大问题长度

View Source
var OverMaxSequenceTimes = errors.New("maximum conversation times exceeded")

OverMaxSequenceTimes 超过最大对话时间

View Source
var OverMaxTextLength = errors.New("maximum text length exceeded")

OverMaxTextLength 超过最大文本长度

Functions

This section is empty.

Types

type ChatContext added in v0.0.9

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

func NewContext added in v0.0.9

func NewContext(options ...ChatContextOption) *ChatContext

func (*ChatContext) GetMaxSeqTimes added in v0.0.12

func (c *ChatContext) GetMaxSeqTimes() int

func (*ChatContext) LoadConversation added in v0.0.13

func (c *ChatContext) LoadConversation(path string) error

LoadConversation 加载对话

func (*ChatContext) PollConversation added in v0.0.13

func (c *ChatContext) PollConversation()

PollConversation 移除最旧的一则对话

func (*ChatContext) ResetConversation added in v0.0.13

func (c *ChatContext) ResetConversation()

ResetConversation 重置对话

func (*ChatContext) SaveConversation added in v0.0.13

func (c *ChatContext) SaveConversation(path string) error

SaveConversation 保存对话

func (*ChatContext) SetAiRole added in v0.0.9

func (c *ChatContext) SetAiRole(role string)

func (*ChatContext) SetBackground added in v0.0.12

func (c *ChatContext) SetBackground(background string)

func (*ChatContext) SetHumanRole added in v0.0.9

func (c *ChatContext) SetHumanRole(role string)

func (*ChatContext) SetMaxSeqTimes added in v0.0.9

func (c *ChatContext) SetMaxSeqTimes(times int)

func (*ChatContext) SetPreset added in v0.0.12

func (c *ChatContext) SetPreset(preset string)

type ChatContextOption added in v0.0.13

type ChatContextOption func(*ChatContext)

func WithMaintainSeqTimes added in v0.0.13

func WithMaintainSeqTimes(maintain bool) ChatContextOption

func WithMaxSeqTimes added in v0.0.13

func WithMaxSeqTimes(times int) ChatContextOption

func WithOldConversation added in v0.0.13

func WithOldConversation(path string) ChatContextOption

WithOldConversation 从文件中加载对话

type ChatGPT

type ChatGPT struct {
	ChatContext *ChatContext
	// contains filtered or unexported fields
}

func New

func New(ApiKey, UserId string, timeOut time.Duration) *ChatGPT

func (*ChatGPT) Chat

func (c *ChatGPT) Chat(question string) (answer string, err error)

func (*ChatGPT) ChatWithContext added in v0.0.9

func (c *ChatGPT) ChatWithContext(question string) (answer string, err error)

func (*ChatGPT) Close

func (c *ChatGPT) Close()

func (*ChatGPT) GetDoneChan added in v0.0.9

func (c *ChatGPT) GetDoneChan() chan struct{}

func (*ChatGPT) SetMaxQuestionLen added in v0.0.9

func (c *ChatGPT) SetMaxQuestionLen(maxQuestionLen int) int

Jump to

Keyboard shortcuts

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