llm_old/llm-cli/main.go

72 lines
1.3 KiB
Go
Raw Normal View History

2024-10-02 14:09:54 +08:00
package main
import (
_ "apigo.cc/ai/llm"
"apigo.cc/apigo/gojs"
_ "apigo.cc/apigo/gojs/modules"
"fmt"
"github.com/ssgo/u"
"os"
)
func main() {
args := os.Args[1:]
if len(args) > 0 && (args[0] == "-e" || args[0] == "export") {
imports := gojs.ExportForDev()
fmt.Println(`exported to node_modules
test.js:
`)
fmt.Println(u.Cyan(imports + `
function main(...args) {
let r = llm.zhipu.fastAsk(args.length>0?args[0]:'你好', console.print)
console.println()
return r
}
run:
llm-cli test.js 你是谁
`))
return
}
isWatch := false
if len(args) > 0 && args[0] == "-w" {
isWatch = true
args = args[1:]
}
jsFile := ""
if len(args) > 0 {
jsFile = args[0]
args = args[1:]
}
if jsFile != "" && u.FileExists(jsFile) {
if isWatch {
if w, err := gojs.WatchRun(jsFile, nil, nil, u.ToInterfaceArray(args)...); err == nil {
w.WaitForKill()
} else {
fmt.Println(u.BRed(err.Error()))
}
} else {
if _, err := gojs.RunFile(jsFile, u.ToInterfaceArray(args)...); err != nil {
fmt.Println(u.BRed(err.Error()))
}
}
return
}
fmt.Println(`Usage:
llm-cli -h | help show usage
llm-cli -e | export export ai.ts file for develop
llm-cli test.js run test.js, if not specified, run ai.js
llm-cli -w | watch test.js run test.js, if .js files changed will be reloaded
`)
return
}