import co from 'apigo.cc/gojs/console' import u from 'apigo.cc/gojs/util' import api from 'apigo.cc/gojs/api' // 注册腾讯云TC3签名器 api.registerSigner("tc3", (req, cfg) => { let action = cfg.string("action", "") let service = cfg.string("service", "") let timestamp = u.timestamp() let contentType = "application/json; charset=utf-8" let canonicalHeaders = "content-type:" + contentType + "\nhost:" + req.finalHost + "\nx-tc-action:" + action.toLowerCase() + "\n" let signedHeaders = "content-type;host;x-tc-action" let hashedRequestPayload = u.hex(u.sha256(req.finalBody)) let canonicalRequest = req.method + "\n" + req.finalPath + "\n" + req.finalQuery + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestPayload let date = u.formatDate("2006-01-02", timestamp) let credentialScope = date + "/" + service + "/tc3_request" let string2sign = "TC3-HMAC-SHA256\n" + timestamp + "\n" + credentialScope + "\n" + u.hex(u.sha256(canonicalRequest)) let secretDate = u.hmacSHA256("TC3" + cfg.string("secretKey", ""), date) let secretService = u.hmacSHA256(secretDate, service) let secretSigning = u.hmacSHA256(secretService, "tc3_request") let signature = u.hex(u.hmacSHA256(secretSigning, string2sign)) let authorization = "TC3-HMAC-SHA256 Credential=" + cfg.string("secretId", "") + "/" + credentialScope + ", SignedHeaders=" + signedHeaders + ", Signature=" + signature req.setHeader("Content-Type", contentType) req.setHeader("X-TC-Action", action) req.setHeader("X-TC-Timestamp", u.string(timestamp)) req.setHeader("X-TC-Version", cfg.string("version", "")) req.setHeader("X-TC-Region", cfg.string("region", "")) req.setHeader("Authorization", authorization) }) // 腾讯云COS签名 api.registerSigner("cos", (req, cfg) => { let startTimestamp = u.timestamp() let keyTime = startTimestamp + ";" + startTimestamp + cfg.int("expiredTime", 600) let signKey = u.hex(u.hmacSHA1(cfg.string("secretKey", ""), keyTime)) let [urlParamList, httpParameters] = api.sortParams(req.query) let [headerList, httpHeaders] = api.sortParams(req.headers) let hashedHttpString = u.hex(u.sha1(req.method.toLowerCase() + "\n" + req.finalPath + "\n" + httpParameters + "\n" + httpHeaders + "\n")) let signature = u.hex(u.hmacSHA1(signKey, "sha1\n" + keyTime + "\n" + hashedHttpString + "\n")) let authorization = "q-sign-algorithm=sha1&q-ak=" + cfg.string("secretId", "") + "&q-sign-time=" + keyTime + "&q-key-time=" + keyTime + "&q-header-list=" + headerList + "&q-url-param-list=" + urlParamList + "&q-signature=" + signature req.setHeader("Authorization", authorization) }) try { // let r = api.tencent.smsPackagesStatistics({ data: { SmsSdkAppId: '1400624676', BeginTime: '2025010100', EndTime: '2045010100' } }) // let r = api.tencent.getBucketList() // let r = api.laoCos.get({ url: "/?max-keys=5" }) // let r = api.laoCos.get({ url: "user/9VKQpY2RH7Ks/avatar.jpg", responseType: 'text/plain' }) // let r = api.laoCos.put({ url: "test/aaa.txt", text: 'hello world' }) // let r = api.laoCos.get({ url: "test/aaa.txt" }) // let r = api.tencent.getCosToken({ config: { path: '/aaa/111' } }) // co.info(r) // 测试llm接口 // let r = api.zhipujwt.do({ url: "https://open.bigmodel.cn/api/paas/v4/chat/completions", data: { model: 'GLM-4.5-Flash', messages: [{ role: 'user', content: '你好' }], thinking: { type: 'disabled' } } }) // test('智谱(jwt):简单对话(.do)', r.statusCode == 200 && !!r.data, r?.data?.choices[0]?.message?.content, r) // testChat('PPIO(openai)', api.openai) // testChat('智谱', api.zhipu) testChat('豆包', api.doubao) // let r = api.zhipu.image({ data: { prompt: '一张猫在沙发上喝咖啡' } }) // test('智谱:图片生成', r.statusCode == 200 && !!r.data, r?.data?.data[0]?.url, r) // let img = r?.data?.data[0]?.url // let img = 'https://aigc-files.bigmodel.cn/api/cogview/20250816103236c2331606d04c460a_0.png' // let r = api.zhipu.chat({ data: { model: 'GLM-4V-Flash', messages: [{ role: 'user', content: [{ type: 'text', text: '看图说话,写一个简短生动的狗血爱情故事' }, { type: 'image_url', image_url: { url: img } }] }] } }) // test('智谱:看图说话', r.statusCode == 200 && !!r.data, r?.data?.choices[0]?.message?.content, r) // 测试文件扫描 // let r = api.textin.scan({ binary: fs.readBytes('testRes/table.jpeg') }) // test('Textin:表格识别', r.statusCode == 200 && !!r.data, r?.data, r) return true } catch (ex) { return ex.message } function testChat(title, llm) { let r = llm.chat({ data: { messages: [{ role: 'user', content: '你好' }] } }) test(title + ':简单对话', r.statusCode == 200 && !!r.data, r?.data?.choices[0]?.message?.content, r) r = llm.chat({ callback: v => { co.println(co.yellow(v.choices[0].delta.content) + ' ') }, data: { messages: [{ role: 'user', content: '你好' }], stream: true } }) test(title + ':流式对话', r.statusCode == 200 && !!r.data, r?.data?.usage?.total_tokens, r) // r = llm.embeddings({ data: { input: '今天天气真好' } }) // test(title + ':向量化', r.statusCode == 200 && !!r.data, r?.data?.data[0]?.embedding?.length, r) } function test(title, condition, successMessage, failedMessage) { if (!condition) { co.info(title, co.bRed('失败'), co.red(u.jsonP(failedMessage))) throw new Error(title + '失败') } co.info(title, co.bGreen('通过'), co.yellow(u.json(successMessage))) }