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.
Merge pull request #1 from Kazuki-tam/feature/chatgpt
Feature/chatgpt
- Loading branch information
Showing
12 changed files
with
518 additions
and
92 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
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,22 @@ | ||
import { createCompletionByChatGpt } from "./openai/index.ts"; | ||
import type { ChatGptFunction } from "./types/openai.ts"; | ||
|
||
/** | ||
* main function | ||
*/ | ||
declare const global: { | ||
[x: string]: ChatGptFunction; | ||
}; | ||
|
||
function CHATGPT( | ||
prompt: string, | ||
system?: string, | ||
): string { | ||
const response = createCompletionByChatGpt( | ||
prompt, | ||
system, | ||
); | ||
return response; | ||
} | ||
|
||
global.CHATGPT = CHATGPT; |
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,42 @@ | ||
import type { ChatGptApiInfo } from "../../types/openai.ts"; | ||
import { | ||
ChatGptSystemSchema, | ||
} from "../../schemas/openaiSchema.ts"; | ||
import { httpRequestWithRetriesForChatGpt } from "./httpRequestWithRetriesForChatGpt.ts"; | ||
import { getPropertiesService } from "../../utils/getPropertiesService.ts"; | ||
|
||
/** | ||
* Create Text Completion with OpenAI ChatGPT | ||
* | ||
* @param {string} prompt Prompt | ||
* @param {string} system System Role | ||
* @param {number} maxTokens Max Tokens | ||
* @param {number} temperature Temperature | ||
* @return Response text returned by ChatGPT | ||
*/ | ||
const createCompletionByChatGpt = ( | ||
prompt: string, | ||
system?: string, | ||
) => { | ||
if (!prompt) { | ||
throw new Error("You have to input the prompt at the least."); | ||
} | ||
|
||
if (system) { | ||
ChatGptSystemSchema.parse(system); | ||
} | ||
|
||
const OPENAI_API_KEY: string = getPropertiesService("OPENAI_API_KEY"); | ||
const response = httpRequestWithRetriesForChatGpt( | ||
OPENAI_API_KEY, | ||
prompt, | ||
system, | ||
); | ||
if (!response) { | ||
throw new Error("Error: Response error."); | ||
} | ||
const parsedRes = JSON.parse(response.getContentText()) as ChatGptApiInfo; | ||
return parsedRes.choices[0].message.content.trim(); | ||
}; | ||
|
||
export { createCompletionByChatGpt }; |
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,61 @@ | ||
import { sleepForBackoff } from "../../utils/sleepForBackoff.ts"; | ||
|
||
/** | ||
* Execute HTTP requests and retry failed requests. | ||
* @param {string} OPENAI_API_KEY OpenAI API key | ||
* @param {string} prompt Prompt | ||
* @return Response returned by ChatGPT | ||
*/ | ||
|
||
const httpRequestWithRetriesForChatGpt = ( | ||
OPENAI_API_KEY: string, | ||
prompt: string, | ||
system: string | undefined, | ||
) => { | ||
const url = "https://api.openai.com/v1/chat/completions"; | ||
const messageArray = [ | ||
{ | ||
role: "user", | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
if (system) { | ||
const systemItem = { | ||
role: "system", | ||
content: system, | ||
}; | ||
messageArray.unshift(systemItem); | ||
} | ||
|
||
const payload = { | ||
model: "gpt-3.5-turbo", | ||
messages: messageArray, | ||
}; | ||
|
||
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 { httpRequestWithRetriesForChatGpt }; |
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
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,4 +1,4 @@ | ||
import { createCompletionByGpt3 } from "./createCompletionByGpt3.ts"; | ||
import { httpRequestWithRetries } from "./httpRequestWithRetries.ts"; | ||
import { createCompletionByGpt3 } from "./gpt/createCompletionByGpt3.ts"; | ||
import { createCompletionByChatGpt } from "./chatgpt/createCompletionByChatGpt.ts"; | ||
|
||
export { createCompletionByGpt3, httpRequestWithRetries }; | ||
export { createCompletionByChatGpt, createCompletionByGpt3 }; |
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