-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
149 lines (134 loc) · 3.45 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
package main
import (
"fmt"
"os"
"github.com/index-labs/evalgpt/agent/python"
"github.com/index-labs/evalgpt/scheduler"
"github.com/index-labs/evalgpt/utils"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "evalgpt",
HelpName: "evalgpt help",
Description: "description",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "openai-api-key",
Usage: "Openai Api Key, if you use open ai model gpt3 or gpt4, you must set this flag",
EnvVars: []string{"OPENAI_API_KEY"},
Required: true,
},
&cli.StringFlag{
Name: "openai-base-url",
Usage: "Openai Base URL",
EnvVars: []string{"OPENAI_BASE_URL"},
},
&cli.StringFlag{
Name: "model",
Usage: "LLM name",
Value: "gpt-4-0613",
EnvVars: []string{"MODEL"},
},
&cli.StringFlag{
Name: "python-interpreter",
Usage: "python interpreter path",
Value: "/usr/bin/python3",
EnvVars: []string{"PYTHON_INTERPRETER"},
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "print verbose log",
DefaultText: "false",
EnvVars: []string{"VERBOSE"},
},
&cli.BoolFlag{
Name: "run-as-server",
Usage: "run as server and provide restful api service",
Value: false,
DefaultText: "false",
EnvVars: []string{"RUN_AS_SERVER"},
},
&cli.StringFlag{
Name: "listen-addr",
Usage: "listen addr",
Value: "127.0.0.1:8080",
EnvVars: []string{"LISTEN_ADDR"},
},
&cli.StringFlag{
Name: "query",
Aliases: []string{"q"},
Usage: "what you want to ask",
},
&cli.StringSliceFlag{
Name: "file",
Usage: "the path to the file to be parsed and processed, eg. --file /tmp/a.txt --file /tmp/b.txt",
},
},
Action: run,
}
err := app.Run(os.Args)
if err != nil {
log.Errorf("error: %v", err)
}
}
func run(ctx *cli.Context) error {
openaiApiKey := ctx.String("openai-api-key")
openaiBaseURL := ctx.String("openai-base-url")
model := ctx.String("model")
pythonInterpreter := ctx.String("python-interpreter")
query := ctx.String("query")
verbose := ctx.Bool("verbose")
fileList := ctx.StringSlice("file")
runAsServer := ctx.Bool("run-as-server")
listenAddr := ctx.String("listen-addr")
if verbose {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.ErrorLevel)
}
log.SetFormatter(&log.TextFormatter{
ForceQuote: true,
})
pipeData, err := utils.ReadStdinPipeData()
if err != nil {
log.Errorf("read stdin failed: %v", err)
return err
}
if len(pipeData) > 0 {
query += fmt.Sprintf(`\n"""%s"""`, pipeData)
}
log.Infof("model: %s, pythonInterpreter: %s", model, pythonInterpreter)
pyAgent := python.NewPythonAgent(python.Config{
PythonInterpreter: pythonInterpreter,
OpenaiApiKey: openaiApiKey,
OpenaiBaseURL: openaiBaseURL,
Model: model,
})
sched := scheduler.NewScheduler(scheduler.Config{
PythonAgent: pyAgent,
})
if runAsServer {
log.Infof("run as server, listen address: %s", listenAddr)
return sched.Run(listenAddr)
}
if len(query) == 0 {
return fmt.Errorf("query is empty")
}
result, outputFiles, err := sched.HandleQuery(query, fileList)
if err != nil {
return err
}
log.Infof("===== result =====")
if len(result) > 0 {
fmt.Println(result)
}
if len(outputFiles) > 0 {
for _, filename := range outputFiles {
fmt.Println("created file:", filename)
}
}
return nil
}