Skip to content

Commit

Permalink
add timeout for openai API stream
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonOstrez committed Jul 20, 2023
1 parent 8091c1c commit e6446dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pythagora",
"version": "0.0.78",
"version": "0.0.79",
"author": {
"name": "Zvonimir Sabljic",
"email": "[email protected]"
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ function setOptions({path, method, headers}) {

async function makeRequest(data, options, customLogFunction) {
let gptResponse = '';
let end = false;
let httpModule = options.protocol === 'http' ? require('http') : require('https');
let timeout;

return new Promise((resolve, reject) => {
const req = httpModule.request(_.omit(options, ['protocol']), function(res){
res.on('data', (chunk) => {
try {
clearTimeout(timeout);
timeout = setTimeout(() => {
reject(new Error("Request timeout"));
}, 30000);

let stringified = chunk.toString();
try {
let json = JSON.parse(stringified);
Expand All @@ -64,6 +69,8 @@ async function makeRequest(data, options, customLogFunction) {
} catch (e) {}
});
res.on('end', async function () {
clearTimeout(timeout);

process.stdout.write('\n');
if (res.statusCode >= 400) return reject(new Error(`Response status code: ${res.statusCode}. Error message: ${gptResponse}`));
if (gptResponse.error) return reject(new Error(`Error: ${gptResponse.error.message}. Code: ${gptResponse.error.code}`));
Expand All @@ -74,6 +81,7 @@ async function makeRequest(data, options, customLogFunction) {
});

req.on('error', (e) => {
clearTimeout(timeout);
console.error("problem with request:"+e.message);
reject(e);
});
Expand Down

0 comments on commit e6446dc

Please sign in to comment.