diff options
author | Raju Komati <komatiraju032@gmail.com> | 2023-04-29 11:25:24 +0200 |
---|---|---|
committer | Raju Komati <komatiraju032@gmail.com> | 2023-04-29 11:25:24 +0200 |
commit | 54b4c789a75fec5c3a92a03dfd2d93d051309651 (patch) | |
tree | 813a7dd223e9f7df3b1708fc65a58ccc87684ba6 /you/__init__.py | |
parent | Update README.md (diff) | |
download | gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar.gz gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar.bz2 gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar.lz gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar.xz gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.tar.zst gpt4free-54b4c789a75fec5c3a92a03dfd2d93d051309651.zip |
Diffstat (limited to '')
-rw-r--r-- | gpt4free/you/__init__.py (renamed from you/__init__.py) | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/you/__init__.py b/gpt4free/you/__init__.py index 8bf31f0d..97b48464 100644 --- a/you/__init__.py +++ b/gpt4free/you/__init__.py @@ -1,28 +1,36 @@ +import json import re -from json import loads +from typing import Optional, List, Dict, Any from uuid import uuid4 from fake_useragent import UserAgent +from pydantic import BaseModel from tls_client import Session +class PoeResponse(BaseModel): + text: Optional[str] = None + links: List[str] = [] + extra: Dict[str, Any] = {} + + class Completion: @staticmethod def create( - prompt: str, - page: int = 1, - count: int = 10, - safe_search: str = 'Moderate', - on_shopping_page: bool = False, - mkt: str = '', - response_filter: str = 'WebPages,Translations,TimeZone,Computation,RelatedSearches', - domain: str = 'youchat', - query_trace_id: str = None, - chat: list = None, - include_links: bool = False, - detailed: bool = False, - debug: bool = False, - ) -> dict: + prompt: str, + page: int = 1, + count: int = 10, + safe_search: str = 'Moderate', + on_shopping_page: bool = False, + mkt: str = '', + response_filter: str = 'WebPages,Translations,TimeZone,Computation,RelatedSearches', + domain: str = 'youchat', + query_trace_id: str = None, + chat: list = None, + include_links: bool = False, + detailed: bool = False, + debug: bool = False, + ) -> PoeResponse: if chat is None: chat = [] @@ -57,26 +65,28 @@ class Completion: r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text ).group() third_party_search_results = re.search( - r'(?<=event: thirdPartySearchResults\ndata:)(.*\n)*?(?=event: )', response.text).group() + r'(?<=event: thirdPartySearchResults\ndata:)(.*\n)*?(?=event: )', response.text + ).group() # slots = findall(r"slots\ndata: (.*)\n\nevent", response.text)[0] text = ''.join(re.findall(r'{\"youChatToken\": \"(.*?)\"}', response.text)) extra = { - 'youChatSerpResults': loads(you_chat_serp_results), + 'youChatSerpResults': json.loads(you_chat_serp_results), # 'slots' : loads(slots) } - return { - 'response': text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'), - 'links': loads(third_party_search_results)['search']['third_party_search_results'] - if include_links - else None, - 'extra': extra if detailed else None, - } + response = PoeResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"')) + if include_links: + response.links = json.loads(third_party_search_results)['search']['third_party_search_results'] - @classmethod - def __get_headers(cls) -> dict: + if detailed: + response.extra = extra + + return response + + @staticmethod + def __get_headers() -> dict: return { 'authority': 'you.com', 'accept': 'text/event-stream', @@ -93,6 +103,6 @@ class Completion: 'user-agent': UserAgent().random, } - @classmethod - def __get_failure_response(cls) -> dict: - return dict(response='Unable to fetch the response, Please try again.', links=[], extra={}) + @staticmethod + def __get_failure_response() -> PoeResponse: + return PoeResponse(text='Unable to fetch the response, Please try again.') |