2023-09-03 08:26:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
import time
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
from ...typing import CreateResult, Messages
|
|
|
|
from ..base_provider import BaseProvider
|
2023-11-16 18:28:59 +00:00
|
|
|
from ..helper import WebDriver, format_prompt, get_browser
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
class Bard(BaseProvider):
|
2023-07-28 10:07:17 +00:00
|
|
|
url = "https://bard.google.com"
|
|
|
|
working = True
|
2023-11-16 17:10:19 +00:00
|
|
|
needs_auth = True
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-08-18 09:45:35 +00:00
|
|
|
@classmethod
|
2023-11-16 17:10:19 +00:00
|
|
|
def create_completion(
|
2023-08-18 09:45:35 +00:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 11:33:20 +00:00
|
|
|
messages: Messages,
|
2023-11-16 17:10:19 +00:00
|
|
|
stream: bool,
|
2023-08-18 09:45:35 +00:00
|
|
|
proxy: str = None,
|
2023-11-16 17:10:19 +00:00
|
|
|
browser: WebDriver = None,
|
2023-11-17 03:50:37 +00:00
|
|
|
user_data_dir: str = None,
|
|
|
|
headless: bool = True,
|
2023-08-27 23:43:45 +00:00
|
|
|
**kwargs
|
2023-11-16 17:10:19 +00:00
|
|
|
) -> CreateResult:
|
2023-08-27 23:43:45 +00:00
|
|
|
prompt = format_prompt(messages)
|
2023-11-17 03:50:37 +00:00
|
|
|
driver = browser if browser else get_browser(user_data_dir, headless, proxy)
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
2023-08-27 23:43:45 +00:00
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
try:
|
|
|
|
driver.get(f"{cls.url}/chat")
|
|
|
|
wait = WebDriverWait(driver, 10)
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
|
|
|
|
except:
|
|
|
|
# Reopen browser for login
|
|
|
|
if not browser:
|
|
|
|
driver.quit()
|
|
|
|
driver = get_browser(None, False, proxy)
|
|
|
|
driver.get(f"{cls.url}/chat")
|
|
|
|
wait = WebDriverWait(driver, 240)
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Prompt textarea not found. You may not be logged in.")
|
2023-10-23 07:46:25 +00:00
|
|
|
|
2023-11-16 17:10:19 +00:00
|
|
|
try:
|
|
|
|
# Add hook in XMLHttpRequest
|
|
|
|
script = """
|
|
|
|
const _http_request_open = XMLHttpRequest.prototype.open;
|
|
|
|
window._message = "";
|
|
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
|
|
|
if (url.includes("/assistant.lamda.BardFrontendService/StreamGenerate")) {
|
|
|
|
this.addEventListener("load", (event) => {
|
|
|
|
window._message = JSON.parse(JSON.parse(this.responseText.split("\\n")[3])[0][2])[4][0][1][0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return _http_request_open.call(this, method, url);
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
driver.execute_script(script)
|
2023-08-18 09:45:35 +00:00
|
|
|
|
2023-11-16 18:28:59 +00:00
|
|
|
# Input and submit prompt
|
2023-11-16 17:10:19 +00:00
|
|
|
driver.find_element(By.CSS_SELECTOR, "div.ql-editor.ql-blank.textarea").send_keys(prompt)
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "button.send-button").click()
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-11-16 18:28:59 +00:00
|
|
|
# Yield response
|
2023-11-16 17:10:19 +00:00
|
|
|
script = "return window._message;"
|
|
|
|
while True:
|
|
|
|
chunk = driver.execute_script(script)
|
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
time.sleep(0.1)
|
|
|
|
finally:
|
|
|
|
if not browser:
|
2023-11-17 03:50:37 +00:00
|
|
|
driver.close()
|
2023-11-16 17:10:19 +00:00
|
|
|
time.sleep(0.1)
|
2023-11-17 03:50:37 +00:00
|
|
|
driver.quit()
|