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
```javascript
import {zhipu} from 'llm'
import console from 'console'
import {zhipu} from 'apigo.cc/ai/llm'
import console from 'apigo.cc/gojs/console'
function main(...args) {
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
## 将 [llm](https://apigo.cc/ai/llm) 和 [低代码](https://apigo.cc/apigo/gojs) 集成到应用
## 将 [llm](https://apigo.cc/ai/llm) 和 [低代码](https://apigo.cc/gojs) 集成到应用
### Install
```shell
go get -u apigo.cc/apigo/gojs
go get -u apigo.cc/gojs
go get -u apigo.cc/ai/llm
```
@ -85,8 +85,8 @@ package main
import (
_ "apigo.cc/ai/llm"
"apigo.cc/apigo/gojs"
_ "apigo.cc/apigo/gojs/modules"
"apigo.cc/gojs"
_ "apigo.cc/gojs/console"
)
func main() {

View File

@ -1,14 +1,15 @@
package llm
import (
"apigo.cc/ai/llm/llm"
"apigo.cc/apigo/gojs"
"apigo.cc/apigo/gojs/dop251/goja"
"bytes"
_ "embed"
"text/template"
"apigo.cc/ai/llm/llm"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/config"
"github.com/ssgo/u"
"text/template"
)
//go:embed llm.ts
@ -27,8 +28,8 @@ func SetSSKey(key, iv []byte) {
}
}
func init() {
list := map[string]*map[string]*struct {
var jsObj gojs.Map
var llmList = map[string]*map[string]*struct {
Endpoint string
ApiKey string
ChatConfig llm.ChatConfig
@ -36,45 +37,63 @@ func init() {
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)
func init() {
llmNames := make([]string, 0)
_ = config.LoadConfig("llm", &llmList)
for llmName, llmConfigs := range llmList {
for confName, _ := range *llmConfigs {
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)
llmNames = append(llmNames, confName)
}
}
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 {
if err = tpl.Execute(buf, llmNames); err == nil {
llmTSCode = string(buf.Bytes())
}
}
gojs.Register("llm", gojs.Module{
Object: jsObj,
gojs.Register("apigo.cc/ai/llm", gojs.Module{
ObjectMaker: func(vm *goja.Runtime) gojs.Map {
if jsObj == nil {
makeJsObj()
}
return jsObj
},
TsCode: llmTSCode,
Desc: "llm plugin for gojs(http://apigo.cc/apigo/gojs)",
Desc: "llm plugin for gojs(http://apigo.cc/gojs)",
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
require (
apigo.cc/apigo/gojs v0.1.1
github.com/go-resty/resty/v2 v2.15.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/sashabaranov/go-openai v1.30.3
apigo.cc/gojs v0.0.1
apigo.cc/gojs/console v0.0.1
apigo.cc/gojs/db v0.0.1
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/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
)
@ -17,17 +21,20 @@ require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/dlclark/regexp2 v1.11.4 // 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-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/ssgo/dao v0.1.5 // 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/tool v0.4.27 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

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

11
llm.go
View File

@ -1,14 +1,13 @@
package llm
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"
"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 {

View File

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