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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Main.py
|
3 |
+
"""
|
4 |
+
from __future__ import annotations
|
5 |
+
|
6 |
+
import json
|
7 |
+
from pathlib import Path
|
8 |
+
from typing import Generator
|
9 |
+
|
10 |
+
from .chathub import *
|
11 |
+
from .conversation import *
|
12 |
+
from .conversation_style import *
|
13 |
+
from .request import *
|
14 |
+
from .utilities import *
|
15 |
+
|
16 |
+
|
17 |
+
class Chatbot:
|
18 |
+
"""
|
19 |
+
Combines everything to make it seamless
|
20 |
+
"""
|
21 |
+
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
proxy: str | None = None,
|
25 |
+
cookies: list[dict] | None = None,
|
26 |
+
) -> None:
|
27 |
+
self.proxy: str | None = proxy
|
28 |
+
self.chat_hub: ChatHub = ChatHub(
|
29 |
+
Conversation(self.proxy, cookies=cookies),
|
30 |
+
proxy=self.proxy,
|
31 |
+
cookies=cookies,
|
32 |
+
)
|
33 |
+
|
34 |
+
@staticmethod
|
35 |
+
async def create(
|
36 |
+
proxy: str | None = None,
|
37 |
+
cookies: list[dict] | None = None,
|
38 |
+
) -> Chatbot:
|
39 |
+
self = Chatbot.__new__(Chatbot)
|
40 |
+
self.proxy = proxy
|
41 |
+
self.chat_hub = ChatHub(
|
42 |
+
await Conversation.create(self.proxy, cookies=cookies),
|
43 |
+
proxy=self.proxy,
|
44 |
+
cookies=cookies,
|
45 |
+
)
|
46 |
+
return self
|
47 |
+
|
48 |
+
async def save_conversation(self, filename: str) -> None:
|
49 |
+
"""
|
50 |
+
Save the conversation to a file
|
51 |
+
"""
|
52 |
+
with open(filename, "w") as f:
|
53 |
+
conversation_id = self.chat_hub.request.conversation_id
|
54 |
+
conversation_signature = self.chat_hub.request.conversation_signature
|
55 |
+
client_id = self.chat_hub.request.client_id
|
56 |
+
invocation_id = self.chat_hub.request.invocation_id
|
57 |
+
f.write(
|
58 |
+
json.dumps(
|
59 |
+
{
|
60 |
+
"conversation_id": conversation_id,
|
61 |
+
"conversation_signature": conversation_signature,
|
62 |
+
"client_id": client_id,
|
63 |
+
"invocation_id": invocation_id,
|
64 |
+
},
|
65 |
+
),
|
66 |
+
)
|
67 |
+
|
68 |
+
async def load_conversation(self, filename: str) -> None:
|
69 |
+
"""
|
70 |
+
Load the conversation from a file
|
71 |
+
"""
|
72 |
+
with open(filename) as f:
|
73 |
+
conversation = json.load(f)
|
74 |
+
self.chat_hub.request = ChatHubRequest(
|
75 |
+
conversation_signature=conversation["conversation_signature"],
|
76 |
+
client_id=conversation["client_id"],
|
77 |
+
conversation_id=conversation["conversation_id"],
|
78 |
+
invocation_id=conversation["invocation_id"],
|
79 |
+
)
|
80 |
+
|
81 |
+
async def get_conversation(self) -> dict:
|
82 |
+
"""
|
83 |
+
Gets the conversation history from conversation_id (requires load_conversation)
|
84 |
+
"""
|
85 |
+
return await self.chat_hub.get_conversation()
|
86 |
+
|
87 |
+
async def get_activity(self) -> dict:
|
88 |
+
"""
|
89 |
+
Gets the recent activity (requires cookies)
|
90 |
+
"""
|
91 |
+
return await self.chat_hub.get_activity()
|
92 |
+
|
93 |
+
async def ask(
|
94 |
+
self,
|
95 |
+
prompt: str,
|
96 |
+
wss_link: str = "wss://sydney.bing.com/sydney/ChatHub",
|
97 |
+
conversation_style: CONVERSATION_STYLE_TYPE = None,
|
98 |
+
webpage_context: str | None = None,
|
99 |
+
search_result: bool = False,
|
100 |
+
locale: str = guess_locale(),
|
101 |
+
simplify_response: bool = False,
|
102 |
+
) -> dict:
|
103 |
+
"""
|
104 |
+
Ask a question to the bot
|
105 |
+
Response:
|
106 |
+
{
|
107 |
+
item (dict):
|
108 |
+
messages (list[dict]):
|
109 |
+
adaptiveCards (list[dict]):
|
110 |
+
body (list[dict]):
|
111 |
+
text (str): Response
|
112 |
+
}
|
113 |
+
To get the response, you can do:
|
114 |
+
response["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
|
115 |
+
"""
|
116 |
+
async for final, response in self.chat_hub.ask_stream(
|
117 |
+
prompt=prompt,
|
118 |
+
conversation_style=conversation_style,
|
119 |
+
wss_link=wss_link,
|
120 |
+
webpage_context=webpage_context,
|
121 |
+
search_result=search_result,
|
122 |
+
locale=locale,
|
123 |
+
):
|
124 |
+
if final:
|
125 |
+
if not simplify_response:
|
126 |
+
return response
|
127 |
+
messages_left = response["item"]["throttling"][
|
128 |
+
"maxNumUserMessagesInConversation"
|
129 |
+
] - response["item"]["throttling"].get(
|
130 |
+
"numUserMessagesInConversation",
|
131 |
+
0,
|
132 |
+
)
|
133 |
+
if messages_left == 0:
|
134 |
+
raise Exception("Max messages reached")
|
135 |
+
message = ""
|
136 |
+
for msg in reversed(response["item"]["messages"]):
|
137 |
+
if msg.get("adaptiveCards") and msg["adaptiveCards"][0]["body"][
|
138 |
+
0
|
139 |
+
].get("text"):
|
140 |
+
message = msg
|
141 |
+
break
|
142 |
+
if not message:
|
143 |
+
raise Exception("No message found")
|
144 |
+
suggestions = [
|
145 |
+
suggestion["text"]
|
146 |
+
for suggestion in message.get("suggestedResponses", [])
|
147 |
+
]
|
148 |
+
adaptive_cards = message.get("adaptiveCards", [])
|
149 |
+
adaptive_text = (
|
150 |
+
adaptive_cards[0]["body"][0].get("text") if adaptive_cards else None
|
151 |
+
)
|
152 |
+
sources = (
|
153 |
+
adaptive_cards[0]["body"][0].get("text") if adaptive_cards else None
|
154 |
+
)
|
155 |
+
sources_text = (
|
156 |
+
adaptive_cards[0]["body"][-1].get("text")
|
157 |
+
if adaptive_cards
|
158 |
+
else None
|
159 |
+
)
|
160 |
+
return {
|
161 |
+
"text": message["text"],
|
162 |
+
"author": message["author"],
|
163 |
+
"sources": sources,
|
164 |
+
"sources_text": sources_text,
|
165 |
+
"suggestions": suggestions,
|
166 |
+
"messages_left": messages_left,
|
167 |
+
"max_messages": response["item"]["throttling"][
|
168 |
+
"maxNumUserMessagesInConversation"
|
169 |
+
],
|
170 |
+
"adaptive_text": adaptive_text,
|
171 |
+
}
|
172 |
+
return {}
|
173 |
+
|
174 |
+
async def ask_stream(
|
175 |
+
self,
|
176 |
+
prompt: str,
|
177 |
+
wss_link: str = "wss://sydney.bing.com/sydney/ChatHub",
|
178 |
+
conversation_style: CONVERSATION_STYLE_TYPE = None,
|
179 |
+
raw: bool = False,
|
180 |
+
webpage_context: str | None = None,
|
181 |
+
search_result: bool = False,
|
182 |
+
locale: str = guess_locale(),
|
183 |
+
) -> Generator[bool, dict | str, None]:
|
184 |
+
"""
|
185 |
+
Ask a question to the bot
|
186 |
+
"""
|
187 |
+
async for response in self.chat_hub.ask_stream(
|
188 |
+
prompt=prompt,
|
189 |
+
conversation_style=conversation_style,
|
190 |
+
wss_link=wss_link,
|
191 |
+
raw=raw,
|
192 |
+
webpage_context=webpage_context,
|
193 |
+
search_result=search_result,
|
194 |
+
locale=locale,
|
195 |
+
):
|
196 |
+
yield response
|
197 |
+
|
198 |
+
async def close(self) -> None:
|
199 |
+
"""
|
200 |
+
Close the connection
|
201 |
+
"""
|
202 |
+
await self.chat_hub.close()
|
203 |
+
|
204 |
+
async def delete_conversation(
|
205 |
+
self,
|
206 |
+
conversation_id: str = None,
|
207 |
+
conversation_signature: str = None,
|
208 |
+
client_id: str = None,
|
209 |
+
) -> None:
|
210 |
+
"""
|
211 |
+
Delete the chat in the server
|
212 |
+
"""
|
213 |
+
await self.chat_hub.delete_conversation(
|
214 |
+
conversation_id=conversation_id,
|
215 |
+
conversation_signature=conversation_signature,
|
216 |
+
client_id=client_id,
|
217 |
+
)
|
218 |
+
|
219 |
+
async def reset(self, delete=False) -> None:
|
220 |
+
"""
|
221 |
+
Reset the conversation
|
222 |
+
"""
|
223 |
+
if delete:
|
224 |
+
await self.remove_and_close()
|
225 |
+
else:
|
226 |
+
await self.close()
|
227 |
+
self.chat_hub = ChatHub(
|
228 |
+
await Conversation.create(self.proxy, cookies=self.chat_hub.cookies),
|
229 |
+
proxy=self.proxy,
|
230 |
+
cookies=self.chat_hub.cookies,
|
231 |
+
)
|
232 |
+
|
233 |
+
|
234 |
+
if __name__ == "__main__":
|
235 |
+
from .main import main
|
236 |
+
|
237 |
+
main()
|