From a34770538a976291215b51acd9215718cc39a1dc Mon Sep 17 00:00:00 2001 From: Bryan Xu <35763689+bryanpalau@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:41:26 +0800 Subject: [PATCH 1/2] Update index.js gpt-4o --- config/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/index.js b/config/index.js index e8fc6c13..c9828f04 100644 --- a/config/index.js +++ b/config/index.js @@ -35,7 +35,7 @@ const config = Object.freeze({ OPENAI_TIMEOUT: env.OPENAI_TIMEOUT || env.APP_API_TIMEOUT, OPENAI_API_KEY: env.OPENAI_API_KEY || null, OPENAI_BASE_URL: env.OPENAI_BASE_URL || 'https://api.openai.com', - OPENAI_COMPLETION_MODEL: env.OPENAI_COMPLETION_MODEL || 'gpt-3.5-turbo', + OPENAI_COMPLETION_MODEL: env.OPENAI_COMPLETION_MODEL || 'gpt-4o', OPENAI_COMPLETION_TEMPERATURE: Number(env.OPENAI_COMPLETION_TEMPERATURE) || 1, OPENAI_COMPLETION_MAX_TOKENS: Number(env.OPENAI_COMPLETION_MAX_TOKENS) || 64, OPENAI_COMPLETION_FREQUENCY_PENALTY: Number(env.OPENAI_COMPLETION_FREQUENCY_PENALTY) || 0, From fae644d2854d52085b5745188cd0ee7c04b85133 Mon Sep 17 00:00:00 2001 From: Bryan Xu <35763689+bryanpalau@users.noreply.github.com> Date: Sun, 20 Oct 2024 22:58:39 +0800 Subject: [PATCH 2/2] Update index.js --- api/index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/api/index.js b/api/index.js index 75da24b4..1214abf8 100644 --- a/api/index.js +++ b/api/index.js @@ -4,15 +4,62 @@ import config from '../config/index.js'; import { validateLineSignature } from '../middleware/index.js'; import storage from '../storage/index.js'; import { fetchVersion, getVersion } from '../utils/index.js'; +import openai from 'openai'; // Step 1: Import the OpenAI SDK const app = express(); +// Set OpenAI API key from environment variables +openai.apiKey = process.env.OPENAI_API_KEY; + app.use(express.json({ verify: (req, res, buf) => { req.rawBody = buf.toString(); }, })); +// Function to get a response from ChatGPT (OpenAI) +const getChatGPTResponse = async (userMessage) => { + const response = await openai.Completion.create({ + model: 'gpt-4', // You can switch this to any available model + prompt: userMessage, + max_tokens: 200, // Adjust as needed + }); + + return response.choices[0].text.trim(); +}; + +// Modified version of handleEvents function to process LINE messages and integrate with ChatGPT +const handleLineEvents = async (events) => { + for (const event of events) { + if (event.type === 'message' && event.message.type === 'text') { + const userMessage = event.message.text; + const replyToken = event.replyToken; + + // Step 2: Get response from ChatGPT (OpenAI) + const chatGPTResponse = await getChatGPTResponse(userMessage); + + // Step 3: Send reply to LINE user + await replyToLine(replyToken, chatGPTResponse); + } + } +}; + +// Function to reply to LINE +const replyToLine = async (replyToken, message) => { + const axios = require('axios'); + const lineEndpoint = 'https://api.line.me/v2/bot/message/reply'; + + await axios.post(lineEndpoint, { + replyToken: replyToken, + messages: [{ type: 'text', text: message }] + }, { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${process.env.LINE_CHANNEL_ACCESS_TOKEN}` + } + }); +}; + app.get('/', async (req, res) => { if (config.APP_URL) { res.redirect(config.APP_URL); @@ -23,15 +70,20 @@ app.get('/', async (req, res) => { res.status(200).send({ status: 'OK', currentVersion, latestVersion }); }); +// Main webhook endpoint for LINE messages app.post(config.APP_WEBHOOK_PATH, validateLineSignature, async (req, res) => { try { await storage.initialize(); - await handleEvents(req.body.events); + + // Step 4: Call handleLineEvents instead of original handleEvents + await handleLineEvents(req.body.events); + res.sendStatus(200); } catch (err) { console.error(err.message); res.sendStatus(500); } + if (config.APP_DEBUG) printPrompts(); });