81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package llm
|
|
|
|
import (
|
|
"apigo.cc/ai/llm/llm"
|
|
"apigo.cc/apigo/gojs"
|
|
"apigo.cc/apigo/gojs/dop251/goja"
|
|
"bytes"
|
|
_ "embed"
|
|
"github.com/ssgo/config"
|
|
"github.com/ssgo/u"
|
|
"text/template"
|
|
)
|
|
|
|
//go:embed llm.ts
|
|
var llmTS string
|
|
|
|
//go:embed llm.md
|
|
var llmMD string
|
|
|
|
var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ"))
|
|
var keysIsSet = false
|
|
|
|
func SetSSKey(key, iv []byte) {
|
|
if !keysIsSet {
|
|
confAes = u.NewAes(key, iv)
|
|
keysIsSet = true
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
list := map[string]*map[string]*struct {
|
|
Endpoint string
|
|
ApiKey string
|
|
ChatConfig llm.ChatConfig
|
|
GCConfig llm.GCConfig
|
|
Debug bool
|
|
}{}
|
|
|
|
jsObj := gojs.Map{}
|
|
llmList := make([]string, 0)
|
|
_ = config.LoadConfig("llm", &list)
|
|
for llmName, llmConfigs := range list {
|
|
for confName, llmConf := range *llmConfigs {
|
|
llmConf.ApiKey = confAes.DecryptUrlBase64ToString(llmConf.ApiKey)
|
|
if confName == "default" {
|
|
confName = llmName
|
|
}
|
|
llmList = append(llmList, confName)
|
|
llmObj := llm.Create(confName, llmName, llm.Config{
|
|
Endpoint: llmConf.Endpoint,
|
|
ApiKey: llmConf.ApiKey,
|
|
ChatConfig: llmConf.ChatConfig,
|
|
GCConfig: llmConf.GCConfig,
|
|
Debug: llmConf.Debug,
|
|
})
|
|
jsObj[confName] = MakeLLM(llmObj)
|
|
}
|
|
}
|
|
jsObj["similarity"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
return vm.ToValue(llm.Similarity(args.Bytes(0), args.Bytes(1)))
|
|
}
|
|
|
|
var tpl *template.Template
|
|
var err error
|
|
llmTSCode := ""
|
|
if tpl, err = template.New("").Parse(llmTS); err == nil {
|
|
buf := bytes.NewBuffer(make([]byte, 0))
|
|
if err = tpl.Execute(buf, llmList); err == nil {
|
|
llmTSCode = string(buf.Bytes())
|
|
}
|
|
}
|
|
|
|
gojs.Register("llm", gojs.Module{
|
|
Object: jsObj,
|
|
TsCode: llmTSCode,
|
|
Desc: "llm plugin for gojs(http://apigo.cc/apigo/gojs)",
|
|
Example: llmMD,
|
|
})
|
|
}
|