Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -79,12 +79,22 @@ app.post('/gen', async (req, res) => {
|
|
| 79 |
'x-goog-api-key': `${openai_key}`,
|
| 80 |
'Content-Type': 'application/json',
|
| 81 |
},
|
|
|
|
| 82 |
});
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
/*
|
| 90 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
|
|
|
| 79 |
'x-goog-api-key': `${openai_key}`,
|
| 80 |
'Content-Type': 'application/json',
|
| 81 |
},
|
| 82 |
+
responseType: 'stream',
|
| 83 |
});
|
| 84 |
|
| 85 |
+
let fullData = '';
|
| 86 |
+
response.data.on('data', (chunk) => {
|
| 87 |
+
fullData += chunk.toString();
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
await new Promise((resolve) => {
|
| 91 |
+
response.data.on('end', () => {
|
| 92 |
+
resolve();
|
| 93 |
+
});
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
const jsonData = JSON.parse(fullData);
|
| 97 |
+
res.json({ content: jsonData.candidates[0].content.parts.map(part => part.text).join('') });
|
| 98 |
|
| 99 |
/*
|
| 100 |
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|