Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bryanpalau patch 1 #349

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down