add command line arg --config, enable reply default message when the OpenAI API is not available

This commit is contained in:
RockYang 2023-03-23 11:47:43 +08:00
parent 20bdf12180
commit c700895d9c
3 changed files with 49 additions and 16 deletions

31
main.go
View File

@ -2,6 +2,8 @@ package main
import ( import (
"embed" "embed"
"flag"
"fmt"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
logger2 "openai/logger" logger2 "openai/logger"
"openai/server" "openai/server"
@ -13,6 +15,7 @@ var logger = logger2.GetLogger()
//go:embed dist //go:embed dist
var webRoot embed.FS var webRoot embed.FS
var configFile string
func main() { func main() {
defer func() { defer func() {
@ -37,10 +40,36 @@ func main() {
return return
} }
if configFile == "" {
configFile = filepath.Join(configDir, "config.toml")
}
// start server // start server
s, err := server.NewServer(filepath.Join(configDir, "/config.toml")) s, err := server.NewServer(configFile)
if err != nil { if err != nil {
panic(err) panic(err)
} }
s.Run(webRoot, "dist") s.Run(webRoot, "dist")
} }
func init() {
flag.StringVar(&configFile, "config", "", "Config file path (default: ~/.config/chat-gpt/config.toml)")
flag.Usage = usage
flag.Parse()
}
func usage() {
fmt.Printf(`WeChat-GPT, Version: 1.0.0
USAGE:
%s [command options]
OPTIONS:
`, os.Args[0])
flagSet := flag.CommandLine
order := []string{"config"}
for _, name := range order {
f := flagSet.Lookup(name)
fmt.Printf(" --%s => %s\n", f.Name, f.Usage)
}
}

View File

@ -95,7 +95,12 @@ func (s *Server) sendMessage(userId string, text string, ws Client) error {
} }
retryCount-- retryCount--
} }
// 如果三次请求都失败的话,则返回对应的错误信息
if err != nil { if err != nil {
replyMessage(types.WsMessage{Type: types.WsStart}, ws)
replyMessage(types.WsMessage{Type: types.WsMiddle, Content: "抱歉AI 助手开小差了,我马上找人去盘它。"}, ws)
replyMessage(types.WsMessage{Type: types.WsEnd}, ws)
return err return err
} }

View File

@ -113,20 +113,20 @@ export default defineComponent({
this.checkSession(); this.checkSession();
for (let i = 0; i < 10; i++) { // for (let i = 0; i < 10; i++) {
this.chatData.push({ // this.chatData.push({
type: "prompt", // type: "prompt",
id: randString(32), // id: randString(32),
icon: 'images/user-icon.png', // icon: 'images/user-icon.png',
content: "孙悟空为什么可以把金棍棒放进耳朵?", // content: "",
}); // });
this.chatData.push({ // this.chatData.push({
type: "reply", // type: "reply",
id: randString(32), // id: randString(32),
icon: 'images/gpt-icon.png', // icon: 'images/gpt-icon.png',
content: "孙悟空是中国神话中的人物,传说中他可以把金箍棒放进耳朵里,这是一种超自然能力,无法用现代科学解释。这种能力可能是象征孙悟空超人力量的古代文化传说。", // content: "",
}); // });
} // }
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
this.chatBoxHeight = window.innerHeight - this.toolBoxHeight; this.chatBoxHeight = window.innerHeight - this.toolBoxHeight;
@ -195,7 +195,6 @@ export default defineComponent({
}); });
} else if (data.type === 'end') { } else if (data.type === 'end') {
this.sending = false; this.sending = false;
this.chatData[this.chatData.length - 1]["cursor"] = false;
} else { } else {
let content = data.content; let content = data.content;
// //