summaryrefslogtreecommitdiffstats
path: root/you
diff options
context:
space:
mode:
authornoptuno <repollo.marrero@gmail.com>2023-04-28 12:33:51 +0200
committernoptuno <repollo.marrero@gmail.com>2023-04-28 12:33:51 +0200
commit396d7e11b2623cf05c3b0fd4ff9b6cce667835fd (patch)
tree4d7f4531e082deba4fb6bac7e5b221d887e066d9 /you
parentRemove ignored files from repository (diff)
parentMerge pull request #241 from ethanx40/main (diff)
downloadgpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar.gz
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar.bz2
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar.lz
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar.xz
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.tar.zst
gpt4free-396d7e11b2623cf05c3b0fd4ff9b6cce667835fd.zip
Diffstat (limited to 'you')
-rw-r--r--you/README.md37
-rw-r--r--you/__init__.py98
2 files changed, 135 insertions, 0 deletions
diff --git a/you/README.md b/you/README.md
new file mode 100644
index 00000000..25c20085
--- /dev/null
+++ b/you/README.md
@@ -0,0 +1,37 @@
+### Example: `you` (use like openai pypi package) <a name="example-you"></a>
+
+```python
+import you
+
+# simple request with links and details
+response = you.Completion.create(
+ prompt="hello world",
+ detailed=True,
+ include_links=True, )
+
+print(response)
+
+# {
+# "response": "...",
+# "links": [...],
+# "extra": {...},
+# "slots": {...}
+# }
+# }
+
+# chatbot
+
+chat = []
+
+while True:
+ prompt = input("You: ")
+ if prompt == 'q':
+ break
+ response = you.Completion.create(
+ prompt=prompt,
+ chat=chat)
+
+ print("Bot:", response["response"])
+
+ chat.append({"question": prompt, "answer": response["response"]})
+```
diff --git a/you/__init__.py b/you/__init__.py
new file mode 100644
index 00000000..8bf31f0d
--- /dev/null
+++ b/you/__init__.py
@@ -0,0 +1,98 @@
+import re
+from json import loads
+from uuid import uuid4
+
+from fake_useragent import UserAgent
+from tls_client import Session
+
+
+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:
+ if chat is None:
+ chat = []
+
+ client = Session(client_identifier='chrome_108')
+ client.headers = Completion.__get_headers()
+
+ response = client.get(
+ f'https://you.com/api/streamingSearch',
+ params={
+ 'q': prompt,
+ 'page': page,
+ 'count': count,
+ 'safeSearch': safe_search,
+ 'onShoppingPage': on_shopping_page,
+ 'mkt': mkt,
+ 'responseFilter': response_filter,
+ 'domain': domain,
+ 'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
+ 'chat': str(chat), # {'question':'','answer':' ''}
+ },
+ )
+
+ if debug:
+ print('\n\n------------------\n\n')
+ print(response.text)
+ print('\n\n------------------\n\n')
+
+ if 'youChatToken' not in response.text:
+ return Completion.__get_failure_response()
+
+ you_chat_serp_results = re.search(
+ r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
+ ).group()
+ third_party_search_results = re.search(
+ 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),
+ # '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,
+ }
+
+ @classmethod
+ def __get_headers(cls) -> dict:
+ return {
+ 'authority': 'you.com',
+ 'accept': 'text/event-stream',
+ 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
+ 'cache-control': 'no-cache',
+ 'referer': 'https://you.com/search?q=who+are+you&tbm=youchat',
+ 'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
+ 'sec-ch-ua-mobile': '?0',
+ 'sec-ch-ua-platform': '"Windows"',
+ 'sec-fetch-dest': 'empty',
+ 'sec-fetch-mode': 'cors',
+ 'sec-fetch-site': 'same-origin',
+ 'cookie': f'safesearch_guest=Moderate; uuid_guest={str(uuid4())}',
+ 'user-agent': UserAgent().random,
+ }
+
+ @classmethod
+ def __get_failure_response(cls) -> dict:
+ return dict(response='Unable to fetch the response, Please try again.', links=[], extra={})