diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-11-18 12:24:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-18 12:24:39 +0100 |
commit | 2fcb3f949bbde7f5380acc68dc6b1f938dec12bc (patch) | |
tree | 035bd400b4057a744b29a0ebee96579bfbf429f6 /g4f/Provider/TalkAi.py | |
parent | ~ | g4f `v-0.1.8.5` (diff) | |
parent | Add AiChatOnline, ChatgptDemoAi, ChatgptNext Providers (diff) | |
download | gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.gz gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.bz2 gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.lz gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.xz gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.zst gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/TalkAi.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/g4f/Provider/TalkAi.py b/g4f/Provider/TalkAi.py new file mode 100644 index 00000000..5b03b91e --- /dev/null +++ b/g4f/Provider/TalkAi.py @@ -0,0 +1,94 @@ +from __future__ import annotations + +import time, json, time + +from ..typing import CreateResult, Messages +from .base_provider import BaseProvider +from .helper import WebDriver, get_browser + +class TalkAi(BaseProvider): + url = "https://talkai.info" + working = True + supports_gpt_35_turbo = True + supports_stream = True + + @classmethod + def create_completion( + cls, + model: str, + messages: Messages, + stream: bool, + proxy: str = None, + browser: WebDriver = None, + **kwargs + ) -> CreateResult: + driver = browser if browser else get_browser("", False, proxy) + + from selenium.webdriver.common.by import By + from selenium.webdriver.support.ui import WebDriverWait + from selenium.webdriver.support import expected_conditions as EC + + try: + driver.get(f"{cls.url}/chat/") + + # Wait for page load + WebDriverWait(driver, 240).until( + EC.presence_of_element_located((By.CSS_SELECTOR, "body.chat-page")) + ) + + data = { + "type": "chat", + "message": messages[-1]["content"], + "messagesHistory": [{ + "from": "you" if message["role"] == "user" else "chatGPT", + "content": message["content"] + } for message in messages], + "model": model if model else "gpt-3.5-turbo", + "max_tokens": 256, + "temperature": 1, + "top_p": 1, + "presence_penalty": 0, + "frequency_penalty": 0, + **kwargs + } + script = """ +const response = await fetch("/chat/send2/", { + "headers": { + "Accept": "application/json", + "Content-Type": "application/json", + }, + "body": {body}, + "method": "POST" +}); +window._reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); +""" + driver.execute_script( + script.replace("{body}", json.dumps(json.dumps(data))) + ) + # Read response + while True: + chunk = driver.execute_script(""" +chunk = await window._reader.read(); +if (chunk["done"]) { + return null; +} +content = ""; +lines = chunk["value"].split("\\n") +lines.forEach((line, index) => { + if (line.startsWith('data: ')) { + content += line.substring('data: '.length); + } +}); +return content; +""") + if chunk: + yield chunk.replace("\\n", "\n") + elif chunk != "": + break + else: + time.sleep(0.1) + finally: + if not browser: + driver.close() + time.sleep(0.1) + driver.quit()
\ No newline at end of file |