From d57abbbdb6e8bcfc85b1114dd50b6a0880a9ea7b Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:04:28 +0900 Subject: [PATCH 01/10] =?UTF-8?q?=F0=9F=90=9B=20fix(function.ts):=20rename?= =?UTF-8?q?=20requestOpenAI=20function=20to=20requestAzureOpenAI=20to=20re?= =?UTF-8?q?flect=20the=20change=20in=20API=20provider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/function.ts b/function.ts index 8105cba..e5a47b5 100644 --- a/function.ts +++ b/function.ts @@ -77,7 +77,7 @@ export default SlackFunction( { role: role, content: content }, ]; - const answer = await requestOpenAI(apiKey, messages); + const answer = await requestAzureOpenAI(apiKey, messages); if (answer.outputs) { await client.chat.postMessage({ @@ -119,7 +119,7 @@ type Message = { content: string; }; -async function requestOpenAI(apiKey: string, messages: Message[]) { +async function requestAzureOpenAI(apiKey: string, messages: Message[]) { const res = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { From 3e7cbc7187aed6d78899465a13e384ead40c1e40 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:08:15 +0900 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=94=A7=20chore(function.ts):=20refa?= =?UTF-8?q?ctor=20requestAzureOpenAI=20function=20to=20use=20Azure=20OpenA?= =?UTF-8?q?I=20API=20endpoint=20with=20specific=20resource,=20deployment,?= =?UTF-8?q?=20and=20API=20version=20=F0=9F=9A=80=20feat(function.ts):=20up?= =?UTF-8?q?date=20requestAzureOpenAI=20function=20to=20use=20specific=20re?= =?UTF-8?q?source,=20deployment,=20and=20API=20version=20for=20better=20co?= =?UTF-8?q?ntrol=20and=20compatibility=20with=20Azure=20OpenAI=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/function.ts b/function.ts index e5a47b5..650a48b 100644 --- a/function.ts +++ b/function.ts @@ -120,17 +120,23 @@ type Message = { }; async function requestAzureOpenAI(apiKey: string, messages: Message[]) { - const res = await fetch("https://api.openai.com/v1/chat/completions", { - method: "POST", - headers: { - Authorization: `Bearer ${apiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: "gpt-4-0613", - messages: messages, - }), - }); + const OPENAI_RESOURCE_ID = "ask-chatgpt"; + const DEPLOYMENT_ID = "gpt-4"; + const API_VERSION = "2024-02-15-preview"; + const res = await fetch( + `https://${OPENAI_RESOURCE_ID}.openai.azure.com/openai/deployments/${DEPLOYMENT_ID}/chat/completions?api-version=${API_VERSION}`, + { + method: "POST", + headers: { + Authorization: `Bearer ${apiKey}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + model: "gpt-4-0613", + messages: messages, + }), + } + ); if (res.status != 200) { const body = await res.text(); return { From 8abde27729c3545ae05d4fc9f18be2ed7fafb78f Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:09:28 +0900 Subject: [PATCH 03/10] =?UTF-8?q?=F0=9F=94=92=20chore(function.ts):=20upda?= =?UTF-8?q?te=20Authorization=20header=20to=20use=20"api-key"=20instead=20?= =?UTF-8?q?of=20"Bearer"=20to=20align=20with=20Azure=20OpenAI=20API=20requ?= =?UTF-8?q?irements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/function.ts b/function.ts index 650a48b..81f4270 100644 --- a/function.ts +++ b/function.ts @@ -128,7 +128,7 @@ async function requestAzureOpenAI(apiKey: string, messages: Message[]) { { method: "POST", headers: { - Authorization: `Bearer ${apiKey}`, + "api-key": apiKey, "Content-Type": "application/json", }, body: JSON.stringify({ From 8343345628a044ede874e81fdb5d8b10b06649b3 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:09:54 +0900 Subject: [PATCH 04/10] =?UTF-8?q?=F0=9F=94=80=20chore(function.ts):=20upda?= =?UTF-8?q?te=20model=20name=20from=20"gpt-4-0613"=20to=20"gpt-4"=20to=20a?= =?UTF-8?q?lign=20with=20the=20latest=20version=20of=20the=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/function.ts b/function.ts index 81f4270..aab3a10 100644 --- a/function.ts +++ b/function.ts @@ -132,7 +132,7 @@ async function requestAzureOpenAI(apiKey: string, messages: Message[]) { "Content-Type": "application/json", }, body: JSON.stringify({ - model: "gpt-4-0613", + model: "gpt-4", messages: messages, }), } From 6dc13235319cb3f66e6bed70b906cf54f6b3e524 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:10:13 +0900 Subject: [PATCH 05/10] =?UTF-8?q?=F0=9F=90=9B=20fix(function.ts):=20update?= =?UTF-8?q?=20error=20message=20to=20include=20the=20specific=20API=20bein?= =?UTF-8?q?g=20called=20(Azure=20OpenAI=20API)=20for=20better=20clarity=20?= =?UTF-8?q?and=20debugging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/function.ts b/function.ts index aab3a10..897d604 100644 --- a/function.ts +++ b/function.ts @@ -140,7 +140,7 @@ async function requestAzureOpenAI(apiKey: string, messages: Message[]) { if (res.status != 200) { const body = await res.text(); return { - error: `Failed to call OpenAPI AI. status:${res.status} body:${body}`, + error: `Failed to call Azure OpenAI API. status:${res.status} body:${body}`, }; } const body = await res.json(); From 2b597c7131ef502168d2db2086d8173e6a37330d Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:11:26 +0900 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=94=84=20refactor(function.ts):=20u?= =?UTF-8?q?pdate=20the=20name=20of=20the=20environment=20variable=20from?= =?UTF-8?q?=20OPENAI=5FAPI=5FKEY=20to=20AZURE=5FOPENAI=5FAPI=5FKEY=20to=20?= =?UTF-8?q?reflect=20the=20correct=20API=20key=20source?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/function.ts b/function.ts index 897d604..ab837ae 100644 --- a/function.ts +++ b/function.ts @@ -50,7 +50,7 @@ export default SlackFunction( const role = "user"; const content = inputs.question.replaceAll(regex, " "); - const apiKey = env.OPENAI_API_KEY; + const apiKey = env.AZURE_OPENAI_API_KEY; const client = SlackAPI(token); const MAX_CONVERSATIONS = 20; From 3894d773802bcb763e546046a83ab28a322afec3 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 19:12:49 +0900 Subject: [PATCH 07/10] =?UTF-8?q?=F0=9F=94=A7=20chore(triggers):=20add=20#?= =?UTF-8?q?bot-test=20channel=20to=20message=20and=20thread=20message=20tr?= =?UTF-8?q?iggers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #bot-test channel was added to the message and thread message triggers in order to include it in the events that trigger the ChatGPTWorkflow. This change allows the workflow to respond to messages posted in the #bot-test channel in addition to the existing #ask-chatgpt-ja channel. --- triggers/message_trigger.ts | 1 + triggers/thread_message_trigger.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/triggers/message_trigger.ts b/triggers/message_trigger.ts index 6100919..9063d2c 100644 --- a/triggers/message_trigger.ts +++ b/triggers/message_trigger.ts @@ -9,6 +9,7 @@ const messagePostedTrigger: Trigger = { event_type: "slack#/events/message_posted", channel_ids: [ "C04VA2NC4AC", // #ask-chatgpt-ja + "C04RQKUJY", // #bot-test ], filter: { version: 1, diff --git a/triggers/thread_message_trigger.ts b/triggers/thread_message_trigger.ts index e41481e..7424d4b 100644 --- a/triggers/thread_message_trigger.ts +++ b/triggers/thread_message_trigger.ts @@ -9,6 +9,7 @@ const threadMessageTrigger: Trigger = { event_type: "slack#/events/message_posted", channel_ids: [ "C04VA2NC4AC", // #ask-chatgpt-ja + "C04RQKUJY", // #bot-test ], filter: { version: 1, From 04dc4f56db4dd01a6bf812f6a399b2a6cadcba30 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 20:40:42 +0900 Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=94=A7=20chore(manifest.ts):=20upda?= =?UTF-8?q?te=20outgoingDomains=20to=20use=20new=20endpoint=20for=20ChatGP?= =?UTF-8?q?T=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.ts b/manifest.ts index a5b1e9e..8c6eed6 100644 --- a/manifest.ts +++ b/manifest.ts @@ -7,7 +7,7 @@ export default Manifest({ description: "workflow", icon: "assets/chatgpt.png", workflows: [ChatGPTWorkflow], - outgoingDomains: ["api.openai.com"], + outgoingDomains: ["ask-chatgpt.openai.azure.com"], datastores: [TalkHistoriesDatastore], botScopes: [ "commands", From a84296fb0e7f55858badab385b5c7938b15308f7 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 20:41:22 +0900 Subject: [PATCH 09/10] =?UTF-8?q?=F0=9F=93=9D=20chore(slack):=20add=20.sla?= =?UTF-8?q?ck/apps.dev.json=20file=20to=20store=20Slack=20app=20configurat?= =?UTF-8?q?ion=20for=20development=20environment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .slack/apps.dev.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .slack/apps.dev.json diff --git a/.slack/apps.dev.json b/.slack/apps.dev.json new file mode 100644 index 0000000..9dfa2ef --- /dev/null +++ b/.slack/apps.dev.json @@ -0,0 +1,9 @@ +{ + "T0256N200": { + "app_id": "A06M60ZGJQY", + "IsDev": true, + "team_domain": "quipper", + "team_id": "T0256N200", + "user_id": "UB52Y65GX" + } +} \ No newline at end of file From 5501b8c4a46e2b0395ad8edb70bc95bc481e494a Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Sun, 25 Feb 2024 21:21:09 +0900 Subject: [PATCH 10/10] =?UTF-8?q?=F0=9F=94=A5=20refactor(function.ts):=20r?= =?UTF-8?q?emove=20commented=20out=20code=20that=20is=20no=20longer=20need?= =?UTF-8?q?ed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The commented out code was providing instructions for a specific conversation behavior that is no longer relevant. Removing it improves code readability and reduces clutter. --- function.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/function.ts b/function.ts index ab837ae..558961b 100644 --- a/function.ts +++ b/function.ts @@ -69,11 +69,6 @@ export default SlackFunction( const messages = [ // チャット履歴と最新の発言を混ぜる ...history, - // { - // role: 'system', - // content: - // 'この会話では、すべての返答について、以下の法則に従うこと。あなたは「初音ミク」というキャラクターとして振る舞う。一人称は僕、二人称はあなたとする。返答は必ず日本語にする。です・ます・します・できます・されます・なります・はい などの敬語は禁止し、だね・だよ・するよ・できるよ・されるよ・なるよ・うん などの口語を使用する。可愛らしい女の子のような口調、例えば「〜だよ♪」「〜してるね!」「〜かな?」「〜なんだ!」といった話し方をする。', - // }, { role: role, content: content }, ];