HerzaJ commited on
Commit
d4f970e
·
verified ·
1 Parent(s): 30991e5

Create gpt-4-turbo.js

Browse files
Files changed (1) hide show
  1. plugins/gpt-4-turbo.js +123 -0
plugins/gpt-4-turbo.js ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+ const fs = require('fs');
3
+ const puppeteer = require('puppeteer');
4
+
5
+ async function gpt4Skywork(question, chatId = null, imagePath = null) {
6
+ const isNewChat = !chatId;
7
+ const targetUrl = isNewChat
8
+ ? 'https://tool-skywork.wemiaow.com'
9
+ : `https://tool-skywork.wemiaow.com/conversation/${chatId}`;
10
+
11
+ let imageBase64 = null;
12
+ if (imagePath && fs.existsSync(imagePath)) {
13
+ const imageBuffer = fs.readFileSync(imagePath);
14
+ imageBase64 = imageBuffer.toString('base64');
15
+ }
16
+
17
+ const browser = await puppeteer.launch({
18
+ headless: true,
19
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
20
+ });
21
+
22
+ try {
23
+ const page = await browser.newPage();
24
+ await page.goto(targetUrl, { waitUntil: 'networkidle2' });
25
+ await page.waitForTimeout(2000);
26
+
27
+ if (imageBase64) {
28
+ const fileInput = await page.$('input[type="file"][accept="image/*"]');
29
+ const buffer = Buffer.from(imageBase64, 'base64');
30
+ await fileInput.uploadFile({
31
+ name: 'image.png',
32
+ type: 'image/png',
33
+ data: buffer
34
+ });
35
+ await page.waitForTimeout(3000);
36
+ }
37
+
38
+ await page.waitForSelector('textarea.scrollbar-custom', { timeout: 10000 });
39
+ await page.type('textarea.scrollbar-custom', question);
40
+ await page.waitForTimeout(500);
41
+
42
+ await page.click('button[type=submit][aria-label="Send message"]');
43
+
44
+ if (isNewChat) {
45
+ await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 });
46
+ }
47
+ await page.waitForTimeout(3000);
48
+
49
+ let isGenerating = true;
50
+ let attempts = 0;
51
+ while (isGenerating && attempts < 180) {
52
+ const stopBtn = await page.$('button.stop-generating-btn');
53
+ if (!stopBtn) {
54
+ isGenerating = false;
55
+ } else {
56
+ await page.waitForTimeout(1000);
57
+ attempts++;
58
+ }
59
+ }
60
+
61
+ await page.waitForTimeout(2000);
62
+ const responseDiv = await page.$('div.relative.flex.min-w-\\[60px\\]');
63
+ const responseText = await page.evaluate(el => el.innerText, responseDiv);
64
+
65
+ const currentUrl = page.url();
66
+ const chatIdMatch = currentUrl.match(/\/conversation\/([^/?]+)/);
67
+ const extractedChatId = chatIdMatch ? chatIdMatch[1] : null;
68
+
69
+ await browser.close();
70
+
71
+ return {
72
+ response: responseText,
73
+ chatId: extractedChatId,
74
+ url: currentUrl
75
+ };
76
+ } catch (error) {
77
+ await browser.close();
78
+ throw error;
79
+ }
80
+ }
81
+
82
+ const handler = async (req, res) => {
83
+ try {
84
+ const { text, chatId, image } = req.query;
85
+
86
+ if (!text) {
87
+ return res.status(400).json({
88
+ success: false,
89
+ error: 'Missing required parameter: text'
90
+ });
91
+ }
92
+
93
+ const result = await gpt4Skywork(text, chatId || null, image || null);
94
+
95
+ res.json({
96
+ author: "Herza",
97
+ success: true,
98
+ data: {
99
+ msg: result.response,
100
+ chatId: result.chatId
101
+ }
102
+ });
103
+
104
+ } catch (error) {
105
+ res.status(500).json({
106
+ success: false,
107
+ error: error.message
108
+ });
109
+ }
110
+ };
111
+
112
+ module.exports = {
113
+ name: 'ChatGPT V4 Turbo',
114
+ description: 'Generate responses using GPT-4 Turbo AI with image support',
115
+ type: 'GET',
116
+ routes: ['api/AI/gpt-4-turbo'],
117
+ tags: ['ai', 'openai', 'vision'],
118
+ main: ['AI'],
119
+ parameters: ['text', 'chatId', 'image'],
120
+ enabled: true,
121
+ limit: 5,
122
+ handler
123
+ };