generated from Kazuki-tam/gas-deno-esbuild-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70162c9
commit aa16528
Showing
5 changed files
with
105 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { sleepForBackoff } from "../utils/sleepForBackoff.ts"; | ||
|
||
/** | ||
* Execute HTTP requests and retry failed requests. | ||
* @param {string | string[]} prompt Prompt | ||
* @param {string} OPENAI_API_KEY OpenAI API key | ||
* @param {string} model Model | ||
* @param {number} maxTokens Max Tokens | ||
* @param {number} temperature Temperature | ||
* @return Response returned by GPT-3 | ||
*/ | ||
|
||
const httpRequestWithRetries = ( | ||
OPENAI_API_KEY: string, | ||
prompt: string | string[], | ||
model: string, | ||
maxTokens: number, | ||
temperature: number, | ||
) => { | ||
const url = "https://api.openai.com/v1/completions"; | ||
const payload = { | ||
model: model, | ||
prompt: prompt, | ||
suffix: null, | ||
temperature: temperature, | ||
max_tokens: maxTokens, | ||
top_p: 1, | ||
}; | ||
|
||
const fetchOptions = { | ||
contentType: "application/json", | ||
headers: { Authorization: "Bearer " + OPENAI_API_KEY }, | ||
muteHttpExceptions: true, | ||
payload: JSON.stringify(payload), | ||
}; | ||
|
||
let response = null; | ||
for (let numRetries = 0; numRetries < 5; numRetries++) { | ||
const lastRequestTime = Date.now(); | ||
try { | ||
Logger.log(`Sending HTTP request to ${url}`); | ||
response = UrlFetchApp.fetch(url, fetchOptions); | ||
const responseCode = response.getResponseCode(); | ||
if (responseCode !== 429 && responseCode < 500) { | ||
return response; | ||
} | ||
} catch (error) { | ||
throw new Error(`Error: ${error}`); | ||
} | ||
Logger.log(`Retrying after ${numRetries} failed requests.`); | ||
sleepForBackoff(numRetries, lastRequestTime); | ||
} | ||
return response; | ||
}; | ||
export { httpRequestWithRetries }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { createCompletionByGpt3 } from "./createCompletionByGpt3.ts"; | ||
import { httpRequestWithRetries } from "./httpRequestWithRetries.ts"; | ||
|
||
export { createCompletionByGpt3 }; | ||
export { createCompletionByGpt3, httpRequestWithRetries }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Gets a property store that all users can access, but only within this script. | ||
* @param {string} propertyName The Properties service | ||
* @return The property value | ||
* https://developers.google.com/apps-script/reference/properties/properties-service | ||
*/ | ||
|
||
const getPropertiesService = (propertyName: string) => { | ||
const propertyValue: string | null = PropertiesService.getScriptProperties() | ||
.getProperty(propertyName); | ||
if (!propertyValue) { | ||
throw new Error(`Error: ${propertyName} is not defined.`); | ||
} | ||
return propertyValue; | ||
}; | ||
|
||
export { getPropertiesService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Sleep after failed requests. | ||
* @param {number} numRetries The number to retry | ||
* @param {number} lastRequestTime The last request time | ||
*/ | ||
const sleepForBackoff = (numRetries: number, lastRequestTime: number): void => { | ||
const backoff = Math.min(1000 * (1.6 ** numRetries), 60000); | ||
const jitter = 1 + 0.23 * (2 * Math.random() - 1); // Random value in [0.77 1.23] | ||
const sleepTime = Date.now() - lastRequestTime + backoff * jitter; | ||
Utilities.sleep(sleepTime); | ||
}; | ||
|
||
export { sleepForBackoff }; |