CLI ChatBot Power By Gpt3(speed)
源代码
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。