st-comp 0.0.249 → 0.0.251
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/VarietyAiHelper.cjs +3 -2
- package/es/VarietyAiHelper.js +220 -173
- package/es/aiTools.js +41 -8
- package/es/style.css +1 -1
- package/lib/aiTools.js +41 -8
- package/lib/bundle.js +1 -1
- package/lib/bundle.umd.cjs +9 -8
- package/lib/{index-6ac0579e.js → index-013031db.js} +504 -457
- package/lib/{python-0432340a.js → python-693b64f2.js} +1 -1
- package/lib/style.css +1 -1
- package/package.json +1 -1
- package/packages/VarietyAiHelper/index.vue +106 -33
- package/public/aiTools.js +41 -8
package/lib/aiTools.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* 发送非流式请求到百炼 AI 应用
|
|
3
|
-
* @param {Object} options
|
|
4
|
-
* @param {string} options.appId - 应用ID
|
|
5
|
-
* @param {string} options.apiKey - API Key
|
|
6
|
-
* @param {string} options.value - 用户输入
|
|
7
|
-
* @returns {Promise<string>} 返回 AI 输出的完整文本(JSON 字符串)
|
|
8
|
-
*/
|
|
1
|
+
// 非流式返回
|
|
9
2
|
export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) => {
|
|
10
3
|
try {
|
|
11
4
|
const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
|
|
@@ -34,3 +27,43 @@ export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) =>
|
|
|
34
27
|
throw error;
|
|
35
28
|
}
|
|
36
29
|
};
|
|
30
|
+
// 流式返回
|
|
31
|
+
export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback }) => {
|
|
32
|
+
try {
|
|
33
|
+
const res = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
input: { prompt: value },
|
|
37
|
+
parameters: {
|
|
38
|
+
incremental_output: "true", // 流式返回
|
|
39
|
+
},
|
|
40
|
+
debug: {},
|
|
41
|
+
}),
|
|
42
|
+
headers: {
|
|
43
|
+
Authorization: `Bearer ${apiKey}`,
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
"X-DashScope-SSE": "enable", // 流式输出
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
if (!res.ok || !res.body) throw new Error("请求失败");
|
|
49
|
+
|
|
50
|
+
const reader = res.body.getReader();
|
|
51
|
+
const decoder = new TextDecoder();
|
|
52
|
+
|
|
53
|
+
while (true) {
|
|
54
|
+
const { done, value } = await reader.read();
|
|
55
|
+
if (done && callback) {
|
|
56
|
+
callback("finish", "");
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const data = decoder.decode(value, { stream: true });
|
|
61
|
+
const resData = JSON.parse(data.split("\n")[3].substr(5));
|
|
62
|
+
const resText = resData?.output?.text;
|
|
63
|
+
if (resText && callback) callback("message", resText);
|
|
64
|
+
} catch (error) {}
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error(error);
|
|
68
|
+
}
|
|
69
|
+
};
|
package/lib/bundle.js
CHANGED