Skip to content

Commit

Permalink
fix: add retry for api3
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed May 14, 2024
1 parent a507c85 commit b76d33d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Empty file added client/OpenAILikeClient.js
Empty file.
30 changes: 19 additions & 11 deletions utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export class OfficialChatGPTClient {
this._apiReverseUrl = apiReverseUrl
}

async sendMessage (prompt, opts = {}) {
async sendMessage (prompt, opts = {}, retry = 3) {
if (retry < 0) {
throw new Error('retry limit exceeded')
}
let {
conversationId,
parentMessageId = uuidv4(),
Expand Down Expand Up @@ -143,17 +146,22 @@ export class OfficialChatGPTClient {
req.write(JSON.stringify(body))
req.end()
})
const response = await requestP
if (statusCode === 200) {
return {
text: response.response,
conversationId: response.conversationId,
id: response.messageId,
parentMessageId
try {
const response = await requestP
if (statusCode === 200) {
return {
text: response.response,
conversationId: response.conversationId,
id: response.messageId,
parentMessageId
}
} else {
console.log(response)
throw new Error(response)
}
} else {
console.log(response)
throw new Error(response)
} catch (err) {
logger.warn(err)
return await this.sendMessage(prompt, opts, retry - 1)
}
}
}

0 comments on commit b76d33d

Please sign in to comment.