XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

``` feat: add private chat mode & update token parsing

- In g4f/Provider/Blackbox.py, import PaymentRequiredError and raise it when the response equals "You have reached your request limit for the hour". - In g4f/Provider/needs_auth/OpenaiChat.py, modify token parsing by splitting the "OpenAI-Sentinel-Proof-Token" header on "~" after the initial split. - In g4f/gui/client/index.html, add a new "Private Conversation" button with the corresponding icon. - In g4f/gui/client/static/js/chat.v1.js: - Introduce the variable `privateConversation` to handle private chats. - Update `new_conversation` to accept a private flag, setting `window.conversation_id` to null and updating the conversation title accordingly. - Adjust `get_conversation` to return `privateConversation` when conversation_id is null. - Revise `save_conversation` and `add_conversation` to store private conversation data when conversation_id is null. - Modify on-load conversation handling to incorporate the updated conversation logic. ```

323765d8
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

4 个文件 +36 -10
Modified g4f/Provider/Blackbox.py +3 -0
@@ -19,6 +19,7 @@ from ..cookies import get_cookies_dir
19 19 from .helper import format_image_prompt
20 20 from ..providers.response import JsonConversation, ImageResponse
21 21 from ..tools.media import merge_media
22 from ..errors import PaymentRequiredError
22 23 from .. import debug
23 24
24 25 class Conversation(JsonConversation):
@@ -689,6 +690,8 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
689 690 async for chunk in response.content.iter_any():
690 691 if chunk:
691 692 chunk_text = chunk.decode()
693 if chunk_text == "You have reached your request limit for the hour":
694 raise PaymentRequiredError(chunk_text)
692 695 full_response.append(chunk_text)
693 696 # Only yield chunks for non-image models
694 697 if model != cls.default_image_model:
Modified g4f/Provider/needs_auth/OpenaiChat.py +1 -1
@@ -605,7 +605,7 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
605 605 elif event.request.url in (backend_url, backend_anon_url):
606 606 if "OpenAI-Sentinel-Proof-Token" in event.request.headers:
607 607 cls.request_config.proof_token = json.loads(base64.b64decode(
608 event.request.headers["OpenAI-Sentinel-Proof-Token"].split("gAAAAAB", 1)[-1].encode()
608 event.request.headers["OpenAI-Sentinel-Proof-Token"].split("gAAAAAB", 1)[-1].split("~")[0].encode()
609 609 ).decode())
610 610 if "OpenAI-Sentinel-Turnstile-Token" in event.request.headers:
611 611 cls.request_config.turnstile_token = event.request.headers["OpenAI-Sentinel-Turnstile-Token"]
Modified g4f/gui/client/index.html +4 -0
@@ -86,6 +86,10 @@
86 86 <i class="fa-regular fa-plus"></i>
87 87 <span>New Conversation</span>
88 88 </button>
89 <button class="new_convo" onclick="new_conversation(true)">
90 <i class="fa-solid fa-user-secret"></i>
91 <span>Private Conversation</span>
92 </button>
89 93 </div>
90 94 <div class="bottom_buttons">
91 95 <button onclick="open_settings();">
Modified g4f/gui/client/static/js/chat.v1.js +28 -9
@@ -47,6 +47,7 @@ let is_demo = false;
47 47 let wakeLock = null;
48 48 let countTokensEnabled = true;
49 49 let reloadConversation = true;
50 let privateConversation = null;
50 51
51 52 userInput.addEventListener("blur", () => {
52 53 document.documentElement.scrollTop = 0;
@@ -1360,13 +1361,13 @@ const set_conversation = async (conversation_id) => {
1360 1361 hide_sidebar(true);
1361 1362 };
1362 1363
1363 const new_conversation = async () => {
1364 const new_conversation = async (private = false) => {
1364 1365 if (!/\/chat\/(share|\?|$)/.test(window.location.href)) {
1365 1366 history.pushState({}, null, `/chat/`);
1366 1367 }
1367 window.conversation_id = generateUUID();
1368 window.conversation_id = private ? null : generateUUID();
1368 1369 document.title = window.title || document.title;
1369 document.querySelector(".chat-top-panel .convo-title").innerText = "New Conversation";
1370 document.querySelector(".chat-top-panel .convo-title").innerText = `${private ? "Private" : "New"} Conversation`;
1370 1371
1371 1372 await clear_conversation();
1372 1373 if (chatPrompt) {
@@ -1622,6 +1623,9 @@ async function safe_load_conversation(conversation_id, scroll=true) {
1622 1623 }
1623 1624
1624 1625 async function get_conversation(conversation_id) {
1626 if (!conversation_id) {
1627 return privateConversation;
1628 }
1625 1629 let conversation = await JSON.parse(
1626 1630 appStorage.getItem(`conversation:${conversation_id}`)
1627 1631 );
@@ -1630,13 +1634,17 @@ async function get_conversation(conversation_id) {
1630 1634
1631 1635 function get_conversation_data(conversation) {
1632 1636 conversation.updated = Date.now();
1633 return JSON.stringify(conversation);
1637 return conversation;
1634 1638 }
1635 1639
1636 async function save_conversation(conversation_id, data) {
1640 async function save_conversation(conversation_id, conversation) {
1641 if (!conversation_id) {
1642 privateConversation = conversation;
1643 return;
1644 }
1637 1645 appStorage.setItem(
1638 1646 `conversation:${conversation_id}`,
1639 data
1647 JSON.stringify(conversation)
1640 1648 );
1641 1649 }
1642 1650
@@ -1646,6 +1654,16 @@ async function get_messages(conversation_id) {
1646 1654 }
1647 1655
1648 1656 async function add_conversation(conversation_id) {
1657 if (!conversation_id) {
1658 privateConversation = {
1659 id: conversation_id,
1660 title: "",
1661 added: Date.now(),
1662 system: chatPrompt?.value,
1663 items: [],
1664 }
1665 return;
1666 }
1649 1667 if (appStorage.getItem(`conversation:${conversation_id}`) == null) {
1650 1668 await save_conversation(conversation_id, get_conversation_data({
1651 1669 id: conversation_id,
@@ -2143,7 +2161,7 @@ window.addEventListener('load', async function() {
2143 2161 window.share_id = null;
2144 2162 }
2145 2163 await load_conversation(conversation);
2146 await save_conversation(conversation.id, JSON.stringify(conversation));
2164 await save_conversation(conversation.id, conversation);
2147 2165 await load_conversations();
2148 2166 if (!window.share_id) {
2149 2167 // Continue after copy conversation
@@ -2211,6 +2229,7 @@ async function on_load() {
2211 2229 count_input();
2212 2230 if (/\/settings\//.test(window.location.href)) {
2213 2231 open_settings();
2232 await load_conversations();
2214 2233 } else if (/\/chat\/(share|\?|$)/.test(window.location.href)) {
2215 2234 chatPrompt.value = document.getElementById("systemPrompt")?.value || "";
2216 2235 chatPrompt.value = document.getElementById("systemPrompt")?.value || "";
@@ -2221,12 +2240,12 @@ async function on_load() {
2221 2240 userInput.style.height = userInput.scrollHeight + "px";
2222 2241 userInput.focus();
2223 2242 } else {
2224 new_conversation();
2243 await new_conversation();
2225 2244 }
2226 2245 } else {
2227 2246 //load_conversation(window.conversation_id);
2247 await load_conversations();
2228 2248 }
2229 load_conversations();
2230 2249 if (window.hljs) {
2231 2250 hljs.addPlugin(new HtmlRenderPlugin())
2232 2251 hljs.addPlugin(new CopyButtonPlugin());