BeautyyuYanli commited on
Commit
f3c67ae
·
unverified ·
1 Parent(s): 6d7c9be

Support passing in the cookies directly (#78)

Browse files
Files changed (2) hide show
  1. README.md +17 -4
  2. src/EdgeGPT.py +14 -8
README.md CHANGED
@@ -102,11 +102,24 @@ options:
102
  -----
103
 
104
  ### Developer demo
105
- Remember to set cookie file path: `export COOKIE_FILE=/path/to/cookies.json`. You can also specify the path to `cookies.json` in the argument `cookiePath` like this:
106
 
107
- ```python
108
- bot = Chatbot(cookiePath='./cookie.json')
109
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  Use Async for the best experience
112
 
 
102
  -----
103
 
104
  ### Developer demo
 
105
 
106
+ Three ways to pass in cookies:
107
+
108
+ - Environment variable: `export COOKIE_FILE=/path/to/cookies.json`.
109
+ - Specify the path to `cookies.json` in the argument `cookiePath` like this:
110
+
111
+ ```python
112
+ bot = Chatbot(cookiePath='./cookie.json')
113
+ ```
114
+
115
+ - Pass in the cookies directly by the argument `cookies`, like this:
116
+
117
+ ```python
118
+ with open('./cookie.json', 'r') as f:
119
+ cookies = json.load(f)
120
+ bot = Chatbot(cookies=cookies)
121
+ ```
122
+
123
 
124
  Use Async for the best experience
125
 
src/EdgeGPT.py CHANGED
@@ -121,7 +121,7 @@ class Conversation:
121
  Conversation API
122
  """
123
 
124
- def __init__(self, cookiePath: str = "") -> None:
125
  self.struct: dict = {
126
  "conversationId": None,
127
  "clientId": None,
@@ -129,11 +129,15 @@ class Conversation:
129
  "result": {"value": "Success", "message": None},
130
  }
131
  self.session = tls_client.Session(client_identifier="chrome_108")
132
- if cookiePath == "":
133
- f = open(os.environ.get("COOKIE_FILE"), encoding="utf-8").read()
134
  else:
135
- f = open(cookiePath, encoding="utf8").read()
136
- cookie_file = json.loads(f)
 
 
 
 
137
  for cookie in cookie_file:
138
  self.session.cookies.set(cookie["name"], cookie["value"])
139
  url = "https://edgeservices.bing.com/edgesvc/turing/conversation/create"
@@ -231,9 +235,11 @@ class Chatbot:
231
  Combines everything to make it seamless
232
  """
233
 
234
- def __init__(self, cookiePath: str = "") -> None:
235
  self.cookiePath: str = cookiePath
236
- self.chat_hub: ChatHub = ChatHub(Conversation(self.cookiePath))
 
 
237
 
238
  async def ask(self, prompt: str) -> dict:
239
  """
@@ -262,7 +268,7 @@ class Chatbot:
262
  Reset the conversation
263
  """
264
  await self.close()
265
- self.chat_hub = ChatHub(Conversation(self.cookiePath))
266
 
267
 
268
  def get_input(prompt):
 
121
  Conversation API
122
  """
123
 
124
+ def __init__(self, cookiePath: str = "", cookies: dict | None = None) -> None:
125
  self.struct: dict = {
126
  "conversationId": None,
127
  "clientId": None,
 
129
  "result": {"value": "Success", "message": None},
130
  }
131
  self.session = tls_client.Session(client_identifier="chrome_108")
132
+ if cookies is not None:
133
+ cookie_file = cookies
134
  else:
135
+ if cookiePath == "":
136
+ f = open(os.environ.get("COOKIE_FILE"),
137
+ encoding="utf-8").read()
138
+ else:
139
+ f = open(cookiePath, encoding="utf8").read()
140
+ cookie_file = json.loads(f)
141
  for cookie in cookie_file:
142
  self.session.cookies.set(cookie["name"], cookie["value"])
143
  url = "https://edgeservices.bing.com/edgesvc/turing/conversation/create"
 
235
  Combines everything to make it seamless
236
  """
237
 
238
+ def __init__(self, cookiePath: str = "", cookies: dict | None = None) -> None:
239
  self.cookiePath: str = cookiePath
240
+ self.cookies: dict | None = cookies
241
+ self.chat_hub: ChatHub = ChatHub(
242
+ Conversation(self.cookiePath, self.cookies))
243
 
244
  async def ask(self, prompt: str) -> dict:
245
  """
 
268
  Reset the conversation
269
  """
270
  await self.close()
271
+ self.chat_hub = ChatHub(Conversation(self.cookiePath, self.cookies))
272
 
273
 
274
  def get_input(prompt):