commit 939b185d1481a302e028613a1289129cf06b272c Author: STARAI\Star Date: Mon Sep 9 00:01:35 2024 +0800 just test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6aa1496 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.* +!.gitignore +go.sum +/build +/node_modules +/package.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f166f95 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 apigo + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fdd269 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module ai + +go 1.22 + +toolchain go1.22.4 + +require ( + apigo.cc/ai/agent v0.0.1 + apigo.cc/ai/ai v0.0.1 + apigo.cc/apigo/plugin v1.0.2 + github.com/ssgo/u v1.7.7 +) + +require ( + github.com/ssgo/config v1.7.7 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/plugin.go b/plugin.go new file mode 100644 index 0000000..ef0bbd2 --- /dev/null +++ b/plugin.go @@ -0,0 +1,55 @@ +package ai + +import ( + "apigo.cc/ai/agent" + "apigo.cc/ai/ai" + "apigo.cc/apigo/plugin" + "github.com/ssgo/u" +) + +func init() { + plugin.Register(plugin.Plugin{ + Id: "apigo.cc/apigo/ai", + Name: "", + Objects: map[string]interface{}{ + "getAgent": func(name string) agent.Agent { + return ai.GetAgent(name) + }, + "messages": func() *ai.MessagesMaker { + return ai.Messages() + }, + }, + ConfigSample: `zhipu: # 代理名称 + apiKey: xxxx # API密钥 +ai01: # 自定义名称 + apiKey: xxxx # API密钥 + endpoint: https://xxxx # 可以指定一个接入点(例如代理) + agent: zhipu # 当名称不是代理ID时需指定 + chatConfig: # 默认的模型配置 + model: GLM-4V # 模型名称 + maxTokens: 1000 # 最大Token数 + temperature: 0.8 # 生成文本时使用的随机性参数 + topP: 0.7 # 生成文本时使用的随机性参数 + tools: # 工具列表 + codeInterpreter: # 代码解释器(不需要指定值) + webSearch: # 联网搜索(不需要指定值) +`, + Init: func(config map[string]interface{}) { + for agentName, conf1 := range config { + conf := ai.AgentConfig{} + u.Convert(conf1, &conf) + agent.CreateAgent(agentName, conf.Agent, agent.APIConfig{ + Endpoint: conf.Endpoint, + ApiKey: conf.Endpoint, + DefaultChatModelConfig: agent.ChatModelConfig{ + Model: conf.ChatConfig.Model, + MaxTokens: conf.ChatConfig.MaxTokens, + Temperature: conf.ChatConfig.Temperature, + TopP: conf.ChatConfig.TopP, + Tools: conf.ChatConfig.Tools, + }, + }) + } + }, + }) +} diff --git a/tests/go.mod b/tests/go.mod new file mode 100644 index 0000000..20a4ba0 --- /dev/null +++ b/tests/go.mod @@ -0,0 +1,24 @@ +module tests + +go 1.22 + +toolchain go1.22.4 + +replace current-plugin v0.0.0 => ../ + +require ( + apigo.cc/apigo/gojs v0.0.10 + current-plugin v0.0.0 + github.com/ssgo/u v1.7.7 +) + +require ( + apigo.cc/ai/agent v0.0.1 // indirect + apigo.cc/ai/ai v0.0.1 // indirect + apigo.cc/apigo/plugin v1.0.2 // indirect + apigo.cc/apigo/quickjs-go v0.4.12 // indirect + github.com/ssgo/config v1.7.7 // indirect + github.com/ssgo/log v1.7.7 // indirect + github.com/ssgo/standard v1.7.7 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/tests/plugin_test.go b/tests/plugin_test.go new file mode 100644 index 0000000..71861a2 --- /dev/null +++ b/tests/plugin_test.go @@ -0,0 +1,27 @@ +package ai_test + +import ( +"apigo.cc/apigo/gojs" +_ "current-plugin" +"fmt" +"github.com/ssgo/u" +"os" +"strings" +"testing" +) + +func TestPlugin(t *testing.T) { + if files, err := os.ReadDir("."); err == nil { + for _, f := range files { + if !f.IsDir() && strings.HasSuffix(f.Name(), "_test.js") { + testName := f.Name()[0 : len(f.Name())-8] + r, err := gojs.RunFile(f.Name(), nil) + if err != nil || r != true { + t.Fatal("test "+testName+" failed", r, err) + } else { + fmt.Println(u.Green("test "+testName), u.BGreen("OK")) + } + } + } + } +} diff --git a/tests/plugin_test.js b/tests/plugin_test.js new file mode 100644 index 0000000..e678771 --- /dev/null +++ b/tests/plugin_test.js @@ -0,0 +1,17 @@ +import console from 'console' +import ai from 'ai' + +// ai.set('aaa', 111) +// let aaa = ai.get('aaa') +// if (aaa !== 111) return 'set value error' +// +// try { +// ai.get('bbb') +// return 'no exception when getting non-existent key' +// } catch (ex){ +// logger.error(ex.message, {stack:ex.stack}) +// } +// logger.info('aaa') +// ai.remove('bbb') +// +return true