https://github.com/leizhenpeng/gpt-chatbot-cli
A CLI ChatBot Power By Gpt3 基于gpt3引擎实现CLI版本的chatgpt~
https://github.com/leizhenpeng/gpt-chatbot-cli
cobra cobra-cli golang gpt gpt-3
Last synced: about 1 month ago
JSON representation
A CLI ChatBot Power By Gpt3 基于gpt3引擎实现CLI版本的chatgpt~
- Host: GitHub
- URL: https://github.com/leizhenpeng/gpt-chatbot-cli
- Owner: Leizhenpeng
- License: other
- Created: 2023-02-02T13:48:42.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-06T06:39:23.000Z (over 2 years ago)
- Last Synced: 2025-05-01T17:13:21.289Z (about 1 month ago)
- Topics: cobra, cobra-cli, golang, gpt, gpt-3
- Language: Go
- Homepage: https://www.bilibili.com/video/BV1ae4y1N7W2
- Size: 1.23 MB
- Stars: 6
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
![]()
CLI ChatBot Power By Gpt3(speed)
## 源代码
🏀🏀
[CLI ChatBot Power By Gin](https://github.com/Leizhenpeng/gpt3-chatBot-cli)
🏀🏀## bilibili 在线吹水
⚽️⚽️
[基于gpt3引擎实现CLI版本的chatgpt~](https://www.bilibili.com/video/BV1ae4y1N7W2)
⚽️⚽️## Features
- 🐤 Gpt3 ChatBot
- 🍉 Store Keyring
- 🥑 Cobra CLI## development
```bash
make build
``````bash
go-chat key set
go-chat run
```## Snapshot
- 记录上下文,实现连续对话
![]()
``` go
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接口
``` go
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](https://github.com/zalando/go-keyring)
- [Cobra CLI library](https://github.com/spf13/cobra-cli/blob/main/README.md)
- [golang常见库cobra](https://segmentfault.com/a/1190000023382214)
- [chatGpt3](https://github.com/PullRequestInc/go-gpt3)