-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgemini.py
264 lines (221 loc) · 9.57 KB
/
gemini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import requests
import json
import re
from typing import Optional, Dict
from fake_useragent import UserAgent
class GeminiConversation:
def __init__(self):
self.conversation_id = ""
self.response_id = ""
self.choice_id = ""
class Gemini:
def __init__(self, cookie_path: str, timeout: int = 30):
"""
Initializes the Gemini client with the provided cookie.
Args:
cookie_path (str): Path to the cookie JSON file
timeout (int, optional): Request timeout in seconds. Defaults to 30
"""
self.session_auth1, self.session_auth2 = self._load_cookies(cookie_path)
self.session = self._init_session()
self.SNlM0e = self._get_snlm0e()
self.conversations: Dict[str, GeminiConversation] = {}
self.timeout = timeout
self.current_conversation = None
def _init_session(self) -> requests.Session:
"""Initialize and configure requests session"""
session = requests.Session()
session.headers.update({
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Host": "gemini.google.com",
"Origin": "https://gemini.google.com",
"Referer": "https://gemini.google.com/",
"User-Agent": UserAgent().random,
"X-Same-Domain": "1",
})
session.cookies.set("__Secure-1PSID", self.session_auth1)
session.cookies.set("__Secure-1PSIDTS", self.session_auth2)
return session
@staticmethod
def _load_cookies(cookie_path: str) -> tuple:
"""
Load cookies from the provided JSON file.
Args:
cookie_path (str): Path to the cookie JSON file
Returns:
tuple: (__Secure-1PSID, __Secure-1PSIDTS) values
"""
try:
with open(cookie_path, 'r') as file:
cookies = json.load(file)
session_auth1 = next(item['value'] for item in cookies if item['name'] == '__Secure-1PSID')
session_auth2 = next(item['value'] for item in cookies if item['name'] == '__Secure-1PSIDTS')
return session_auth1, session_auth2
except FileNotFoundError:
raise Exception(f"Cookie file not found at path: {cookie_path}")
except json.JSONDecodeError:
raise Exception("Invalid JSON format in the cookie file.")
except StopIteration:
raise Exception("Required cookies not found in the cookie file.")
def _get_snlm0e(self) -> str:
"""Get the SNlM0e value required for Gemini requests"""
try:
resp = self.session.get(
"https://gemini.google.com/app",
timeout=10,
)
resp.raise_for_status()
SNlM0e = re.search(r'"SNlM0e":"(.*?)"', resp.text)
if not SNlM0e:
raise Exception("SNlM0e value not found in response.")
return SNlM0e.group(1)
except requests.RequestException as e:
raise Exception(f"Failed to retrieve SNlM0e: {e}")
def create_conversation(self, conversation_name: str = None) -> str:
"""
Create a new conversation.
Args:
conversation_name (str, optional): Name for the conversation.
If None, generates a unique name.
Returns:
str: Conversation name
"""
if conversation_name is None:
conversation_name = f"conversation_{len(self.conversations) + 1}"
if conversation_name in self.conversations:
raise ValueError(f"Conversation '{conversation_name}' already exists")
self.conversations[conversation_name] = GeminiConversation()
self.current_conversation = conversation_name
return conversation_name
def switch_conversation(self, conversation_name: str) -> None:
"""
Switch to a different conversation.
Args:
conversation_name (str): Name of the conversation to switch to
"""
if conversation_name not in self.conversations:
raise ValueError(f"Conversation '{conversation_name}' does not exist")
self.current_conversation = conversation_name
def list_conversations(self) -> list:
"""List all available conversations"""
return list(self.conversations.keys())
def delete_conversation(self, conversation_name: str) -> None:
"""
Delete a conversation.
Args:
conversation_name (str): Name of the conversation to delete
"""
if conversation_name not in self.conversations:
raise ValueError(f"Conversation '{conversation_name}' does not exist")
del self.conversations[conversation_name]
if self.current_conversation == conversation_name:
self.current_conversation = None if not self.conversations else next(iter(self.conversations))
def ask(self, question: str, conversation_name: str = None) -> Optional[dict]:
"""
Send a question to Gemini and get the response.
Args:
question (str): The question to ask
conversation_name (str, optional): Name of the conversation to use.
If None, uses current conversation
Returns:
Optional[dict]: Response containing content and images
"""
if conversation_name:
self.switch_conversation(conversation_name)
elif not self.current_conversation:
self.create_conversation()
conv = self.conversations[self.current_conversation]
try:
params = {
"bl": "boq_assistant-bard-web-server_20230713.13_p0",
"_reqid": "0",
"rt": "c",
}
message_struct = [
[question],
None,
[conv.conversation_id, conv.response_id, conv.choice_id],
]
data = {
"f.req": json.dumps([None, json.dumps(message_struct)]),
"at": self.SNlM0e,
}
response = self.session.post(
"https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate",
params=params,
data=data,
timeout=self.timeout,
)
response.raise_for_status()
chat_data = json.loads(response.content.splitlines()[3])[0][2]
if not chat_data:
return {"content": None, "images": []}
json_chat_data = json.loads(chat_data)
images = []
if len(json_chat_data) >= 3 and len(json_chat_data[4][0]) >= 4 and json_chat_data[4][0][4]:
for img_data in json_chat_data[4][0][4]:
try:
images.append(img_data[0][0][0])
except (IndexError, TypeError):
continue
results = {
"content": json_chat_data[4][0][1][0] if len(json_chat_data[4][0][1]) > 0 else None,
"conversation_id": json_chat_data[1][0],
"response_id": json_chat_data[1][1],
"images": images,
}
# Update conversation state
conv.conversation_id = results["conversation_id"]
conv.response_id = results["response_id"]
conv.choice_id = json_chat_data[4][0][0] if len(json_chat_data[4][0]) > 0 else ""
return results
except Exception as e:
return {"content": f"Error: {str(e)}", "images": []}
def main():
"""Example usage of the Gemini client"""
gemini = Gemini('cookie.json')
# Create a few conversations
gemini.create_conversation("daily_chat")
# gemini.create_conversation("coding_help")
print("Available conversations:", gemini.list_conversations())
# Show current conversation
print(f"\nCurrent conversation: {gemini.current_conversation}")
print("\nCommands:")
print(" /switch <name> - Switch to a different conversation")
print(" /new <name> - Create a new conversation")
print(" /list - List all conversations")
print(" /delete <name> - Delete a conversation")
print(" /quit - Exit the program")
# Chat in different conversations
while True:
try:
user_input = input("\n>>> ")
# Handle commands
if user_input.startswith('/'):
cmd_parts = user_input.split()
cmd = cmd_parts[0].lower()
if cmd == '/switch' and len(cmd_parts) > 1:
gemini.switch_conversation(cmd_parts[1])
elif cmd == '/new' and len(cmd_parts) > 1:
gemini.create_conversation(cmd_parts[1])
elif cmd == '/list':
print("Conversations:", gemini.list_conversations())
elif cmd == '/delete' and len(cmd_parts) > 1:
gemini.delete_conversation(cmd_parts[1])
elif cmd == '/quit':
break
else:
print("Invalid command")
continue
# Send message and get response
response = gemini.ask(user_input)
if response and response["content"]:
print("\nGemini:", response["content"])
if response["images"]:
print("\nImages:", response["images"])
else:
print("\nNo response received")
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
main()