llm_old/llm-cli/main.go
2024-10-12 14:37:11 +08:00

90 lines
1.8 KiB
Go

package main
import (
"fmt"
"os"
_ "apigo.cc/ai/llm"
_ "apigo.cc/ai/llm/openai"
_ "apigo.cc/ai/llm/zhipu"
"apigo.cc/gojs"
_ "apigo.cc/gojs/console"
_ "apigo.cc/gojs/db"
_ "apigo.cc/gojs/file"
_ "apigo.cc/gojs/http"
_ "apigo.cc/gojs/log"
_ "apigo.cc/gojs/util"
"github.com/ssgo/u"
)
func init() {
gojs.Alias("llm", "apigo.cc/ai/llm")
gojs.Alias("console", "apigo.cc/gojs/console")
gojs.Alias("db", "apigo.cc/gojs/db")
gojs.Alias("file", "apigo.cc/gojs/file")
gojs.Alias("http", "apigo.cc/gojs/http")
gojs.Alias("log", "apigo.cc/gojs/log")
gojs.Alias("util", "apigo.cc/gojs/util")
}
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
}