基于gpt3引擎实现CLI版本的chatgpt~(数量取胜的go语言学习法)

376 阅读1分钟

CLI ChatBot Power By Gpt3

CLI ChatBot Power By Gpt3

CLI ChatBot Power By Gpt3(speed)

源代码

🏀🏀CLI ChatBot Power By Gin🏀🏀

bilibili 在线吹水

⚽️⚽️基于gpt3引擎实现CLI版本的chatgpt~⚽️⚽️

Features

  • 🐤 Gpt3 ChatBot
  • 🍉 Store Keyring
  • 🥑 Cobra CLI

development

make build
go-chat key set <your-key>
go-chat run

Snapshot

  • 记录上下文,实现连续对话

CLI ChatBot Power By Gpt3

func NewCacheHistory() CacheHistoryInterface {
   return &CacheHistory{
      db: cache.New(time.Second*60, time.Minute*10),
   }
}

func (c CacheHistory) SetQACache(q string, a string) {
   saveValue := q + "\n" + a
   c.set(userSessionContextKey, saveValue)
}

func (c CacheHistory) GetQACache() (string, bool) {
   res, ok := c.get(userSessionContextKey)
   if !ok {
      return "", false
   }
   return res.(string), true
}

func (c CacheHistory) ClearQACache() {
   c.clear()
}
  • 封装调用gpt3接口
func GetAnswer(question string) {
  
   fmt.Print("Answer: ")
   i := 0
   ctx := context.Background()
 
   if err := client.CompletionStreamWithEngine(ctx, engine, gpt3.CompletionRequest{
      Prompt: []string{
         question,
      },
      MaxTokens:   gpt3.IntPtr(maxTokens),
      Temperature: gpt3.Float32Ptr(temperature),
   }, func(resp *gpt3.CompletionResponse) {
      if i > 1 {
         fmt.Print(resp.Choices[0].Text)
      }
      i++
   }); err != nil {
      log.Fatalln(err)
   }
   fmt.Println()
}
  • cobra项目结构
├── LICENSE
├── Makefile
├── cmd
│   ├── key.go
│   ├── root.go
│   └── run.go
├── go-chat
├── go.mod
├── go.sum
├── main.go
├── readme.md
└── services
    ├── browse.go
    ├── cli.go
    ├── gpt.go
    └── key.go
    

More Info