Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 1692b56

Browse files
committed
Add get and set conversation
Add get and set conversation
1 parent fcb171f commit 1692b56

File tree

7 files changed

+89
-121
lines changed

7 files changed

+89
-121
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"]
55
build-backend = "setuptools.build_meta"
66

77
[project]
8-
name = "re_edge_gpt"
9-
version = "0.0.18"
8+
name = "re_edge_gpt_dev"
9+
version = "0.0.19"
1010
authors = [
1111
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
1212
]

re_edge_gpt/chathub.py

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from .request import ChatHubRequest
2222
from .upload_image import upload_image, upload_image_url
2323
from .utilities import append_identifier
24-
from .utilities import get_ran_hex
2524
from .utilities import guess_locale
2625

2726
ssl_context = ssl.create_default_context()
@@ -62,36 +61,7 @@ def __init__(
6261
self.encrypted_conversation_signature = conversation.struct["encryptedConversationSignature"]
6362
else:
6463
self.encrypted_conversation_signature = None
65-
66-
async def get_conversation(
67-
self,
68-
conversation_id: str = None,
69-
conversation_signature: str = None,
70-
client_id: str = None,
71-
) -> dict:
72-
self.conversation_id = conversation_id or self.request.conversation_id
73-
conversation_signature = (
74-
conversation_signature or self.request.conversation_signature
75-
)
76-
client_id = client_id or self.request.client_id
77-
url = f"https://sydney.bing.com/sydney/GetConversation" \
78-
f"?conversationId={conversation_id}" \
79-
f"&source=cib&participantId={client_id}" \
80-
f"&conversationSignature={conversation_signature}" \
81-
f"&traceId={get_ran_hex()}"
82-
response = await self.session.get(url)
83-
return response.json()
84-
85-
async def get_activity(self) -> dict:
86-
url = "https://www.bing.com/turing/conversation/chats"
87-
headers = HEADERS_INIT_CONVER.copy()
88-
if self.cookies is not None:
89-
for cookie in self.cookies:
90-
if cookie["name"] == "_U":
91-
headers["Cookie"] = f"SUID=A; _U={cookie['value']};"
92-
break
93-
response = await self.session.get(url, headers=headers)
94-
return response.json()
64+
self.conversation = conversation
9565

9666
async def ask_stream(
9767
self,
@@ -221,28 +191,20 @@ async def ask_stream(
221191
elif raw:
222192
yield False, response
223193

224-
async def delete_conversation(
225-
self,
226-
conversation_id: str = None,
227-
conversation_signature: str = None,
228-
client_id: str = None,
229-
) -> None:
230-
conversation_id = conversation_id or self.request.conversation_id
231-
conversation_signature = (
232-
conversation_signature or self.request.conversation_signature
233-
)
234-
client_id = client_id or self.request.client_id
235-
url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
236-
await self.session.post(
237-
url,
238-
json={
239-
"conversationId": conversation_id,
240-
"conversationSignature": conversation_signature,
241-
"participant": {"id": client_id},
242-
"source": "cib",
243-
"optionsSets": ["autosave"],
244-
},
245-
)
246-
247194
async def close(self) -> None:
248195
await self.session.aclose()
196+
197+
async def get_conversation(self):
198+
return {
199+
"conversation_id": self.conversation_id,
200+
"client_id": self.request.client_id,
201+
"encrypted_conversation_signature": self.encrypted_conversation_signature,
202+
"conversation_signature": self.request.conversation_signature,
203+
}
204+
205+
async def set_conversation(self, conversation_dict: dict):
206+
self.conversation.struct["conversationId"] = conversation_dict.get("conversation_id")
207+
self.conversation.struct["client_id"] = conversation_dict.get("client_id")
208+
self.conversation.struct[
209+
"encrypted_conversation_signature"] = conversation_dict.get("encrypted_conversation_signature")
210+
self.conversation.struct["conversation_signature"] = conversation_dict.get("conversation_signature")

re_edge_gpt/re_edge_gpt.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,6 @@ async def create(
4040
)
4141
return self
4242

43-
async def save_conversation(self, filename: str) -> None:
44-
"""
45-
Save the conversation to a file
46-
"""
47-
with open(filename, "w") as f:
48-
conversation_id = self.chat_hub.request.conversation_id
49-
conversation_signature = self.chat_hub.request.conversation_signature
50-
client_id = self.chat_hub.request.client_id
51-
invocation_id = self.chat_hub.request.invocation_id
52-
f.write(
53-
json.dumps(
54-
{
55-
"conversation_id": conversation_id,
56-
"conversation_signature": conversation_signature,
57-
"client_id": client_id,
58-
"invocation_id": invocation_id,
59-
},
60-
),
61-
)
62-
63-
async def load_conversation(self, filename: str) -> None:
64-
"""
65-
Load the conversation from a file
66-
"""
67-
with open(filename) as f:
68-
conversation = json.load(f)
69-
self.chat_hub.request = ChatHubRequest(
70-
conversation_signature=conversation["conversation_signature"],
71-
client_id=conversation["client_id"],
72-
conversation_id=conversation["conversation_id"],
73-
invocation_id=conversation["invocation_id"],
74-
)
75-
76-
async def get_conversation(self) -> dict:
77-
"""
78-
Gets the conversation history from conversation_id (requires load_conversation)
79-
"""
80-
return await self.chat_hub.get_conversation()
81-
82-
async def get_activity(self) -> dict:
83-
"""
84-
Gets the recent activity (requires cookies)
85-
"""
86-
return await self.chat_hub.get_activity()
8743

8844
async def ask(
8945
self,
@@ -198,21 +154,6 @@ async def close(self) -> None:
198154
"""
199155
await self.chat_hub.close()
200156

201-
async def delete_conversation(
202-
self,
203-
conversation_id: str = None,
204-
conversation_signature: str = None,
205-
client_id: str = None,
206-
) -> None:
207-
"""
208-
Delete the chat in the server
209-
"""
210-
await self.chat_hub.delete_conversation(
211-
conversation_id=conversation_id,
212-
conversation_signature=conversation_signature,
213-
client_id=client_id,
214-
)
215-
216157
async def reset(self) -> None:
217158
"""
218159
Reset the conversation

re_edge_gpt/utilities.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def get_ran_hex(length: int = 32) -> str:
1919

2020
def get_location_hint_from_locale(locale_str: str) -> Union[dict, None]:
2121
locale_str = locale_str.lower()
22-
if locale == "en-gb":
22+
if locale_str == "en-gb":
2323
hint = LocationHint.UK.value
24-
elif locale == "en-ie":
24+
elif locale_str == "en-ie":
2525
hint = LocationHint.EU.value
26-
elif locale == "zh-cn":
26+
elif locale_str == "zh-cn":
2727
hint = LocationHint.CHINA.value
2828
else:
2929
hint = LocationHint.USA.value

dev.toml renamed to stable.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"]
55
build-backend = "setuptools.build_meta"
66

77
[project]
8-
name = "re_edge_gpt_dev"
9-
version = "0.0.18"
8+
name = "re_edge_gpt"
9+
version = "0.0.19"
1010
authors = [
1111
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
1212
]

test/unit_test/manual_test/test_bot_manual.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def test_ask() -> None:
1717
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
1818
bot = await Chatbot.create(cookies=cookies)
1919
response = await bot.ask(
20-
prompt="How to get extract iron on mine",
20+
prompt="How to extract gold",
2121
conversation_style=ConversationStyle.balanced,
2222
simplify_response=True
2323
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import asyncio
2+
import json
3+
from pathlib import Path
4+
5+
from re_edge_gpt import Chatbot
6+
from re_edge_gpt import ConversationStyle
7+
8+
# If you are using jupyter pls install this package
9+
# from nest_asyncio import apply
10+
11+
conversation_dict = {}
12+
13+
14+
async def test_ask() -> None:
15+
bot = None
16+
try:
17+
cookies: list[dict] = json.loads(open(
18+
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
19+
bot = await Chatbot.create(cookies=cookies)
20+
response = await bot.ask(
21+
prompt="Translate next word what I say to english",
22+
conversation_style=ConversationStyle.balanced,
23+
simplify_response=True
24+
)
25+
# If you are using non ascii char you need set ensure_ascii=False
26+
print(json.dumps(response, indent=2, ensure_ascii=False))
27+
print(await bot.chat_hub.get_conversation())
28+
conversation_dict.update(await bot.chat_hub.get_conversation())
29+
except Exception as error:
30+
raise error
31+
finally:
32+
if bot is not None:
33+
await bot.close()
34+
35+
36+
async def test_ask_conversation() -> None:
37+
bot = None
38+
try:
39+
cookies: list[dict] = json.loads(open(
40+
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
41+
bot = await Chatbot.create(cookies=cookies)
42+
await bot.chat_hub.set_conversation(conversation_dict=conversation_dict)
43+
response = await bot.ask(
44+
prompt="піца",
45+
conversation_style=ConversationStyle.balanced,
46+
simplify_response=True
47+
)
48+
# If you are using non ascii char you need set ensure_ascii=False
49+
print(json.dumps(response, indent=2, ensure_ascii=False))
50+
except Exception as error:
51+
raise error
52+
finally:
53+
if bot is not None:
54+
await bot.close()
55+
56+
57+
if __name__ == "__main__":
58+
# If you are using jupyter pls use nest_asyncio apply()
59+
# apply()
60+
try:
61+
loop = asyncio.get_running_loop()
62+
except RuntimeError:
63+
loop = asyncio.get_event_loop()
64+
loop.run_until_complete(test_ask())
65+
loop.run_until_complete(test_ask_conversation())

0 commit comments

Comments
 (0)