chatgpt

package module
v0.0.0-...-f4aebdc Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: MIT Imports: 6 Imported by: 0

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: Cześć!\n%s: Jestem asystentem AI, co mogę dla Ciebie zrobić?"
)
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

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

func NewContext

func NewContext() *ChatContext

func (*ChatContext) GetMaxSeqTimes

func (c *ChatContext) GetMaxSeqTimes() int

func (*ChatContext) SetAiRole

func (c *ChatContext) SetAiRole(role string)

func (*ChatContext) SetBackground

func (c *ChatContext) SetBackground(background string)

func (*ChatContext) SetHumanRole

func (c *ChatContext) SetHumanRole(role string)

func (*ChatContext) SetMaxSeqTimes

func (c *ChatContext) SetMaxSeqTimes(times int)

func (*ChatContext) SetPreset

func (c *ChatContext) SetPreset(preset string)

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

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

func (*ChatGPT) Close

func (c *ChatGPT) Close()

func (*ChatGPT) GetDoneChan

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

func (*ChatGPT) SetMaxQuestionLen

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