2024-09-07 23:17:32 +08:00
|
|
|
|
package tests
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"apigo.cc/ai/ai"
|
2024-09-29 21:20:28 +08:00
|
|
|
|
llm2 "apigo.cc/ai/ai/interface/llm"
|
2024-09-17 18:44:21 +08:00
|
|
|
|
"apigo.cc/ai/ai/js"
|
2024-09-18 18:29:21 +08:00
|
|
|
|
"encoding/hex"
|
2024-09-07 23:17:32 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/ssgo/u"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAgent(t *testing.T) {
|
2024-09-17 18:44:21 +08:00
|
|
|
|
ai.Init()
|
|
|
|
|
//testChat(t, "gpt")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
// 尚未支持
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testCode(t, "gpt")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
// keya 消耗过大,计费不合理
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testAskWithImage(t, "gpt", "4032.jpg")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
// keya 不支持
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testMakeImage(t, "gpt", "冬天大雪纷飞,一个男人身穿军绿色棉大衣,戴着红色围巾和绿色帽子走在铺面大雪的小镇路上", "")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testChat(t, "glm")
|
|
|
|
|
//testCode(t, "glm", "", keys.Zhipu)
|
|
|
|
|
//testSearch(t, "glm", "", keys.Zhipu)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
|
|
|
|
// 测试图片识别
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testAskWithImage(t, "glm", "", keys.Zhipu, "4032.jpg")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
|
|
|
|
// 视频似乎尚不支持 glm-4v-plus
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testAskWithVideo(t, "glm", "", keys.Zhipu, "glm-4v", "1080.mp4")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testMakeImage(t, "glm", "", keys.Zhipu, "冬天大雪纷飞,一个男人身穿军绿色棉大衣,戴着红色围巾和绿色帽子走在铺面大雪的小镇路上", "")
|
|
|
|
|
//testMakeVideo(t, "glm", "", keys.Zhipu, "大雪纷飞,男人蹦蹦跳跳", "https://aigc-files.bigmodel.cn/api/cogview/20240904133130c4b7121019724aa3_0.png")
|
|
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
|
//testJS(t)
|
2024-09-17 18:44:21 +08:00
|
|
|
|
//testFile(t)
|
2024-09-20 16:50:35 +08:00
|
|
|
|
testHttp(t)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testChat(t *testing.T, llmName string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, usage, err := lm.FastAsk(llm2.Messages().User().Text("你是什么模型,请给出具体名称、版本号").Make(), func(text string) {
|
2024-09-07 23:17:32 +08:00
|
|
|
|
fmt.Print(u.BCyan(text))
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(usage))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testCode(t *testing.T, llmName string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, usage, err := lm.CodeInterpreterAsk(llm2.Messages().User().Text("计算[5,10,20,700,99,310,978,100]的平均值和方差。").Make(), func(text string) {
|
2024-09-07 23:17:32 +08:00
|
|
|
|
fmt.Print(u.BCyan(text))
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(usage))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testSearch(t *testing.T, llmName string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, usage, err := lm.WebSearchAsk(llm2.Messages().User().Text("今天上海的天气怎么样?").Make(), func(text string) {
|
2024-09-07 23:17:32 +08:00
|
|
|
|
fmt.Print(u.BCyan(text))
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(usage))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testAskWithImage(t *testing.T, llmName, imageFile string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ask := `请回答:
|
|
|
|
|
1、我用的是什么操作系统?
|
|
|
|
|
2、现在的日期时间是?请给出年月日。
|
|
|
|
|
3、正在用什么软件播放什么歌?谁演唱的?歌曲的大意是?
|
|
|
|
|
4、后面的浏览器中正在浏览什么内容?猜测一下我浏览这个网页是想干嘛?
|
|
|
|
|
`
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, usage, err := lm.MultiAsk(llm2.Messages().User().Text(ask).Image("data:image/jpeg;base64,"+u.Base64(u.ReadFileBytesN(imageFile))).Make(), func(text string) {
|
2024-09-07 23:17:32 +08:00
|
|
|
|
fmt.Print(u.BCyan(text))
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(usage))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testAskWithVideo(t *testing.T, llmName, videoFile string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ask := `请回答:
|
|
|
|
|
1、正在用什么软件播放什么歌?谁演唱的?歌曲的大意是?
|
|
|
|
|
4、后面的浏览器中正在浏览什么内容?猜测一下我浏览这个网页是想干嘛?
|
|
|
|
|
`
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, usage, err := lm.MultiAsk(llm2.Messages().User().Text(ask).Video("data:video/mp4,"+u.Base64(u.ReadFileBytesN(videoFile))).Make(), func(text string) {
|
2024-09-07 23:17:32 +08:00
|
|
|
|
fmt.Print(u.BCyan(text))
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(usage))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testMakeImage(t *testing.T, llmName, prompt, refImage string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, err := lm.FastMakeImage(prompt, llm2.GCConfig{
|
2024-09-17 18:44:21 +08:00
|
|
|
|
Size: "1024x1024",
|
|
|
|
|
})
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
for i, v := range r {
|
|
|
|
|
fmt.Println("result:", i, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
func testMakeVideo(t *testing.T, llmName, prompt, refImage string) {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
lm := llm2.Get(llmName)
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if lm == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
|
t.Fatal("interface is nil")
|
2024-09-07 23:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
|
r, covers, err := lm.FastMakeVideo(prompt, llm2.GCConfig{
|
2024-09-17 18:44:21 +08:00
|
|
|
|
Size: "1280x720",
|
|
|
|
|
Ref: refImage,
|
|
|
|
|
})
|
2024-09-07 23:17:32 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
for i, v := range r {
|
|
|
|
|
fmt.Println("result:", i, v, covers[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-17 18:44:21 +08:00
|
|
|
|
|
|
|
|
|
func testJS(t *testing.T) {
|
2024-09-18 18:29:21 +08:00
|
|
|
|
r1, err := ai.RunFile("test.js", "1+2=4吗")
|
2024-09-17 18:44:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
r := js.ChatResult{}
|
|
|
|
|
u.Convert(r1, &r)
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r.Result)
|
|
|
|
|
fmt.Println("usage:", u.JsonP(r.TokenUsage))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testFile(t *testing.T) {
|
2024-09-18 18:29:21 +08:00
|
|
|
|
r1, err := ai.Run(`
|
2024-09-17 18:44:21 +08:00
|
|
|
|
import fs from './lib/file'
|
|
|
|
|
import out from './lib/console'
|
|
|
|
|
let r = fs.read('test.js')
|
|
|
|
|
return r
|
|
|
|
|
`, "test.js")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r1)
|
|
|
|
|
}
|
2024-09-18 18:29:21 +08:00
|
|
|
|
|
2024-09-20 16:50:35 +08:00
|
|
|
|
func testHttp(t *testing.T) {
|
|
|
|
|
fmt.Println(ai.Run(`
|
|
|
|
|
return http.do('GET','https://apigo.cc',null,str=>{console.info(str)},{aaa:111,bbb:222})
|
|
|
|
|
`, ""))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
|
func testUtil(t *testing.T) {
|
|
|
|
|
fmt.Println(hex.EncodeToString(u.Sha256([]byte{0, 98, 2})))
|
|
|
|
|
r, err := ai.Run(`
|
|
|
|
|
let s = String.fromCharCode(0, 98, 2)
|
|
|
|
|
//let r = util.base64(s)
|
|
|
|
|
//let r2 = util.unBase64(r)
|
|
|
|
|
//console.info(r)
|
|
|
|
|
//console.info(r2.length, r2)
|
|
|
|
|
//console.info(s.charCodeAt(0), r2.charCodeAt(0))
|
|
|
|
|
//return r2 === s
|
|
|
|
|
let r = util.hex(util.sha256(s))
|
|
|
|
|
console.info(r)
|
|
|
|
|
return r == '278f25d5e7c09b2cdde1ed72dd83fbb7a7d7dd668ef967c9af65f68aa04049cd'
|
|
|
|
|
`, "test.js")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("发生错误", err.Error())
|
|
|
|
|
}
|
|
|
|
|
if r != true {
|
|
|
|
|
t.Fatal("运行结果不正确", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Println("result:", r)
|
|
|
|
|
}
|