From 72f8f4de3d13ddecad70fbe7cce48944f8fffab0 Mon Sep 17 00:00:00 2001 From: uncle Date: Fri, 29 Sep 2023 15:24:11 +0900 Subject: [PATCH 1/3] Add timeout to OpenAI API --- dist/index.js | 2 ++ src/ai.ts | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index 402107f..90e893a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11408,6 +11408,8 @@ async function completionRequest(apiKey, systemPrompt, userPrompt) { { role: 'user', content: userPrompt } ], model: 'gpt-3.5-turbo' + }, { + timeout: 60 * 1000 // 60s }); if (response.choices && response.choices[0] && diff --git a/src/ai.ts b/src/ai.ts index 1699ffb..c4692b1 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -7,13 +7,18 @@ export async function completionRequest( ): Promise { try { const openai = new OpenAI({ apiKey }) - const response = await openai.chat.completions.create({ - messages: [ - { role: 'system', content: systemPrompt }, - { role: 'user', content: userPrompt } - ], - model: 'gpt-3.5-turbo' - }) + const response = await openai.chat.completions.create( + { + messages: [ + { role: 'system', content: systemPrompt }, + { role: 'user', content: userPrompt } + ], + model: 'gpt-3.5-turbo' + }, + { + timeout: 60 * 1000 // 60s + } + ) if ( response.choices && response.choices[0] && From 20877aaa373667745d869e58ef163c5f92c48e20 Mon Sep 17 00:00:00 2001 From: uncle Date: Fri, 29 Sep 2023 15:29:46 +0900 Subject: [PATCH 2/3] Fix timeout values --- src/ai.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ai.ts b/src/ai.ts index c4692b1..f9a4538 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -7,6 +7,7 @@ export async function completionRequest( ): Promise { try { const openai = new OpenAI({ apiKey }) + const timeout = 300 * 1000 // 5 minutes const response = await openai.chat.completions.create( { messages: [ @@ -16,7 +17,7 @@ export async function completionRequest( model: 'gpt-3.5-turbo' }, { - timeout: 60 * 1000 // 60s + timeout: timeout } ) if ( From 000b500f2fb37afb30fa6c8161c4f3b2c272c08b Mon Sep 17 00:00:00 2001 From: uncle Date: Fri, 29 Sep 2023 15:34:09 +0900 Subject: [PATCH 3/3] fix timeout valiable name --- badges/coverage.svg | 2 +- dist/index.js | 3 ++- src/ai.ts | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/badges/coverage.svg b/badges/coverage.svg index 87a9754..3223905 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 95.23%Coverage95.23% \ No newline at end of file +Coverage: 95.31%Coverage95.31% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 90e893a..5883345 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11402,6 +11402,7 @@ const openai_1 = __importDefault(__nccwpck_require__(47)); async function completionRequest(apiKey, systemPrompt, userPrompt) { try { const openai = new openai_1.default({ apiKey }); + const requestTimeout = 300 * 1000; // 5 minutes const response = await openai.chat.completions.create({ messages: [ { role: 'system', content: systemPrompt }, @@ -11409,7 +11410,7 @@ async function completionRequest(apiKey, systemPrompt, userPrompt) { ], model: 'gpt-3.5-turbo' }, { - timeout: 60 * 1000 // 60s + timeout: requestTimeout }); if (response.choices && response.choices[0] && diff --git a/src/ai.ts b/src/ai.ts index f9a4538..0afe676 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -7,7 +7,7 @@ export async function completionRequest( ): Promise { try { const openai = new OpenAI({ apiKey }) - const timeout = 300 * 1000 // 5 minutes + const requestTimeout = 300 * 1000 // 5 minutes const response = await openai.chat.completions.create( { messages: [ @@ -17,7 +17,7 @@ export async function completionRequest( model: 'gpt-3.5-turbo' }, { - timeout: timeout + timeout: requestTimeout } ) if (