update to apigo.cc/gojs

This commit is contained in:
Star 2024-10-12 14:37:11 +08:00
parent 70ded2fb95
commit 0f8d3a157f
6 changed files with 111 additions and 65 deletions

View File

@ -22,8 +22,8 @@ llm-cli -w | watch test.js run test.js, if .js files changed will be reloaded
#### test.js #### test.js
```javascript ```javascript
import {zhipu} from 'llm' import {zhipu} from 'apigo.cc/ai/llm'
import console from 'console' import console from 'apigo.cc/gojs/console'
function main(...args) { function main(...args) {
let r = zhipu.fastAsk((args.length>0?args[0]:'你好', r => { let r = zhipu.fastAsk((args.length>0?args[0]:'你好', r => {
@ -69,12 +69,12 @@ sskey -e 'your apiKey'
copy url base64 format encrypted apiKey into llm.yml or env.yml copy url base64 format encrypted apiKey into llm.yml or env.yml
## 将 [llm](https://apigo.cc/ai/llm) 和 [低代码](https://apigo.cc/apigo/gojs) 集成到应用 ## 将 [llm](https://apigo.cc/ai/llm) 和 [低代码](https://apigo.cc/gojs) 集成到应用
### Install ### Install
```shell ```shell
go get -u apigo.cc/apigo/gojs go get -u apigo.cc/gojs
go get -u apigo.cc/ai/llm go get -u apigo.cc/ai/llm
``` ```
@ -85,8 +85,8 @@ package main
import ( import (
_ "apigo.cc/ai/llm" _ "apigo.cc/ai/llm"
"apigo.cc/apigo/gojs" "apigo.cc/gojs"
_ "apigo.cc/apigo/gojs/modules" _ "apigo.cc/gojs/console"
) )
func main() { func main() {

View File

@ -1,14 +1,15 @@
package llm package llm
import ( import (
"apigo.cc/ai/llm/llm"
"apigo.cc/apigo/gojs"
"apigo.cc/apigo/gojs/dop251/goja"
"bytes" "bytes"
_ "embed" _ "embed"
"text/template"
"apigo.cc/ai/llm/llm"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/config" "github.com/ssgo/config"
"github.com/ssgo/u" "github.com/ssgo/u"
"text/template"
) )
//go:embed llm.ts //go:embed llm.ts
@ -27,8 +28,8 @@ func SetSSKey(key, iv []byte) {
} }
} }
func init() { var jsObj gojs.Map
list := map[string]*map[string]*struct { var llmList = map[string]*map[string]*struct {
Endpoint string Endpoint string
ApiKey string ApiKey string
ChatConfig llm.ChatConfig ChatConfig llm.ChatConfig
@ -36,45 +37,63 @@ func init() {
Debug bool Debug bool
}{} }{}
jsObj := gojs.Map{} func init() {
llmList := make([]string, 0) llmNames := make([]string, 0)
_ = config.LoadConfig("llm", &list) _ = config.LoadConfig("llm", &llmList)
for llmName, llmConfigs := range list { for llmName, llmConfigs := range llmList {
for confName, llmConf := range *llmConfigs { for confName, _ := range *llmConfigs {
llmConf.ApiKey = confAes.DecryptUrlBase64ToString(llmConf.ApiKey)
if confName == "default" { if confName == "default" {
confName = llmName confName = llmName
} }
llmList = append(llmList, confName) llmNames = append(llmNames, 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 tpl *template.Template
var err error var err error
llmTSCode := "" llmTSCode := ""
if tpl, err = template.New("").Parse(llmTS); err == nil { if tpl, err = template.New("").Parse(llmTS); err == nil {
buf := bytes.NewBuffer(make([]byte, 0)) buf := bytes.NewBuffer(make([]byte, 0))
if err = tpl.Execute(buf, llmList); err == nil { if err = tpl.Execute(buf, llmNames); err == nil {
llmTSCode = string(buf.Bytes()) llmTSCode = string(buf.Bytes())
} }
} }
gojs.Register("llm", gojs.Module{ gojs.Register("apigo.cc/ai/llm", gojs.Module{
Object: jsObj, ObjectMaker: func(vm *goja.Runtime) gojs.Map {
if jsObj == nil {
makeJsObj()
}
return jsObj
},
TsCode: llmTSCode, TsCode: llmTSCode,
Desc: "llm plugin for gojs(http://apigo.cc/apigo/gojs)", Desc: "llm plugin for gojs(http://apigo.cc/gojs)",
Example: llmMD, Example: llmMD,
}) })
} }
func makeJsObj() {
jsObj = make(gojs.Map)
for llmName, llmConfigs := range llmList {
for confName, llmConf := range *llmConfigs {
llmConf.ApiKey = confAes.DecryptUrlBase64ToString(llmConf.ApiKey)
if confName == "default" {
confName = llmName
}
llmObj := llm.Create(confName, llmName, llm.Config{
Endpoint: llmConf.Endpoint,
ApiKey: llmConf.ApiKey,
ChatConfig: llmConf.ChatConfig,
GCConfig: llmConf.GCConfig,
Debug: llmConf.Debug,
})
if llmObj != nil {
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)))
}
}

27
go.mod
View File

@ -3,13 +3,17 @@ module apigo.cc/ai/llm
go 1.18 go 1.18
require ( require (
apigo.cc/apigo/gojs v0.1.1 apigo.cc/gojs v0.0.1
github.com/go-resty/resty/v2 v2.15.2 // indirect apigo.cc/gojs/console v0.0.1
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect apigo.cc/gojs/db v0.0.1
github.com/sashabaranov/go-openai v1.30.3 apigo.cc/gojs/file v0.0.1
apigo.cc/gojs/http v0.0.1
apigo.cc/gojs/log v0.0.1
apigo.cc/gojs/util v0.0.2
github.com/sashabaranov/go-openai v1.32.0
github.com/ssgo/config v1.7.7 github.com/ssgo/config v1.7.7
github.com/ssgo/log v1.7.7 github.com/ssgo/log v1.7.7
github.com/ssgo/u v1.7.7 github.com/ssgo/u v1.7.9
github.com/yankeguo/zhipu v0.1.2 github.com/yankeguo/zhipu v0.1.2
) )
@ -17,17 +21,20 @@ require (
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-resty/resty/v2 v2.14.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ssgo/dao v0.1.5 // indirect github.com/ssgo/dao v0.1.5 // indirect
github.com/ssgo/db v1.7.9 // indirect github.com/ssgo/db v1.7.9 // indirect
github.com/ssgo/httpclient v1.7.7 // indirect github.com/ssgo/httpclient v1.7.8 // indirect
github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/net v0.29.0 // indirect golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.25.0 // indirect golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.18.0 // indirect golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

View File

@ -1,14 +1,32 @@
package main package main
import ( import (
_ "apigo.cc/ai/llm"
"apigo.cc/apigo/gojs"
_ "apigo.cc/apigo/gojs/modules"
"fmt" "fmt"
"github.com/ssgo/u"
"os" "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() { func main() {
args := os.Args[1:] args := os.Args[1:]

11
llm.go
View File

@ -1,14 +1,13 @@
package llm package llm
import ( import (
"apigo.cc/ai/llm/llm"
_ "apigo.cc/ai/llm/openai"
_ "apigo.cc/ai/llm/zhipu"
"apigo.cc/apigo/gojs"
"apigo.cc/apigo/gojs/dop251/goja"
"github.com/ssgo/u"
"reflect" "reflect"
"strings" "strings"
"apigo.cc/ai/llm/llm"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/u"
) )
func MakeLLM(lm llm.LLM) map[string]any { func MakeLLM(lm llm.LLM) map[string]any {

View File

@ -1,12 +1,13 @@
package llm package llm
import ( import (
_ "apigo.cc/ai/llm/zhipu"
"apigo.cc/apigo/gojs"
_ "apigo.cc/apigo/gojs/modules"
"fmt" "fmt"
"github.com/ssgo/u"
"testing" "testing"
_ "apigo.cc/ai/llm/zhipu"
"apigo.cc/gojs"
_ "apigo.cc/gojs/console"
"github.com/ssgo/u"
) )
//func TestZhipu(t *testing.T) { //func TestZhipu(t *testing.T) {
@ -23,6 +24,8 @@ import (
//} //}
func TestExport(t *testing.T) { func TestExport(t *testing.T) {
gojs.Alias("llm", "apigo.cc/ai/llm")
gojs.Alias("console", "apigo.cc/gojs/console")
gojs.ExportForDev() gojs.ExportForDev()
} }