Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -2,7 +2,7 @@ import express from 'express';
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import dotenv from 'dotenv';
|
| 4 |
import OpenAI from "openai";
|
| 5 |
-
import { BedrockRuntimeClient, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
|
| 6 |
import { NodeHttpHandler } from "@smithy/node-http-handler";
|
| 7 |
|
| 8 |
dotenv.config();
|
|
@@ -20,7 +20,7 @@ const bedrockClient = new BedrockRuntimeClient({
|
|
| 20 |
region: "us-east-1" ,
|
| 21 |
requestHandler: new NodeHttpHandler({
|
| 22 |
http2Handler: undefined,
|
| 23 |
-
})
|
| 24 |
});
|
| 25 |
|
| 26 |
const azureOpenAI = new OpenAI({
|
|
@@ -30,7 +30,7 @@ const azureOpenAI = new OpenAI({
|
|
| 30 |
defaultHeaders: { "api-key": "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P" }
|
| 31 |
});
|
| 32 |
|
| 33 |
-
// ---
|
| 34 |
app.post('/api/generate', async (req, res) => {
|
| 35 |
const { model, prompt, system_prompt} = req.body;
|
| 36 |
console.log(`[TRAFFIC] Request for ${model}`);
|
|
@@ -74,9 +74,8 @@ app.post('/api/stream', async (req, res) => {
|
|
| 74 |
console.log(`[STREAM] Request for ${model}`);
|
| 75 |
|
| 76 |
// Set headers for streaming response
|
| 77 |
-
res.setHeader('Content-Type', 'text/
|
| 78 |
-
res.setHeader('
|
| 79 |
-
res.setHeader('Connection', 'keep-alive');
|
| 80 |
|
| 81 |
try {
|
| 82 |
if (model === "claude") {
|
|
@@ -96,7 +95,9 @@ app.post('/api/stream', async (req, res) => {
|
|
| 96 |
for await (const chunk of response.stream) {
|
| 97 |
if (chunk.contentBlockDelta) {
|
| 98 |
const text = chunk.contentBlockDelta.delta?.text || "";
|
| 99 |
-
if (text)
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
}
|
| 102 |
res.end();
|
|
@@ -114,7 +115,9 @@ app.post('/api/stream', async (req, res) => {
|
|
| 114 |
|
| 115 |
for await (const chunk of stream) {
|
| 116 |
const text = chunk.choices[0]?.delta?.content || "";
|
| 117 |
-
if (text)
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
res.end();
|
| 120 |
}
|
|
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import dotenv from 'dotenv';
|
| 4 |
import OpenAI from "openai";
|
| 5 |
+
import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
|
| 6 |
import { NodeHttpHandler } from "@smithy/node-http-handler";
|
| 7 |
|
| 8 |
dotenv.config();
|
|
|
|
| 20 |
region: "us-east-1" ,
|
| 21 |
requestHandler: new NodeHttpHandler({
|
| 22 |
http2Handler: undefined,
|
| 23 |
+
})
|
| 24 |
});
|
| 25 |
|
| 26 |
const azureOpenAI = new OpenAI({
|
|
|
|
| 30 |
defaultHeaders: { "api-key": "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P" }
|
| 31 |
});
|
| 32 |
|
| 33 |
+
// --- BLOCKING GENERATION (Legacy/Internal) ---
|
| 34 |
app.post('/api/generate', async (req, res) => {
|
| 35 |
const { model, prompt, system_prompt} = req.body;
|
| 36 |
console.log(`[TRAFFIC] Request for ${model}`);
|
|
|
|
| 74 |
console.log(`[STREAM] Request for ${model}`);
|
| 75 |
|
| 76 |
// Set headers for streaming response
|
| 77 |
+
res.setHeader('Content-Type', 'text/plain');
|
| 78 |
+
res.setHeader('Transfer-Encoding', 'chunked');
|
|
|
|
| 79 |
|
| 80 |
try {
|
| 81 |
if (model === "claude") {
|
|
|
|
| 95 |
for await (const chunk of response.stream) {
|
| 96 |
if (chunk.contentBlockDelta) {
|
| 97 |
const text = chunk.contentBlockDelta.delta?.text || "";
|
| 98 |
+
if (text) {
|
| 99 |
+
res.write(text);
|
| 100 |
+
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
res.end();
|
|
|
|
| 115 |
|
| 116 |
for await (const chunk of stream) {
|
| 117 |
const text = chunk.choices[0]?.delta?.content || "";
|
| 118 |
+
if (text) {
|
| 119 |
+
res.write(text);
|
| 120 |
+
}
|
| 121 |
}
|
| 122 |
res.end();
|
| 123 |
}
|