Conight commited on
Commit
3421236
·
unverified ·
1 Parent(s): ab41314

Fix return typing and format code. (#47)

Browse files
Files changed (1) hide show
  1. src/EdgeGPT.py +14 -10
src/EdgeGPT.py CHANGED
@@ -4,16 +4,20 @@ Main.py
4
  import argparse
5
  import asyncio
6
  import json
7
- import sys
8
  import os
 
 
9
 
10
  import tls_client
11
  import websockets.client as websockets
12
 
13
  DELIMITER = "\x1e"
14
 
15
- headers = {
16
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41",
 
 
 
17
  "origin": "https://www.bing.com",
18
  "referer": "https://www.bing.com/",
19
  "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
@@ -45,7 +49,7 @@ class ChatHubRequest:
45
  conversation_id: str,
46
  invocation_id: int = 0,
47
  ) -> None:
48
- self.struct: dict
49
 
50
  self.client_id: str = client_id
51
  self.conversation_id: str = conversation_id
@@ -115,7 +119,7 @@ class Conversation:
115
  response = self.session.get(
116
  url,
117
  timeout_seconds=30,
118
- headers=headers,
119
  )
120
  if response.status_code != 200:
121
  raise Exception("Authentication failed")
@@ -135,7 +139,7 @@ class ChatHub:
135
  """
136
 
137
  def __init__(self, conversation: Conversation) -> None:
138
- self.wss: websockets.WebSocketClientProtocol = None
139
  self.request: ChatHubRequest
140
  self.loop: bool
141
  self.task: asyncio.Task
@@ -145,7 +149,7 @@ class ChatHub:
145
  conversation_id=conversation.struct["conversationId"],
146
  )
147
 
148
- async def ask_stream(self, prompt: str) -> str:
149
  """
150
  Ask a question to the bot
151
  """
@@ -154,14 +158,14 @@ class ChatHub:
154
  if self.wss.closed:
155
  self.wss = await websockets.connect(
156
  "wss://sydney.bing.com/sydney/ChatHub",
157
- extra_headers=headers,
158
  max_size=None,
159
  )
160
  await self.__initial_handshake()
161
  else:
162
  self.wss = await websockets.connect(
163
  "wss://sydney.bing.com/sydney/ChatHub",
164
- extra_headers=headers,
165
  max_size=None,
166
  )
167
  await self.__initial_handshake()
@@ -213,7 +217,7 @@ class Chatbot:
213
  if final:
214
  return response
215
 
216
- async def ask_stream(self, prompt: str) -> str:
217
  """
218
  Ask a question to the bot
219
  """
 
4
  import argparse
5
  import asyncio
6
  import json
 
7
  import os
8
+ import sys
9
+ from typing import Generator, Optional
10
 
11
  import tls_client
12
  import websockets.client as websockets
13
 
14
  DELIMITER = "\x1e"
15
 
16
+ HEADERS = {
17
+ "user-agent": (
18
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
19
+ "Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41"
20
+ ),
21
  "origin": "https://www.bing.com",
22
  "referer": "https://www.bing.com/",
23
  "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
 
49
  conversation_id: str,
50
  invocation_id: int = 0,
51
  ) -> None:
52
+ self.struct: dict = {}
53
 
54
  self.client_id: str = client_id
55
  self.conversation_id: str = conversation_id
 
119
  response = self.session.get(
120
  url,
121
  timeout_seconds=30,
122
+ headers=HEADERS,
123
  )
124
  if response.status_code != 200:
125
  raise Exception("Authentication failed")
 
139
  """
140
 
141
  def __init__(self, conversation: Conversation) -> None:
142
+ self.wss: Optional[websockets.WebSocketClientProtocol] = None
143
  self.request: ChatHubRequest
144
  self.loop: bool
145
  self.task: asyncio.Task
 
149
  conversation_id=conversation.struct["conversationId"],
150
  )
151
 
152
+ async def ask_stream(self, prompt: str) -> Generator[str]:
153
  """
154
  Ask a question to the bot
155
  """
 
158
  if self.wss.closed:
159
  self.wss = await websockets.connect(
160
  "wss://sydney.bing.com/sydney/ChatHub",
161
+ extra_headers=HEADERS,
162
  max_size=None,
163
  )
164
  await self.__initial_handshake()
165
  else:
166
  self.wss = await websockets.connect(
167
  "wss://sydney.bing.com/sydney/ChatHub",
168
+ extra_headers=HEADERS,
169
  max_size=None,
170
  )
171
  await self.__initial_handshake()
 
217
  if final:
218
  return response
219
 
220
+ async def ask_stream(self, prompt: str) -> Generator[str]:
221
  """
222
  Ask a question to the bot
223
  """