use file to unlock ctx
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
const express = require("express");
|
||||
const https = require("https");
|
||||
const FormData = require("form-data");
|
||||
const { v4: uuidv4 } = require("uuid");
|
||||
const app = express();
|
||||
const axios = require("axios");
|
||||
const port = 8080;
|
||||
const YOUCOM_COOKIE = process.env.YOUCOM_COOKIE;
|
||||
|
||||
(axios.defaults.headers.common["User-Agent"] =
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"),
|
||||
(axios.defaults.headers.common["Cookie"] = process.env.YOUCOM_COOKIE);
|
||||
|
||||
app.post("/v1/messages", (req, res) => {
|
||||
req.rawBody = "";
|
||||
@@ -13,7 +17,7 @@ app.post("/v1/messages", (req, res) => {
|
||||
req.rawBody += chunk;
|
||||
});
|
||||
|
||||
req.on("end", function () {
|
||||
req.on("end", async () => {
|
||||
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
||||
try {
|
||||
let jsonBody = JSON.parse(req.rawBody);
|
||||
@@ -41,7 +45,7 @@ app.post("/v1/messages", (req, res) => {
|
||||
})
|
||||
);
|
||||
} else if (jsonBody.stream == true) {
|
||||
// squash messages
|
||||
// 计算用户消息长度
|
||||
let userMessage = [{ question: "", answer: "" }];
|
||||
let userQuery = "";
|
||||
let lastUpdate = true;
|
||||
@@ -67,11 +71,23 @@ app.post("/v1/messages", (req, res) => {
|
||||
}
|
||||
});
|
||||
userQuery = userMessage[userMessage.length - 1].question;
|
||||
// if (userMessage[userMessage.length - 1].answer == "") {
|
||||
// userMessage.pop();
|
||||
// }
|
||||
if (userMessage[userMessage.length - 1].answer == "") {
|
||||
userMessage.pop();
|
||||
}
|
||||
console.log(userMessage);
|
||||
|
||||
// user message to plaintext
|
||||
let previousMessages = jsonBody.messages
|
||||
.map((msg) => {
|
||||
return `${msg.role}: ${msg.content}`;
|
||||
})
|
||||
.join("\n\n");
|
||||
|
||||
// 只保留最后一条用户消息
|
||||
if(userMessage.length > 1) {
|
||||
userMessage = userMessage.slice(-1);
|
||||
}
|
||||
|
||||
let msgid = uuidv4();
|
||||
|
||||
// send message start
|
||||
@@ -92,6 +108,25 @@ app.post("/v1/messages", (req, res) => {
|
||||
);
|
||||
res.write(createEvent("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
|
||||
res.write(createEvent("ping", { type: "ping" }));
|
||||
|
||||
// GET https://you.com/api/get_nonce to get nonce
|
||||
let nonce = await axios("https://you.com/api/get_nonce").then((res) => res.data);
|
||||
if (!nonce) throw new Error("Failed to get nonce");
|
||||
|
||||
// POST https://you.com/api/upload to upload user message
|
||||
const form_data = new FormData();
|
||||
messageBuffer = Buffer.from(previousMessages, "utf8");
|
||||
form_data.append("file", messageBuffer, { filename: "Previous_Conversation.txt", contentType: "text/plain" });
|
||||
let uploadedFile = await axios
|
||||
.post("https://you.com/api/upload", form_data, {
|
||||
headers: {
|
||||
...form_data.getHeaders(),
|
||||
"X-Upload-Nonce": nonce,
|
||||
},
|
||||
})
|
||||
.then((res) => res.data.filename);
|
||||
if (!uploadedFile) throw new Error("Failed to upload messages");
|
||||
|
||||
// proxy response
|
||||
let youcom_params = new URLSearchParams();
|
||||
youcom_params.append("page", "0");
|
||||
@@ -104,85 +139,87 @@ app.post("/v1/messages", (req, res) => {
|
||||
youcom_params.append("conversationTurnId", msgid);
|
||||
youcom_params.append("selectedAIModel", "claude_3_opus");
|
||||
youcom_params.append("selectedChatMode", "custom");
|
||||
youcom_params.append("pastChatLength", userMessage.length - 1);
|
||||
youcom_params.append("pastChatLength", "0");
|
||||
youcom_params.append("queryTraceId", msgid);
|
||||
youcom_params.append("use_personalization_extraction", "false");
|
||||
youcom_params.append("domain", "youchat");
|
||||
youcom_params.append("responseFilter", "");
|
||||
youcom_params.append("mkt", "zh-CN");
|
||||
youcom_params.append(
|
||||
"userFiles",
|
||||
JSON.stringify([
|
||||
{
|
||||
user_filename: "Previous_Conversation.txt",
|
||||
filename: uploadedFile,
|
||||
size: messageBuffer.length,
|
||||
},
|
||||
])
|
||||
);
|
||||
youcom_params.append("chat", JSON.stringify(userMessage));
|
||||
|
||||
var proxyReq = https.request(
|
||||
{
|
||||
hostname: "you.com",
|
||||
port: 443,
|
||||
method: "GET",
|
||||
path: "/api/streamingSearch?" + youcom_params.toString(),
|
||||
headers: {
|
||||
cookie: YOUCOM_COOKIE,
|
||||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
||||
accept: "text/event-stream",
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
var proxyReq = await axios({
|
||||
method: "GET",
|
||||
url: "https://you.com/api/streamingSearch?" + youcom_params.toString(),
|
||||
headers: {
|
||||
accept: "text/event-stream",
|
||||
},
|
||||
(proxyRes) => {
|
||||
let cachedLine = "";
|
||||
proxyRes.on("data", (chunk) => {
|
||||
// try to parse eventstream chunk
|
||||
chunk = chunk.toString();
|
||||
|
||||
if (cachedLine) {
|
||||
chunk = cachedLine + chunk;
|
||||
cachedLine = "";
|
||||
}
|
||||
|
||||
if (!chunk.endsWith("\n")) {
|
||||
const lines = chunk.split("\n");
|
||||
cachedLine = lines.pop();
|
||||
chunk = lines.join("\n");
|
||||
}
|
||||
|
||||
try {
|
||||
if (chunk.indexOf("event: youChatToken\n") != -1) {
|
||||
chunk.split("\n").forEach((line) => {
|
||||
if (line.startsWith(`data: {"youChatToken"`)) {
|
||||
let data = line.substring(6);
|
||||
let json = JSON.parse(data);
|
||||
//console.log(json);
|
||||
chunkJSON = JSON.stringify({
|
||||
type: "content_block_delta",
|
||||
index: 0,
|
||||
delta: { type: "text_delta", text: json.youChatToken },
|
||||
});
|
||||
res.write(createEvent("content_block_delta", chunkJSON));
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
proxyRes.on("end", () => {
|
||||
// send ending
|
||||
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
|
||||
res.write(
|
||||
createEvent("message_delta", {
|
||||
type: "message_delta",
|
||||
delta: { stop_reason: "end_turn", stop_sequence: null },
|
||||
usage: { output_tokens: 12 },
|
||||
})
|
||||
);
|
||||
res.write(createEvent("message_stop", { type: "message_stop" }));
|
||||
|
||||
res.end();
|
||||
});
|
||||
}
|
||||
);
|
||||
proxyReq.on("error", (e) => {
|
||||
responseType: "stream",
|
||||
}).catch((e) => {
|
||||
throw e;
|
||||
});
|
||||
//proxyReq.write(youcom_params.toString());
|
||||
proxyReq.end();
|
||||
|
||||
let cachedLine = "";
|
||||
const stream = proxyReq.data;
|
||||
stream.on("data", (chunk) => {
|
||||
// try to parse eventstream chunk
|
||||
chunk = chunk.toString();
|
||||
|
||||
if (cachedLine) {
|
||||
chunk = cachedLine + chunk;
|
||||
cachedLine = "";
|
||||
}
|
||||
|
||||
if (!chunk.endsWith("\n")) {
|
||||
const lines = chunk.split("\n");
|
||||
cachedLine = lines.pop();
|
||||
chunk = lines.join("\n");
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(chunk);
|
||||
if (chunk.indexOf("event: youChatToken\n") != -1) {
|
||||
chunk.split("\n").forEach((line) => {
|
||||
if (line.startsWith(`data: {"youChatToken"`)) {
|
||||
let data = line.substring(6);
|
||||
let json = JSON.parse(data);
|
||||
//console.log(json);
|
||||
chunkJSON = JSON.stringify({
|
||||
type: "content_block_delta",
|
||||
index: 0,
|
||||
delta: { type: "text_delta", text: json.youChatToken },
|
||||
});
|
||||
res.write(createEvent("content_block_delta", chunkJSON));
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
stream.on("end", () => {
|
||||
// send ending
|
||||
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
|
||||
res.write(
|
||||
createEvent("message_delta", {
|
||||
type: "message_delta",
|
||||
delta: { stop_reason: "end_turn", stop_sequence: null },
|
||||
usage: { output_tokens: 12 },
|
||||
})
|
||||
);
|
||||
res.write(createEvent("message_stop", { type: "message_stop" }));
|
||||
|
||||
res.end();
|
||||
});
|
||||
} else {
|
||||
throw new Error("Invalid request");
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.6.8",
|
||||
"express": "^4.19.2",
|
||||
"form-data": "^4.0.0",
|
||||
"uuid": "^9.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user