darjun / go-daily-lib

Go 每日一库
5.03k stars 599 forks source link

每日一库推荐:chatgpt #64

Open solywsh opened 1 year ago

solywsh commented 1 year ago

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

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

项目描述:

示例代码:

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