博客 / 詳情

返回

基於gpt3引擎實現CLI版本的chatgpt(數量取勝的go語言學習法)

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

  • 記錄上下文,實現連續對話
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

  • Go Keyring library
  • Cobra CLI library
  • golang常見庫cobra
  • chatGpt3
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.