Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 0725526

Browse files
authored
add retries (#60)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI **Release Notes** This pull request adds retry functionality to improve the reliability of sending messages using the OpenAI API. It includes a new function called `retry` in the `src/utils.ts` file and modifies the `src/bot.ts` file to use this function for retrying message sending in case of failure. This is a "New Feature" that enhances the user experience by reducing the likelihood of failed message sending. <!-- end of auto-generated comment: release notes by openai -->
1 parent 980f768 commit 0725526

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

dist/index.js

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bot.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './fetch-polyfill.js'
33
import * as core from '@actions/core'
44
import * as openai from 'chatgpt'
55
import * as optionsJs from './options.js'
6+
import * as utils from './utils.js'
67

78
// define type to save parentMessageId and conversationId
89
export type Ids = {
@@ -25,8 +26,6 @@ export class Bot {
2526
completionParams: {
2627
temperature: options.temperature
2728
}
28-
// assistantLabel: " ",
29-
// userLabel: " ",
3029
})
3130
} else {
3231
const err =
@@ -58,23 +57,32 @@ export class Bot {
5857
}
5958

6059
let response: openai.ChatMessage | null = null
60+
6161
if (this.api) {
6262
const opts: openai.SendMessageOptions = {
63-
timeoutMs: 90000
63+
timeoutMs: 60000
6464
}
6565
if (ids.parentMessageId) {
6666
opts.parentMessageId = ids.parentMessageId
6767
}
6868
try {
69-
response = await this.api.sendMessage(message, opts)
70-
const end = Date.now()
71-
core.info(`response: ${JSON.stringify(response)}`)
72-
core.info(`openai response time: ${end - start} ms`)
69+
response = await utils.retry(
70+
this.api.sendMessage.bind(this.api),
71+
[message, opts],
72+
3
73+
)
7374
} catch (e: any) {
7475
core.info(
7576
`response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}`
7677
)
7778
}
79+
const end = Date.now()
80+
core.info(`response: ${JSON.stringify(response)}`)
81+
core.info(
82+
`openai sendMessage (including retries) response time: ${
83+
end - start
84+
} ms`
85+
)
7886
} else {
7987
core.setFailed('The OpenAI API is not initialized')
8088
}

src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as core from '@actions/core'
2+
3+
export const retry = async (
4+
fn: Function,
5+
args: any[],
6+
times: number
7+
): Promise<any> => {
8+
for (let i = 0; i < times; i++) {
9+
try {
10+
return await fn(...args)
11+
} catch (error) {
12+
if (i === times - 1) {
13+
throw error
14+
}
15+
core.warning(`Function failed on try ${i + 1}, retrying...`)
16+
continue
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)