Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

每日一库推荐:chatgpt #64

Open
solywsh opened this issue Dec 10, 2022 · 0 comments
Open

每日一库推荐:chatgpt #64

solywsh opened this issue Dec 10, 2022 · 0 comments

Comments

@solywsh
Copy link

solywsh commented Dec 10, 2022

最近chatgpt很火,自己封装了一下

项目地址:https://github.com/solywsh/chatgpt

项目描述:

  • 封装了openai的三方接口库,由于openai官方的接口对多个请求不支持对话的上下文联系,目前市面上采用的方案是抓包网页版进行封装,需要获得cookie,并且只能维持单个会话。于是参照python的telegram机器人sudoskys/Openaibot封装了go的版本。
  • 由于直接请求的是openai的api接口,所以不需要cookie,只需要openai的api key即可,并且支持维持多个会话的上下文。
  • 支持上下文联系对话和不联系对话两种
  • 注册教程:https://www.v2ex.com/t/900126

示例代码:

package main

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

func main() {
	chat := New("openai_api_key", "", 10 * time.Minute)
	defer chat.Close()
        // DoneChan 一般用在多会话时接收结束状态信号时使用,应用示例可以看我自己的qq机器人项目https://github.com/solywsh/revue/blob/main/cq/chatgpt.go中44行的例子
	//go func() {
	//	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: 喵喵~!
}

集成到qq机器人之后的应用:

img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant