summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Phind.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/Provider/Phind.py')
-rw-r--r--g4f/Provider/Phind.py143
1 files changed, 57 insertions, 86 deletions
diff --git a/g4f/Provider/Phind.py b/g4f/Provider/Phind.py
index 82769ab0..9d2acc75 100644
--- a/g4f/Provider/Phind.py
+++ b/g4f/Provider/Phind.py
@@ -1,103 +1,74 @@
from __future__ import annotations
-import time
-from urllib.parse import quote
+from datetime import datetime
-from ..typing import CreateResult, Messages
-from .base_provider import BaseProvider
-from .helper import format_prompt
-from .webdriver import WebDriver, WebDriverSession
+from ..typing import AsyncResult, Messages
+from .base_provider import AsyncGeneratorProvider
+from ..requests import StreamSession
-class Phind(BaseProvider):
+class Phind(AsyncGeneratorProvider):
url = "https://www.phind.com"
working = True
supports_gpt_4 = True
supports_stream = True
+ supports_message_history = True
@classmethod
- def create_completion(
+ async def create_async_generator(
cls,
model: str,
messages: Messages,
- stream: bool,
proxy: str = None,
timeout: int = 120,
- webdriver: WebDriver = None,
- creative_mode: bool = None,
+ creative_mode: bool = False,
**kwargs
- ) -> CreateResult:
- with WebDriverSession(webdriver, "", proxy=proxy) as driver:
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
-
- prompt = quote(format_prompt(messages))
- driver.get(f"{cls.url}/search?q={prompt}&source=searchbox")
-
- # Register fetch hook
- source = """
-window._fetch = window.fetch;
-window.fetch = async (url, options) => {
- const response = await window._fetch(url, options);
- if (url != "/api/infer/answer") {
- return response;
- }
- copy = response.clone();
- window._reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
- return copy;
-}
-"""
- driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
- "source": source
- })
-
- # Need to change settings
- wait = WebDriverWait(driver, timeout)
- def open_dropdown():
- # Open settings dropdown
- wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.text-dark.dropdown-toggle")))
- driver.find_element(By.CSS_SELECTOR, "button.text-dark.dropdown-toggle").click()
- # Wait for dropdown toggle
- wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='GPT-4']")))
- if model.startswith("gpt-4") or creative_mode:
- # Enable GPT-4
- if model.startswith("gpt-4"):
- open_dropdown()
- driver.find_element(By.XPATH, "//button[text()='GPT-4']").click()
- # Enable creative mode
- if creative_mode or creative_mode == None:
- open_dropdown()
- driver.find_element(By.ID, "Creative Mode").click()
- # Submit changes
- driver.find_element(By.CSS_SELECTOR, ".search-bar-input-group button[type='submit']").click()
- # Wait for page reload
- wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-container")))
-
- while True:
- chunk = driver.execute_script("""
-if(window._reader) {
- chunk = await window._reader.read();
- if (chunk['done']) {
- return null;
- }
- content = '';
- chunk['value'].split('\\r\\n').forEach((line, index) => {
- if (line.startsWith('data: ')) {
- line = line.substring('data: '.length);
- if (!line.startsWith('<PHIND_METADATA>')) {
- if (line) content += line;
- else content += '\\n';
- }
+ ) -> AsyncResult:
+ headers = {
+ "Accept": "*/*",
+ "Origin": cls.url,
+ "Referer": f"{cls.url}/search",
+ "Sec-Fetch-Dest": "empty",
+ "Sec-Fetch-Mode": "cors",
+ "Sec-Fetch-Site": "same-origin",
}
- });
- return content.replace('\\n\\n', '\\n');
-} else {
- return ''
-}
-""")
- if chunk:
- yield chunk
- elif chunk != "":
- break
- else:
- time.sleep(0.1) \ No newline at end of file
+ async with StreamSession(
+ impersonate="chrome110",
+ proxies={"https": proxy},
+ timeout=timeout
+ ) as session:
+ prompt = messages[-1]["content"]
+ data = {
+ "question": prompt,
+ "questionHistory": [
+ message["content"] for message in messages[:-1] if message["role"] == "user"
+ ],
+ "answerHistory": [
+ message["content"] for message in messages if message["role"] == "assistant"
+ ],
+ "webResults": [],
+ "options": {
+ "date": datetime.now().strftime("%d.%m.%Y"),
+ "language": "en-US",
+ "detailed": True,
+ "anonUserId": "",
+ "answerModel": "GPT-4" if model.startswith("gpt-4") else "Phind Model",
+ "creativeMode": creative_mode,
+ "customLinks": []
+ },
+ "context": "",
+ "rewrittenQuestion": prompt
+ }
+ async with session.post(f"{cls.url}/api/infer/followup/answer", headers=headers, json=data) as response:
+ new_line = False
+ async for line in response.iter_lines():
+ if line.startswith(b"data: "):
+ chunk = line[6:]
+ if chunk.startswith(b"<PHIND_METADATA>"):
+ pass
+ elif chunk:
+ yield chunk.decode()
+ elif new_line:
+ yield "\n"
+ new_line = False
+ else:
+ new_line = True \ No newline at end of file