-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
192 lines (175 loc) · 4.25 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"github.com/cupogo/andvari/models/oid"
"github.com/cupogo/andvari/utils/zlog"
"github.com/liut/morrigan/htdocs"
"github.com/liut/morrigan/pkg/services/stores"
"github.com/liut/morrigan/pkg/settings"
"github.com/liut/morrigan/pkg/web"
)
func usage(cc *cli.Context) error {
return settings.Usage()
}
func initdb(cc *cli.Context) error {
return stores.InitDB()
}
func importDocs(cc *cli.Context) error {
input := cc.Args().First()
file, err := os.Open(input)
if err != nil {
logger().Warnw("open fail", "input", input, "err", err)
return err
}
defer file.Close()
err = stores.Sgt().Qa().ImportFromCSV(context.Background(), file)
if err != nil {
logger().Warnw("import fail", "input", input, "err", err)
return err
}
return nil
}
func fillQAs(cc *cli.Context) error {
ctx := context.Background()
spec := &stores.DocumentSpec{}
if cc.Args().Len() > 0 {
spec.IDsStr = oid.OIDsStr(cc.String("ids"))
}
spec.Limit = 90
spec.Sort = "id"
return stores.Sgt().Qa().FillQAs(ctx, spec)
}
func exportQAs(cc *cli.Context) error {
output := cc.Args().First() // csv
file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logger().Warnw("open fail", "output", output, "err", err)
return err
}
defer file.Close()
ctx := context.Background()
spec := &stores.DocumentSpec{}
spec.Limit = 90
spec.Sort = "id"
ea := stores.ExportArg{
Spec: spec,
Out: file,
Format: cc.String("format"),
}
return stores.Sgt().Qa().ExportQAs(ctx, ea)
}
func embeddingPrompt(cc *cli.Context) error {
ctx := context.Background()
spec := &stores.DocumentSpec{}
spec.Limit = 90
spec.Sort = "id"
return stores.Sgt().Qa().EmbeddingPrompt(ctx, spec)
}
func logger() zlog.Logger {
return zlog.Get()
}
func main() {
var zlogger *zap.Logger
if settings.InDevelop() {
zlogger, _ = zap.NewDevelopment()
} else {
zlogger, _ = zap.NewProduction()
}
sugar := zlogger.Sugar()
zlog.Set(sugar)
app := &cli.App{
Usage: "A Backend for OpenAI/ChatGPT",
UseShortOptionHandling: true,
Commands: []*cli.Command{
{
Name: "usage",
Aliases: []string{"env"},
Usage: "show usage",
Action: usage,
},
{
Name: "initdb",
Usage: "init database schema",
Action: initdb,
},
{
Name: "import",
Usage: "import documents from a csv",
Action: importDocs,
},
{
Name: "fill-qa",
Usage: "fill QA in documents",
Aliases: []string{"fillQAs"},
Action: fillQAs,
Flags: []cli.Flag{
&cli.StringFlag{Name: "ids", Usage: "special document `IDs` (space with comma)"},
},
},
{
Name: "export-qa",
Usage: "export QA from documents",
Aliases: []string{"exportQAs"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "format", Aliases: []string{"t"}, Value: "csv", Usage: "csv|jsonl"},
},
Action: exportQAs,
},
{
Name: "embedding",
Usage: "read prompt documents and embedding",
Aliases: []string{"embedding-pormpt"},
Action: embeddingPrompt,
},
{
Name: "web",
Aliases: []string{"run"},
Usage: "run a web server",
Flags: []cli.Flag{
&cli.StringFlag{Name: "listen", Aliases: []string{"l"}, Value: settings.Current.HTTPListen, Usage: "http listen address"},
},
Action: webRun,
},
{
Name: "version", Aliases: []string{"ver"},
Usage: "show build version",
Action: func(ctx *cli.Context) error {
sugar.Infow("", "version", settings.Version(), "runtime", runtime.Version())
return nil
},
},
},
}
// if len(os.Args) < 2 {
// webRun()
// return
// }
if err := app.Run(os.Args); err != nil {
logger().Fatalw("app run fail", "err", err)
}
}
func webRun(cc *cli.Context) error {
srv := web.New(web.Config{
Addr: cc.String("listen"),
Debug: settings.InDevelop(),
DocHandler: http.FileServer(http.FS(htdocs.FS())),
})
ctx := context.Background()
go func() {
quit := make(chan os.Signal, 2)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger().Info("shuting down server...")
if err := srv.Stop(ctx); err != nil {
logger().Infow("server shutdown:", "err", err)
}
}()
return srv.Serve(ctx)
}