everydaycats commited on
Commit
d385cab
·
verified ·
1 Parent(s): ddb48f2

Delete ai_engine2.js

Browse files
Files changed (1) hide show
  1. ai_engine2.js +0 -101
ai_engine2.js DELETED
@@ -1,101 +0,0 @@
1
- import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
2
- import { NodeHttpHandler } from "@smithy/node-http-handler";
3
-
4
- const bedrockClient = new BedrockRuntimeClient({
5
- region: "us-east-1",
6
- requestHandler: new NodeHttpHandler({ http2Handler: undefined })
7
- });
8
-
9
- function getModelId(model) {
10
- if (model === "maverick") return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/us.meta.llama4-maverick-17b-instruct-v1:0";
11
- if (model === "haiku") return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-haiku-4-5-20251001-v1:0";
12
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-sonnet-4-6";
13
- }
14
-
15
- export const generateCompletion = async ({ model, prompt, system_prompt, images }) => {
16
- try {
17
- let contentBlock =[];
18
-
19
- // 🚨 THE FIX: AWS SDK v3 requires Uint8Array for images.
20
- // A standard Node Buffer will cause a "Missing Keys" Validation Error!
21
- if (images && Array.isArray(images) && images.length > 0) {
22
- images.forEach((imgStr) => {
23
- if (typeof imgStr === 'string') {
24
- // Strip the base64 prefix just in case it was sent from the frontend
25
- const b64 = imgStr.replace(/^data:image\/\w+;base64,/, "");
26
- const buffer = Buffer.from(b64, 'base64');
27
-
28
- contentBlock.push({
29
- image: {
30
- format: 'jpeg',
31
- source: { bytes: new Uint8Array(buffer) } // <-- The crucial fix
32
- }
33
- });
34
- }
35
- });
36
- }
37
-
38
- // Always put the text prompt at the end of the content array
39
- contentBlock.push({ text: prompt });
40
-
41
- const command = new ConverseCommand({
42
- modelId: getModelId(model),
43
- system: [{ text: system_prompt || "You are an elite video metadata extractor." }],
44
- messages: [{ role: "user", content: contentBlock }],
45
- inferenceConfig: { maxTokens: 4000, temperature: 0.7 }
46
- });
47
-
48
- const response = await bedrockClient.send(command);
49
- const text = response.output.message.content.find(b => b.text)?.text;
50
-
51
- return { success: true, data: text };
52
-
53
- } catch (error) {
54
- console.error("❌ AI Engine Error:", error.message);
55
- // Log deep AWS validation issues if they happen
56
- if (error.$metadata) {
57
- console.error("AWS Meta Details:", error.$metadata);
58
- }
59
- return { success: false, error: error.message };
60
- }
61
- };
62
-
63
- // If you are using the streaming function anywhere, apply the exact same fix:
64
- export const streamCompletion = async ({ model, prompt, system_prompt, images, res }) => {
65
- try {
66
- const bedrockModelId = getModelId(model);
67
- let contentBlock =[];
68
-
69
- if (images && Array.isArray(images) && images.length > 0) {
70
- images.forEach((imgStr) => {
71
- if (typeof imgStr === 'string') {
72
- const b64 = imgStr.replace(/^data:image\/\w+;base64,/, "");
73
- const buffer = Buffer.from(b64, 'base64');
74
- contentBlock.push({
75
- image: { format: 'jpeg', source: { bytes: new Uint8Array(buffer) } }
76
- });
77
- }
78
- });
79
- }
80
- contentBlock.push({ text: prompt });
81
-
82
- const command = new ConverseStreamCommand({
83
- modelId: bedrockModelId,
84
- system:[{ text: system_prompt || "You are a professional assistant." }],
85
- messages: [{ role: "user", content: contentBlock }],
86
- inferenceConfig: { maxTokens: 4000, temperature: 0.7 }
87
- });
88
-
89
- const response = await bedrockClient.send(command);
90
- for await (const chunk of response.stream) {
91
- if (chunk.contentBlockDelta?.delta?.text) {
92
- res.write(chunk.contentBlockDelta.delta.text);
93
- }
94
- }
95
- res.end();
96
- } catch (error) {
97
- console.error("❌ Stream Error:", error.message);
98
- res.write(`\n\nERROR: ${error.message}`);
99
- res.end();
100
- }
101
- };