Spaces:
Sleeping
Sleeping
Update plugins/qwen.js
Browse files- plugins/qwen.js +23 -240
plugins/qwen.js
CHANGED
|
@@ -1,274 +1,57 @@
|
|
| 1 |
-
const axios = require('axios');
|
| 2 |
-
|
| 3 |
-
class QwenChatScraper {
|
| 4 |
-
constructor(userId = null, cookies = '') {
|
| 5 |
-
this.baseURL = 'https://chat.qwen.ai/api';
|
| 6 |
-
this.userId = userId;
|
| 7 |
-
this.cookies = cookies;
|
| 8 |
-
|
| 9 |
-
this.client = axios.create({
|
| 10 |
-
baseURL: this.baseURL,
|
| 11 |
-
headers: {
|
| 12 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 13 |
-
'Accept': 'application/json',
|
| 14 |
-
'Content-Type': 'application/json',
|
| 15 |
-
'Origin': 'https://chat.qwen.ai',
|
| 16 |
-
'Referer': 'https://chat.qwen.ai/',
|
| 17 |
-
'Cookie': cookies
|
| 18 |
-
}
|
| 19 |
-
});
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
async login(email, password) {
|
| 23 |
-
try {
|
| 24 |
-
await this.client.get('/config');
|
| 25 |
-
|
| 26 |
-
const loginResponse = await this.client.post('/v1/login', {
|
| 27 |
-
email: email,
|
| 28 |
-
password: password
|
| 29 |
-
});
|
| 30 |
-
|
| 31 |
-
if (loginResponse.data && loginResponse.data.success) {
|
| 32 |
-
const setCookieHeaders = loginResponse.headers['set-cookie'] || [];
|
| 33 |
-
this.cookies = this.parseCookies(setCookieHeaders);
|
| 34 |
-
|
| 35 |
-
this.client.defaults.headers['Cookie'] = this.cookies;
|
| 36 |
-
|
| 37 |
-
const authResponse = await this.client.get('/v1/auths/');
|
| 38 |
-
|
| 39 |
-
if (authResponse.data && authResponse.data.id) {
|
| 40 |
-
this.userId = authResponse.data.id;
|
| 41 |
-
|
| 42 |
-
return {
|
| 43 |
-
success: true,
|
| 44 |
-
userId: this.userId,
|
| 45 |
-
email: authResponse.data.email,
|
| 46 |
-
name: authResponse.data.name,
|
| 47 |
-
cookies: this.cookies
|
| 48 |
-
};
|
| 49 |
-
}
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
return {
|
| 53 |
-
success: false,
|
| 54 |
-
message: 'Login failed: Invalid credentials'
|
| 55 |
-
};
|
| 56 |
-
|
| 57 |
-
} catch (error) {
|
| 58 |
-
return {
|
| 59 |
-
success: false,
|
| 60 |
-
message: `Login error: ${error.response?.data?.message || error.message}`
|
| 61 |
-
};
|
| 62 |
-
}
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
parseCookies(setCookieHeaders) {
|
| 66 |
-
const cookies = [];
|
| 67 |
-
|
| 68 |
-
for (const header of setCookieHeaders) {
|
| 69 |
-
const cookieParts = header.split(';')[0];
|
| 70 |
-
cookies.push(cookieParts);
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
return cookies.join('; ');
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
async createChat(title = 'New Chat', model = 'qwen3-max') {
|
| 77 |
-
const response = await this.client.post('/v2/chats/new', {
|
| 78 |
-
title,
|
| 79 |
-
models: [model],
|
| 80 |
-
chat_mode: 'normal',
|
| 81 |
-
chat_type: 't2t',
|
| 82 |
-
timestamp: Date.now()
|
| 83 |
-
});
|
| 84 |
-
return response.data.data;
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
async sendMessage(chatId, message, model = 'qwen3-max') {
|
| 88 |
-
const messageId = this.generateUUID();
|
| 89 |
-
const timestamp = Date.now();
|
| 90 |
-
|
| 91 |
-
const response = await this.client.post(
|
| 92 |
-
`/v2/chat/completions?chat_id=${chatId}`,
|
| 93 |
-
{
|
| 94 |
-
stream: true,
|
| 95 |
-
incremental_output: true,
|
| 96 |
-
chat_id: chatId,
|
| 97 |
-
chat_mode: 'normal',
|
| 98 |
-
model: model,
|
| 99 |
-
parent_id: null,
|
| 100 |
-
messages: [{
|
| 101 |
-
fid: messageId,
|
| 102 |
-
parentId: null,
|
| 103 |
-
childrenIds: [],
|
| 104 |
-
role: 'user',
|
| 105 |
-
content: message,
|
| 106 |
-
user_action: 'chat',
|
| 107 |
-
files: [],
|
| 108 |
-
timestamp: timestamp,
|
| 109 |
-
models: [model],
|
| 110 |
-
chat_type: 't2t',
|
| 111 |
-
feature_config: {
|
| 112 |
-
thinking_enabled: false,
|
| 113 |
-
output_schema: 'phase'
|
| 114 |
-
},
|
| 115 |
-
extra: {
|
| 116 |
-
meta: {
|
| 117 |
-
subChatType: 't2t'
|
| 118 |
-
}
|
| 119 |
-
},
|
| 120 |
-
sub_chat_type: 't2t',
|
| 121 |
-
parent_id: null
|
| 122 |
-
}],
|
| 123 |
-
timestamp: timestamp
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
responseType: 'stream',
|
| 127 |
-
headers: {
|
| 128 |
-
'Accept': 'text/event-stream'
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
-
);
|
| 132 |
-
|
| 133 |
-
return this.collectStream(response.data);
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
async collectStream(stream) {
|
| 137 |
-
let buffer = '';
|
| 138 |
-
let fullContent = '';
|
| 139 |
-
let responseData = {
|
| 140 |
-
chat_id: null,
|
| 141 |
-
parent_id: null,
|
| 142 |
-
response_id: null,
|
| 143 |
-
content: '',
|
| 144 |
-
usage: null,
|
| 145 |
-
status: 'typing'
|
| 146 |
-
};
|
| 147 |
-
|
| 148 |
-
for await (const chunk of stream) {
|
| 149 |
-
buffer += chunk.toString();
|
| 150 |
-
const lines = buffer.split('\n');
|
| 151 |
-
buffer = lines.pop();
|
| 152 |
-
|
| 153 |
-
for (const line of lines) {
|
| 154 |
-
if (line.startsWith('data: ')) {
|
| 155 |
-
try {
|
| 156 |
-
const jsonStr = line.substring(6);
|
| 157 |
-
const data = JSON.parse(jsonStr);
|
| 158 |
-
|
| 159 |
-
if (data['response.created']) {
|
| 160 |
-
responseData.chat_id = data['response.created'].chat_id;
|
| 161 |
-
responseData.parent_id = data['response.created'].parent_id;
|
| 162 |
-
responseData.response_id = data['response.created'].response_id;
|
| 163 |
-
}
|
| 164 |
-
|
| 165 |
-
if (data.choices && data.choices[0]) {
|
| 166 |
-
const delta = data.choices[0].delta;
|
| 167 |
-
|
| 168 |
-
if (delta.content) {
|
| 169 |
-
fullContent += delta.content;
|
| 170 |
-
responseData.content = fullContent;
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
if (delta.status) {
|
| 174 |
-
responseData.status = delta.status;
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
if (data.usage) {
|
| 178 |
-
responseData.usage = data.usage;
|
| 179 |
-
}
|
| 180 |
-
}
|
| 181 |
-
} catch (e) {}
|
| 182 |
-
}
|
| 183 |
-
}
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
return {
|
| 187 |
-
chatId: responseData.chat_id,
|
| 188 |
-
responseId: responseData.response_id,
|
| 189 |
-
content: fullContent,
|
| 190 |
-
usage: responseData.usage,
|
| 191 |
-
status: 'finished'
|
| 192 |
-
};
|
| 193 |
-
}
|
| 194 |
-
|
| 195 |
-
generateUUID() {
|
| 196 |
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
| 197 |
-
const r = Math.random() * 16 | 0;
|
| 198 |
-
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
| 199 |
-
return v.toString(16);
|
| 200 |
-
});
|
| 201 |
-
}
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
const defaultCookies = '_c_WBKFRo=ggfUx8IeZNjRUHiHvEW8mvqCPJ5QqXCw3gxOxSEC; _gcl_au=1.1.671306628.1754633897.966854012.1761357423.1761359270; atpsida=d16698b082b415e3ce98462f_1761359275_3; aui=0033f46d-cdc9-44b9-aa02-a2f07588bdfd; cna=roQcIXSjlkcCAXIKh7pG5R+v; cnaui=0033f46d-cdc9-44b9-aa02-a2f07588bdfd; isg=BP39jyHRSeCe1e1luUJvtbf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HAiOjE3NjE5NjQwNzR9.PbRw0I_udl4LzYsham38oHN5KyhyCgIRn7ryAPZUXGo; xlly_s=1; _bl_uid=Lkmw8e752FgfpXqta4myh1txn7OF; acw_tc=0a03e58917613592701907846e3bc0bf8c460fdfd424ec929f27d30faea69c; x-ap=ap-southeast-5';
|
| 205 |
-
|
| 206 |
const handler = async (req, res) => {
|
| 207 |
try {
|
| 208 |
-
|
| 209 |
-
return res.status(405).json({
|
| 210 |
-
success: false,
|
| 211 |
-
error: 'Method Not Allowed. Use GET method'
|
| 212 |
-
});
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
const { prompt, model = 'qwen3-max', key } = req.query;
|
| 216 |
|
| 217 |
-
if (!
|
| 218 |
return res.status(400).json({
|
| 219 |
success: false,
|
| 220 |
-
error: 'Missing required parameter:
|
| 221 |
});
|
| 222 |
}
|
| 223 |
|
| 224 |
-
const
|
| 225 |
|
| 226 |
-
if (!
|
| 227 |
return res.status(500).json({
|
| 228 |
success: false,
|
| 229 |
-
error: 'QWEN
|
| 230 |
});
|
| 231 |
}
|
| 232 |
|
| 233 |
-
const
|
| 234 |
-
|
| 235 |
-
const
|
| 236 |
-
const result = await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
res.json({
|
| 239 |
author: "Herza",
|
| 240 |
success: true,
|
| 241 |
-
|
| 242 |
-
model: model,
|
| 243 |
-
response: result.content,
|
| 244 |
-
usage: result.usage
|
| 245 |
});
|
| 246 |
|
| 247 |
} catch (error) {
|
| 248 |
-
|
| 249 |
-
? (typeof error.response.data === 'string'
|
| 250 |
-
? 'Invalid cookies or authentication failed'
|
| 251 |
-
: error.response.data.message || error.message)
|
| 252 |
-
: error.message;
|
| 253 |
-
|
| 254 |
-
res.status(error.response?.status || 500).json({
|
| 255 |
success: false,
|
| 256 |
-
error:
|
| 257 |
-
details: error.response?.status === 401 || error.response?.status === 403
|
| 258 |
-
? 'Cookies invalid or expired. Please get new cookies.'
|
| 259 |
-
: undefined
|
| 260 |
});
|
| 261 |
}
|
| 262 |
};
|
| 263 |
|
| 264 |
module.exports = {
|
| 265 |
-
name: 'Qwen
|
| 266 |
description: 'Generate responses using Qwen AI',
|
| 267 |
type: 'GET',
|
| 268 |
routes: ['api/AI/qwen'],
|
| 269 |
-
tags: ['ai', 'qwen'
|
| 270 |
main: ['AI'],
|
| 271 |
-
parameters: ['
|
| 272 |
enabled: true,
|
| 273 |
handler
|
| 274 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const handler = async (req, res) => {
|
| 2 |
try {
|
| 3 |
+
const { text, model = 'qwen3-max' } = req.query;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
if (!text) {
|
| 6 |
return res.status(400).json({
|
| 7 |
success: false,
|
| 8 |
+
error: 'Missing required parameter: text'
|
| 9 |
});
|
| 10 |
}
|
| 11 |
|
| 12 |
+
const apiKey = process.env.QWEN;
|
| 13 |
|
| 14 |
+
if (!apiKey) {
|
| 15 |
return res.status(500).json({
|
| 16 |
success: false,
|
| 17 |
+
error: 'QWEN API key not configured'
|
| 18 |
});
|
| 19 |
}
|
| 20 |
|
| 21 |
+
const url = `https://qwen-azure.vercel.app/api/qwen?text=${encodeURIComponent(text)}&model=${model}&key=${apiKey}`;
|
| 22 |
+
|
| 23 |
+
const response = await fetch(url);
|
| 24 |
+
const result = await response.json();
|
| 25 |
+
|
| 26 |
+
if (!response.ok || !result.success) {
|
| 27 |
+
return res.status(response.status || 500).json({
|
| 28 |
+
success: false,
|
| 29 |
+
error: result.error || 'Failed to get response from Qwen API'
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
|
| 33 |
res.json({
|
| 34 |
author: "Herza",
|
| 35 |
success: true,
|
| 36 |
+
data: result.data
|
|
|
|
|
|
|
|
|
|
| 37 |
});
|
| 38 |
|
| 39 |
} catch (error) {
|
| 40 |
+
res.status(500).json({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
success: false,
|
| 42 |
+
error: error.message
|
|
|
|
|
|
|
|
|
|
| 43 |
});
|
| 44 |
}
|
| 45 |
};
|
| 46 |
|
| 47 |
module.exports = {
|
| 48 |
+
name: 'Qwen AI',
|
| 49 |
description: 'Generate responses using Qwen AI',
|
| 50 |
type: 'GET',
|
| 51 |
routes: ['api/AI/qwen'],
|
| 52 |
+
tags: ['ai', 'qwen'],
|
| 53 |
main: ['AI'],
|
| 54 |
+
parameters: ['text', 'model', 'key'],
|
| 55 |
enabled: true,
|
| 56 |
handler
|
| 57 |
};
|