From 30b4da79e07a163acad45650a3b08035597342bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Thu, 1 Aug 2024 15:05:36 +0900 Subject: [PATCH] fix(ci): build --- package.json | 2 +- src/helpers/chat-gpt.ts | 57 +++++++++++++++++++++++++++++++++++++++++ src/helpers/jwt.ts | 2 +- src/worker.ts | 2 +- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/helpers/chat-gpt.ts diff --git a/package.json b/package.json index bedc734..ca56e01 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "deploy": "wrangler deploy", "start": "wrangler dev", "setup-key": "tsx ./deploy-keys.ts", - "build": "tsx build/esbuild-build.ts", + "build": "tsc --noEmit", "format": "run-s format:lint format:prettier format:cspell", "format:lint": "eslint --fix .", "format:prettier": "prettier --write .", diff --git a/src/helpers/chat-gpt.ts b/src/helpers/chat-gpt.ts new file mode 100644 index 0000000..b2d4838 --- /dev/null +++ b/src/helpers/chat-gpt.ts @@ -0,0 +1,57 @@ +import { ErrorType } from "../types/basic"; +import { PROMPT_SYSTEM, PROMPT_USER } from "./prompt"; +import { removeNewlinesAndExtractValues } from "./utils"; + +export async function completeGpt3(messageText: string) { + try { + const apiKey = OPENAI_API_KEY; + const apiUrl = "https://api.openai.com/v1/chat/completions"; + + const requestBody = { + model: "gpt-3.5-turbo", + messages: [ + { + role: "system", + content: PROMPT_SYSTEM, + }, + { + role: "user", + content: PROMPT_USER.replace(/{messageText}/g, messageText), + }, + ], + max_tokens: 1500, + temperature: 0, + stream: false, + }; + + const response = await fetch(apiUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify(requestBody), + }); + + const data = await response.json(); + + console.log(data); // log raw data + + if (data?.error) { + console.log(`ChatGPT Error: ${data?.error}`); + return; + } + + return removeNewlinesAndExtractValues(data.choices[0].message.content); + } catch (e) { + console.log((e as ErrorType).message); + return { + issueTitle: null, + timeEstimate: null, + }; + } +} + +export default { + completeGPT3: completeGpt3, +}; diff --git a/src/helpers/jwt.ts b/src/helpers/jwt.ts index b7f9d0b..50611b9 100644 --- a/src/helpers/jwt.ts +++ b/src/helpers/jwt.ts @@ -16,7 +16,7 @@ export async function validateJwt(token: string) { throw new Error("Invalid JWT"); } - const { payload } = jwt.decode(token); + const { payload } = jwt.decode(token) as { payload: JwtResponse }; if (!payload.group) { throw new Error("Invalid JWT, group is missing"); diff --git a/src/worker.ts b/src/worker.ts index 83b12ae..f27e49b 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -3,7 +3,7 @@ */ import { BOT_COMMANDS, ENABLE_TOPIC, GITHUB_PATHNAME } from "./constants"; -import { completeGpt3 } from "./helpers/chatgpt"; +import { completeGpt3 } from "./helpers/chat-gpt"; import { createIssue } from "./helpers/github"; import { onPrivateCallbackQuery } from "./helpers/navigation"; import { oAuthHandler } from "./helpers/oauth-login";