Skip to content

Commit

Permalink
Merge pull request #9 from keenborder786/gpt_fix
Browse files Browse the repository at this point in the history
GPT Fix
  • Loading branch information
keenborder786 authored Jun 22, 2024
2 parents 99453ea + 433c339 commit 0479ddf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 74 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ pip install free_llms
```python

from free_llms.models import GPTChrome
driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"]
with GPTChrome(driver_config=driver_config,
email = 'email',
password = 'password') as session: # A single session started with ChartGPT

driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"]
with GPTChrome(
driver_config=driver_config,
email="", # for gpt we do not need email
password="", # # for gpt we do not need password
) as session: # A single session started with ChartGPT
data = session.send_prompt("""Write an SQL Query which shows how to get third highest salary
""") # First Message
data1 = session.send_prompt('Now convert it into python') # Second message
print(session.messages) # Messages in the current session in pair of <Human,AI>
""") # First Message
data1 = session.send_prompt("Now convert it into python") # Second message
print(session.messages) # Messages in the current session in pair of <Human,AI>

```


Expand Down
4 changes: 2 additions & 2 deletions examples/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"]
with GPTChrome(
driver_config=driver_config,
email="[email protected]",
password="password",
email="", # for gpt we do not need email
password="", # # for gpt we do not need password
) as session: # A single session started with ChartGPT
data = session.send_prompt("""Write an SQL Query which shows how to get third highest salary
""") # First Message
Expand Down
58 changes: 6 additions & 52 deletions src/free_llms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,69 +177,23 @@ class GPTChrome(LLMChrome):

@property
def _model_url(self) -> str:
return "https://chatgpt.com/auth/login?sso="
return "https://chatgpt.com/"

@property
def _elements_identifier(self) -> Dict[str, str]:
return {
"Login": '//*[@id="__next"]/div[1]/div[2]/div[1]/div/div/button[1]', # noqa: E501
"Email": "username",
"Email_Continue": "action",
"Password": '//*[@id="password"]',
"Prompt_Text_Area": "prompt-textarea",
"Prompt_Text_Output": '//*[@id="__next"]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501
"Prompt_Text_Output": "/html/body/div[1]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div", # noqa: E501
}

def login(self, retries_attempt: int = 3) -> bool:
self.driver.get(self._model_url)
for i in range(retries_attempt):
self.run_manager.on_text(text=f"Making login attempt no. {i+1} on ChatGPT", verbose=self.verbosity)
try:
login_button = WebDriverWait(self.driver, self.waiting_time).until(
EC.element_to_be_clickable((By.XPATH, self._elements_identifier["Login"]))
)
login_button.click()
email_input = WebDriverWait(self.driver, self.waiting_time).until(
EC.presence_of_element_located((By.ID, self._elements_identifier["Email"]))
)
email_input.click()
email_input.send_keys(self.email)
continue_button = WebDriverWait(self.driver, self.waiting_time).until(
EC.presence_of_element_located((By.NAME, self._elements_identifier["Email_Continue"]))
)
continue_button.click()
password_button = WebDriverWait(self.driver, self.waiting_time).until(
EC.element_to_be_clickable((By.XPATH, self._elements_identifier["Password"]))
)
password_button.clear()
password_button.click()
password_button.send_keys(self.password)
password_button.submit()
self.run_manager.on_text(text=f"Login succeed on attempt no. {i+1}", verbose=self.verbosity)
return True
except TimeoutException:
continue
return False
return True

def send_prompt(self, query: str) -> AIMessage:
while True:
try:
text_area = WebDriverWait(self.driver, self.waiting_time).until(
EC.presence_of_element_located((By.ID, self._elements_identifier["Prompt_Text_Area"]))
)
break
except TimeoutException:
current_url = self.driver.current_url
self.driver.quit()
_chrome_version = os.environ.get("CHROME_VERSION", None)

self.driver = uc.Chrome(
options=configure_options(self.driver_config + DRIVERS_DEFAULT_CONFIG),
version_main=int(_chrome_version) if _chrome_version is not None else _chrome_version,
headless=True,
)
self.run_manager.on_text(text="Captacha Detected on ChatGPT. Starting Annoymous Session", verbose=self.verbosity)
self.driver.get(current_url)
text_area = WebDriverWait(self.driver, self.waiting_time).until(
EC.presence_of_element_located((By.ID, self._elements_identifier["Prompt_Text_Area"]))
)

text_area.click()
text_area.send_keys(query)
Expand Down
19 changes: 6 additions & 13 deletions tests/unit_tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@


def test_gpt_chrome():
with pytest.raises(ValueError, match="Cannot Login given the credentials"):
with GPTChrome(driver_config=[], email="wrong_email", password="wrong_password", retries_attempt=1) as session:
pass
assert GPTChrome(driver_config=[], email="wrong_email", password="wrong_password")._elements_identifier == {
"Login": '//*[@id="__next"]/div[1]/div[2]/div[1]/div/div/button[1]', # noqa: E501
"Email": "username",
"Email_Continue": "action",
"Password": '//*[@id="password"]',
"Prompt_Text_Area": "prompt-textarea",
"Prompt_Text_Output": '//*[@id="__next"]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501
}
assert GPTChrome(driver_config=[], email="wrong_email", password="wrong_password")._model_url == "https://chatgpt.com/auth/login?sso="
chrome_instance = GPTChrome(driver_config=[], email="wrong_email", password="wrong_password", retries_attempt=1)
assert GPTChrome(driver_config=[], email="", password="")._elements_identifier == {
"Prompt_Text_Area": "prompt-textarea",
"Prompt_Text_Output": '/html/body/div[1]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501
}
assert GPTChrome(driver_config=[], email="", password="")._model_url == "https://chatgpt.com/"
chrome_instance = GPTChrome(driver_config=[], email="", password="", retries_attempt=1)
chrome_instance.driver.get("https://chatgpt.com/")
ans = chrome_instance.send_prompt("How are you doing?")
assert isinstance(ans, AIMessage)
Expand Down

0 comments on commit 0479ddf

Please sign in to comment.