wirejs-deploy-amplify-basic 0.1.137-llm → 0.1.139-llm
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/dist/services/llm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LLM as BaseLLM } from 'wirejs-resources';
|
|
2
|
-
import { BedrockRuntimeClient,
|
|
2
|
+
import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand, } from '@aws-sdk/client-bedrock-runtime';
|
|
3
3
|
import { defaultProvider } from '@aws-sdk/credential-provider-node';
|
|
4
4
|
export class LLM extends BaseLLM {
|
|
5
5
|
bedrockClient;
|
|
@@ -19,7 +19,9 @@ export class LLM extends BaseLLM {
|
|
|
19
19
|
convertToBedrockFormat(messages) {
|
|
20
20
|
return messages.map(msg => ({
|
|
21
21
|
role: msg.role === 'user' ? 'user' : 'assistant',
|
|
22
|
-
content:
|
|
22
|
+
content: [{
|
|
23
|
+
text: msg.content
|
|
24
|
+
}]
|
|
23
25
|
}));
|
|
24
26
|
}
|
|
25
27
|
getModelId(model) {
|
|
@@ -36,54 +38,49 @@ export class LLM extends BaseLLM {
|
|
|
36
38
|
return modelMap[model] || model;
|
|
37
39
|
}
|
|
38
40
|
async invokeBedrock(modelId, messages, stream) {
|
|
39
|
-
const body = JSON.stringify({
|
|
40
|
-
anthropic_version: "bedrock-2023-05-31",
|
|
41
|
-
max_tokens: 10000,
|
|
42
|
-
messages: this.convertToBedrockFormat(messages)
|
|
43
|
-
});
|
|
44
41
|
if (stream) {
|
|
45
|
-
const command = new
|
|
42
|
+
const command = new ConverseStreamCommand({
|
|
46
43
|
modelId,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
messages: this.convertToBedrockFormat(messages),
|
|
45
|
+
inferenceConfig: {
|
|
46
|
+
maxTokens: 10000
|
|
47
|
+
}
|
|
50
48
|
});
|
|
51
49
|
return await this.bedrockClient.send(command);
|
|
52
50
|
}
|
|
53
51
|
else {
|
|
54
|
-
const command = new
|
|
52
|
+
const command = new ConverseCommand({
|
|
55
53
|
modelId,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
messages: this.convertToBedrockFormat(messages),
|
|
55
|
+
inferenceConfig: {
|
|
56
|
+
maxTokens: 10000
|
|
57
|
+
}
|
|
59
58
|
});
|
|
60
59
|
return await this.bedrockClient.send(command);
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
async streamBedrock(response, onChunk) {
|
|
63
|
+
if (!response.stream)
|
|
64
|
+
return null;
|
|
64
65
|
let content = '';
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
catch (error) {
|
|
84
|
-
console.error(error);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
66
|
+
for await (const chunk of response.stream) {
|
|
67
|
+
const chunkText = chunk.contentBlockDelta?.delta?.text;
|
|
68
|
+
if (chunkText) {
|
|
69
|
+
content += chunkText;
|
|
70
|
+
const llmChunk = {
|
|
71
|
+
created_at: new Date().toISOString(),
|
|
72
|
+
message: {
|
|
73
|
+
role: 'assistant',
|
|
74
|
+
content: chunkText
|
|
75
|
+
},
|
|
76
|
+
done: false
|
|
77
|
+
};
|
|
78
|
+
if (onChunk) {
|
|
79
|
+
try {
|
|
80
|
+
await onChunk(llmChunk);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error(error);
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
86
|
}
|
|
@@ -128,7 +125,9 @@ export class LLM extends BaseLLM {
|
|
|
128
125
|
// throw a fit wins.
|
|
129
126
|
for (const model of this.models) {
|
|
130
127
|
try {
|
|
131
|
-
|
|
128
|
+
const result = await this.invokeModel(model, history, onChunk);
|
|
129
|
+
if (!result)
|
|
130
|
+
throw new Error("No response from model.");
|
|
132
131
|
}
|
|
133
132
|
catch (error) {
|
|
134
133
|
console.log(error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-deploy-amplify-basic",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.139-llm",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"recursive-copy": "^2.0.14",
|
|
45
45
|
"rimraf": "^6.0.1",
|
|
46
46
|
"wirejs-dom": "^1.0.42",
|
|
47
|
-
"wirejs-resources": "^0.1.
|
|
47
|
+
"wirejs-resources": "^0.1.139-llm"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@aws-amplify/backend": "^1.14.0",
|