This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INITIAL: ChatGPT API wrapping, uses Selenium to re-login from time to…
… time
- Loading branch information
1 parent
16d2056
commit 0ed101c
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from typing import Dict | ||
import requests | ||
import json | ||
from uuid import uuid4 | ||
from selenium import webdriver | ||
from selenium.webdriver.support.ui import WebDriverWait | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support import expected_conditions as EC | ||
|
||
|
||
class ChatGPT: | ||
def __init__(self, username: str, password: str) -> None: | ||
self.username = username | ||
self.password = password | ||
self.urls = { | ||
'login': 'https://chat.openai.com/auth/login', | ||
'session': 'https://chat.openai.com/api/auth/session', | ||
'conversation': 'https://chat.openai.com/backend-api/conversation', | ||
} | ||
self.session = requests.session() | ||
self.access_token = None | ||
self.conversation_id = str(uuid4()) | ||
self.parent_message_id = str(uuid4()) | ||
|
||
def login(self) -> Dict[str, str]: | ||
chrome_options = webdriver.ChromeOptions() | ||
chrome_options.add_argument('--headless') | ||
chrome_options.add_argument('--no-sandbox') | ||
chrome_options.add_argument('--disable-dev-shm-usage') | ||
driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options) | ||
driver.get(self.urls['login']) | ||
driver.find_element(By.XPATH, "//button[contains(.,'Log in')]").click() | ||
element = WebDriverWait(driver, timeout=10).until( | ||
EC.presence_of_element_located((By.XPATH, "//input[@name='username']")) | ||
) | ||
element.send_keys(self.username) | ||
driver.find_element(By.XPATH, "//button[@type='submit']").click() | ||
element = WebDriverWait(driver, timeout=10).until( | ||
EC.presence_of_element_located((By.XPATH, "//input[@name='password']")) | ||
) | ||
element.send_keys(self.password) | ||
driver.find_element(By.XPATH, "//button[@type='submit']").click() | ||
WebDriverWait(driver=driver, timeout=10).until( | ||
lambda x: x.execute_script("return document.readyState === 'complete'") | ||
) | ||
cookies = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()} | ||
return cookies | ||
|
||
def get_access_token(self) -> str: | ||
cookies = self.login() | ||
response = self.session.get(self.urls['session'], cookies=cookies) | ||
data = json.loads(response.text) | ||
return data['accessToken'] | ||
|
||
def get_response(self, request: str, attempts: int = 1) -> str: | ||
if self.access_token is None: | ||
self.access_token = self.get_access_token() | ||
headers = { | ||
'Authorization': 'Bearer {}'.format(self.access_token) | ||
} | ||
message_id = str(uuid4()) | ||
data = { | ||
'action': 'next', | ||
'messages': [ | ||
{ | ||
'id': message_id, | ||
'role': 'user', | ||
'content': { | ||
'content_type': 'text', | ||
'parts': [ | ||
str(request), | ||
], | ||
}, | ||
}, | ||
], | ||
'parent_message_id': self.parent_message_id, | ||
'model': 'text-davinci-002-render', | ||
} | ||
response = self.session.post(self.urls['conversation'], headers=headers, json=data) | ||
if response.ok: | ||
self.parent_message_id = message_id | ||
data = [chunk for chunk in response.text.split('\n') if chunk.strip()] | ||
data = data[-2].lstrip('data: ') | ||
data = json.loads(data) | ||
return data['message']['content']['parts'][0] | ||
elif attempts > 0: | ||
self.access_token = None | ||
return self.get_response(request, attempts - 1) | ||
return "Unknown error." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import argparse | ||
from chatgpt import ChatGPT | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'--email', | ||
type=str, | ||
required=True, | ||
help='OpenAI e-mail', | ||
) | ||
parser.add_argument( | ||
'--password', | ||
type=str, | ||
required=True, | ||
help='OpenAI password', | ||
) | ||
args = parser.parse_args() | ||
chat = ChatGPT(args.email, args.password) | ||
while True: | ||
message = input('> ') | ||
print(chat.get_response(message)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
requests==2.28.1 | ||
selenium==4.7.2 | ||
|