-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathapi.go
94 lines (81 loc) · 2.34 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package chat_gpt_ppt
import (
"fmt"
"log"
"strings"
"github.com/abiosoft/ishell/v2"
)
type ApiConfig struct {
Token string `json:"token"`
Topics []string `json:"topics"`
OutputFile string `json:"outputFile"`
RendererType RendererType `json:"rendererType"`
RendererBin string `json:"rendererBin"`
ClientType ClientType `json:"clientType"`
Interactive bool `json:"interactive"`
}
var logger = log.Default()
func GenAndRenderString(shellContext *ishell.Context, config ApiConfig) (string, error) {
// init client
c := GetClient(config.ClientType)
if c == nil {
return "", fmt.Errorf("no client named: %v", config.ClientType)
}
// https://github.com/williamfzc/chat-gpt-ppt/issues/12
safeToken := strings.TrimSpace(config.Token)
c.SetToken(safeToken)
// init renderer
renderer := GetRenderer(config.RendererType)
if renderer == nil {
return "", fmt.Errorf("no renderer named: %v", config.RendererType)
}
if config.RendererBin != "" {
shellContext.Printf("set renderer bin: %v\n", config.RendererBin)
renderer.SetBinPath(config.RendererBin)
}
// prepare
shellContext.Println("start preparing ...")
err := c.Prepare(config.Topics)
if err != nil {
return "", err
}
// fill
shellContext.Println("start generating ...")
topics := make([]*Topic, 0)
for _, eachTopic := range config.Topics {
finalTopic, err := getFinalTopic(shellContext, c, eachTopic, config.Interactive)
if err != nil {
return "", err
}
topics = append(topics, finalTopic)
}
// renderer
shellContext.Println("start rendering ...")
shellContext.Stop()
for _, eachTopic := range topics {
renderer.AddTopic(eachTopic)
}
str, err := renderer.RenderString()
if err != nil {
return "", err
}
return str, nil
}
func getFinalTopic(shellContext *ishell.Context, c Client, eachTopic string, needConfirm bool) (*Topic, error) {
resp, err := c.FillTopic(eachTopic)
if err != nil {
return nil, err
}
if !needConfirm {
return resp, nil
}
shellContext.Println("Here is your response, type any key to continue, type 'n' to edit", resp.ToMarkdown())
ok := shellContext.ReadLine()
if ok != "n" {
return resp, nil
} else {
shellContext.Println("You can enter a new topic to regenerate this page.")
newTopic := shellContext.ReadLine()
return getFinalTopic(shellContext, c, newTopic, needConfirm)
}
}