Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -10,7 +10,7 @@ const app = express();
|
|
| 10 |
const PORT = process.env.PORT || 7860;
|
| 11 |
|
| 12 |
app.use(cors());
|
| 13 |
-
app.use(express.json({ limit: '50mb' }));
|
| 14 |
|
| 15 |
// --- SYSTEM PROMPT DEFINITIONS ---
|
| 16 |
const CLAUDE_SYSTEM_PROMPT = "You are a pro. Provide elite, high-level technical responses.";
|
|
@@ -30,10 +30,9 @@ const azureOpenAI = new OpenAI({
|
|
| 30 |
defaultHeaders: { "api-key": "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P" }
|
| 31 |
});
|
| 32 |
|
|
|
|
| 33 |
app.post('/api/generate', async (req, res) => {
|
| 34 |
const { model, prompt, system_prompt} = req.body;
|
| 35 |
-
console.log(`[TRAFFIC] Request for ${model}`);
|
| 36 |
-
|
| 37 |
try {
|
| 38 |
if (model === "claude") {
|
| 39 |
const command = new ConverseCommand({
|
|
@@ -46,7 +45,6 @@ app.post('/api/generate', async (req, res) => {
|
|
| 46 |
output_config: { effort: "high" }
|
| 47 |
}
|
| 48 |
});
|
| 49 |
-
|
| 50 |
const response = await bedrockClient.send(command);
|
| 51 |
const text = response.output.message.content.find(b => b.text)?.text;
|
| 52 |
res.json({ success: true, data: text });
|
|
@@ -62,15 +60,15 @@ app.post('/api/generate', async (req, res) => {
|
|
| 62 |
res.json({ success: true, data: response.choices[0].message.content });
|
| 63 |
}
|
| 64 |
} catch (err) {
|
| 65 |
-
console.error(`❌ [${model.toUpperCase()} ERROR]:`, err.
|
| 66 |
-
res.status(500).json({ success: false, error:
|
| 67 |
}
|
| 68 |
});
|
| 69 |
|
| 70 |
-
//
|
| 71 |
app.post('/api/stream', async (req, res) => {
|
| 72 |
-
const { model, prompt, system_prompt, images } = req.body;
|
| 73 |
-
console.log(`[STREAM] Request for ${model}
|
| 74 |
|
| 75 |
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
| 76 |
res.setHeader('Transfer-Encoding', 'chunked');
|
|
@@ -79,20 +77,11 @@ app.post('/api/stream', async (req, res) => {
|
|
| 79 |
|
| 80 |
try {
|
| 81 |
if (model === "claude") {
|
| 82 |
-
// Construct Content Block for Claude
|
| 83 |
let contentBlock = [{ text: prompt }];
|
| 84 |
-
|
| 85 |
if (images && images.length > 0) {
|
| 86 |
-
// Prepend images to content block
|
| 87 |
-
// Bedrock expects pure base64 without data prefix
|
| 88 |
const imageBlocks = images.map(imgStr => {
|
| 89 |
const base64Data = imgStr.replace(/^data:image\/\w+;base64,/, "");
|
| 90 |
-
return {
|
| 91 |
-
image: {
|
| 92 |
-
format: 'png', // Assuming PNG or generic byte handling
|
| 93 |
-
source: { bytes: Buffer.from(base64Data, 'base64') }
|
| 94 |
-
}
|
| 95 |
-
};
|
| 96 |
});
|
| 97 |
contentBlock = [...imageBlocks, ...contentBlock];
|
| 98 |
}
|
|
@@ -102,42 +91,23 @@ app.post('/api/stream', async (req, res) => {
|
|
| 102 |
system: [{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
|
| 103 |
messages: [{ role: "user", content: contentBlock }],
|
| 104 |
inferenceConfig: { maxTokens: 48000, temperature: 1 },
|
| 105 |
-
additionalModelRequestFields: {
|
| 106 |
-
thinking: { type: "adaptive" },
|
| 107 |
-
output_config: { effort: "high" }
|
| 108 |
-
}
|
| 109 |
});
|
| 110 |
|
| 111 |
const response = await bedrockClient.send(command);
|
| 112 |
-
|
| 113 |
for await (const chunk of response.stream) {
|
| 114 |
if (chunk.contentBlockDelta) {
|
| 115 |
const delta = chunk.contentBlockDelta.delta;
|
| 116 |
-
if (delta.reasoningContent
|
| 117 |
-
|
| 118 |
-
} else if (delta.text) {
|
| 119 |
-
res.write(delta.text);
|
| 120 |
-
}
|
| 121 |
}
|
| 122 |
}
|
| 123 |
res.end();
|
| 124 |
-
|
| 125 |
} else {
|
| 126 |
-
|
| 127 |
-
let messagesPayload = [
|
| 128 |
-
{ role: "system", content: system_prompt || GPT_SYSTEM_PROMPT }
|
| 129 |
-
];
|
| 130 |
-
|
| 131 |
-
let userContent = [];
|
| 132 |
if (images && images.length > 0) {
|
| 133 |
-
|
| 134 |
-
userContent.push({ type: "
|
| 135 |
-
images.forEach(imgStr => {
|
| 136 |
-
userContent.push({
|
| 137 |
-
type: "image_url",
|
| 138 |
-
image_url: { url: imgStr } // OpenAI accepts data URI directly
|
| 139 |
-
});
|
| 140 |
-
});
|
| 141 |
messagesPayload.push({ role: "user", content: userContent });
|
| 142 |
} else {
|
| 143 |
messagesPayload.push({ role: "user", content: prompt });
|
|
@@ -152,11 +122,8 @@ app.post('/api/stream', async (req, res) => {
|
|
| 152 |
|
| 153 |
for await (const chunk of stream) {
|
| 154 |
const delta = chunk.choices[0]?.delta;
|
| 155 |
-
if (delta?.reasoning_content) {
|
| 156 |
-
|
| 157 |
-
} else if (delta?.content) {
|
| 158 |
-
res.write(delta.content);
|
| 159 |
-
}
|
| 160 |
}
|
| 161 |
res.end();
|
| 162 |
}
|
|
@@ -167,8 +134,4 @@ app.post('/api/stream', async (req, res) => {
|
|
| 167 |
}
|
| 168 |
});
|
| 169 |
|
| 170 |
-
app.
|
| 171 |
-
res.json({ success: true, data: response.choices[0].message.content });
|
| 172 |
-
});
|
| 173 |
-
|
| 174 |
-
app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));
|
|
|
|
| 10 |
const PORT = process.env.PORT || 7860;
|
| 11 |
|
| 12 |
app.use(cors());
|
| 13 |
+
app.use(express.json({ limit: '50mb' }));
|
| 14 |
|
| 15 |
// --- SYSTEM PROMPT DEFINITIONS ---
|
| 16 |
const CLAUDE_SYSTEM_PROMPT = "You are a pro. Provide elite, high-level technical responses.";
|
|
|
|
| 30 |
defaultHeaders: { "api-key": "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P" }
|
| 31 |
});
|
| 32 |
|
| 33 |
+
// Standard Generation
|
| 34 |
app.post('/api/generate', async (req, res) => {
|
| 35 |
const { model, prompt, system_prompt} = req.body;
|
|
|
|
|
|
|
| 36 |
try {
|
| 37 |
if (model === "claude") {
|
| 38 |
const command = new ConverseCommand({
|
|
|
|
| 45 |
output_config: { effort: "high" }
|
| 46 |
}
|
| 47 |
});
|
|
|
|
| 48 |
const response = await bedrockClient.send(command);
|
| 49 |
const text = response.output.message.content.find(b => b.text)?.text;
|
| 50 |
res.json({ success: true, data: text });
|
|
|
|
| 60 |
res.json({ success: true, data: response.choices[0].message.content });
|
| 61 |
}
|
| 62 |
} catch (err) {
|
| 63 |
+
console.error(`❌ [${model.toUpperCase()} ERROR]:`, err.message);
|
| 64 |
+
res.status(500).json({ success: false, error: err.message });
|
| 65 |
}
|
| 66 |
});
|
| 67 |
|
| 68 |
+
// Streaming Generation
|
| 69 |
app.post('/api/stream', async (req, res) => {
|
| 70 |
+
const { model, prompt, system_prompt, images } = req.body;
|
| 71 |
+
console.log(`[STREAM] Request for ${model}`);
|
| 72 |
|
| 73 |
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
| 74 |
res.setHeader('Transfer-Encoding', 'chunked');
|
|
|
|
| 77 |
|
| 78 |
try {
|
| 79 |
if (model === "claude") {
|
|
|
|
| 80 |
let contentBlock = [{ text: prompt }];
|
|
|
|
| 81 |
if (images && images.length > 0) {
|
|
|
|
|
|
|
| 82 |
const imageBlocks = images.map(imgStr => {
|
| 83 |
const base64Data = imgStr.replace(/^data:image\/\w+;base64,/, "");
|
| 84 |
+
return { image: { format: 'png', source: { bytes: Buffer.from(base64Data, 'base64') } } };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
});
|
| 86 |
contentBlock = [...imageBlocks, ...contentBlock];
|
| 87 |
}
|
|
|
|
| 91 |
system: [{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
|
| 92 |
messages: [{ role: "user", content: contentBlock }],
|
| 93 |
inferenceConfig: { maxTokens: 48000, temperature: 1 },
|
| 94 |
+
additionalModelRequestFields: { thinking: { type: "adaptive" }, output_config: { effort: "high" } }
|
|
|
|
|
|
|
|
|
|
| 95 |
});
|
| 96 |
|
| 97 |
const response = await bedrockClient.send(command);
|
|
|
|
| 98 |
for await (const chunk of response.stream) {
|
| 99 |
if (chunk.contentBlockDelta) {
|
| 100 |
const delta = chunk.contentBlockDelta.delta;
|
| 101 |
+
if (delta.reasoningContent?.text) res.write(`__THINK__${delta.reasoningContent.text}`);
|
| 102 |
+
else if (delta.text) res.write(delta.text);
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
}
|
| 105 |
res.end();
|
|
|
|
| 106 |
} else {
|
| 107 |
+
let messagesPayload = [{ role: "system", content: system_prompt || GPT_SYSTEM_PROMPT }];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if (images && images.length > 0) {
|
| 109 |
+
let userContent = [{ type: "text", text: prompt }];
|
| 110 |
+
images.forEach(img => userContent.push({ type: "image_url", image_url: { url: img } }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
messagesPayload.push({ role: "user", content: userContent });
|
| 112 |
} else {
|
| 113 |
messagesPayload.push({ role: "user", content: prompt });
|
|
|
|
| 122 |
|
| 123 |
for await (const chunk of stream) {
|
| 124 |
const delta = chunk.choices[0]?.delta;
|
| 125 |
+
if (delta?.reasoning_content) res.write(`__THINK__${delta.reasoning_content}`);
|
| 126 |
+
else if (delta?.content) res.write(delta.content);
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
res.end();
|
| 129 |
}
|
|
|
|
| 134 |
}
|
| 135 |
});
|
| 136 |
|
| 137 |
+
app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agents live on port ${PORT}`));
|
|
|
|
|
|
|
|
|
|
|
|