diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | gpt4free/README.md | 5 | ||||
-rw-r--r-- | gpt4free/__init__.py | 7 | ||||
-rw-r--r-- | gpt4free/cocalc/__init__.py | 67 | ||||
-rw-r--r-- | gpt4free/cocalc/readme.md | 19 | ||||
-rw-r--r-- | gpt4free/forefront/README.md | 13 | ||||
-rw-r--r-- | gpt4free/forefront/__init__.py | 91 | ||||
-rw-r--r-- | gpt4free/forefront/typing.py | 6 | ||||
-rw-r--r-- | gpt4free/you/__init__.py | 61 | ||||
-rw-r--r-- | requirements.txt | 5 |
10 files changed, 115 insertions, 163 deletions
@@ -1,4 +1,6 @@ -Due to legal and personal issues, the development speed of this Repository may slow down over the next one to two weeks. I apologize for any inconvenience this may cause. I have been putting a lot of effort into this small personal/educational project, and it is now on the verge of being taken down. +**The development speed of this Repository may slow down over the next one to two weeks !!** + +-- Due to legal and personal issues. I apologize for any inconvenience this may cause. I have been putting a lot of effort into this small personal/educational project, and it is now on the verge of being taken down. <p>You may join our discord: <a href="https://discord.com/invite/gpt4free">discord.gg/gpt4free<a> for further updates. <a href="https://discord.gg/gpt4free"><img align="center" alt="gpt4free Discord" width="22px" src="https://raw.githubusercontent.com/peterthehan/peterthehan/master/assets/discord.svg" /></a></p> diff --git a/gpt4free/README.md b/gpt4free/README.md index f3ba27ab..73e7fa09 100644 --- a/gpt4free/README.md +++ b/gpt4free/README.md @@ -42,9 +42,6 @@ print(f'END') response = gpt4free.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi') print(response) -# usage cocalc -response = gpt4free.Completion.create(Provider.CoCalc, prompt='Write a poem on Lionel Messi', cookie_input='') -print(response) ``` @@ -73,8 +70,6 @@ Some of the keyword arguments are optional, while others are required. - Theb: (no keyword arguments required) -- CoCalc: - - `cookie_input`: str - this needs to be provided by user #### Token generation of quora ```python diff --git a/gpt4free/__init__.py b/gpt4free/__init__.py index 6df778e3..1e652897 100644 --- a/gpt4free/__init__.py +++ b/gpt4free/__init__.py @@ -1,6 +1,5 @@ from enum import Enum -from gpt4free import cocalc from gpt4free import forefront from gpt4free import quora from gpt4free import theb @@ -15,7 +14,6 @@ class Provider(Enum): Poe = 'poe' ForeFront = 'fore_front' Theb = 'theb' - CoCalc = 'cocalc' UseLess = 'useless' @@ -40,8 +38,6 @@ class Completion: return Completion.__fore_front_service(prompt, **kwargs) elif provider == Provider.Theb: return Completion.__theb_service(prompt, **kwargs) - elif provider == Provider.CoCalc: - return Completion.__cocalc_service(prompt, **kwargs) elif provider == Provider.UseLess: return Completion.__useless_service(prompt, **kwargs) else: @@ -67,6 +63,3 @@ class Completion: def __theb_service(prompt: str, **kwargs): return ''.join(theb.Completion.create(prompt=prompt)) - @staticmethod - def __cocalc_service(prompt: str, **kwargs): - return cocalc.Completion.create(prompt, cookie_input=kwargs.get('cookie_input', '')).text diff --git a/gpt4free/cocalc/__init__.py b/gpt4free/cocalc/__init__.py deleted file mode 100644 index e122051a..00000000 --- a/gpt4free/cocalc/__init__.py +++ /dev/null @@ -1,67 +0,0 @@ -import requests
-from fake_useragent import UserAgent
-from pydantic import BaseModel
-
-
-class CoCalcResponse(BaseModel):
- text: str
- status: bool
-
-
-class Completion:
- """A class for generating text completions using CoCalc's GPT-based chatbot."""
-
- API_ENDPOINT = "https://cocalc.com/api/v2/openai/chatgpt"
- DEFAULT_SYSTEM_PROMPT = "ASSUME I HAVE FULL ACCESS TO COCALC. "
-
- @staticmethod
- def create(prompt: str, cookie_input: str) -> CoCalcResponse:
- """
- Generate a text completion for the given prompt using CoCalc's GPT-based chatbot.
-
- Args:
- prompt: The text prompt to complete.
- cookie_input: The cookie required to authenticate the chatbot API request.
-
- Returns:
- A CoCalcResponse object containing the text completion and a boolean indicating
- whether the request was successful.
- """
-
- # Initialize a session with custom headers
- session = Completion._initialize_session(cookie_input)
-
- # Set the data that will be submitted
- payload = Completion._create_payload(prompt, Completion.DEFAULT_SYSTEM_PROMPT)
-
- try:
- # Submit the request and return the results
- response = session.post(Completion.API_ENDPOINT, json=payload).json()
- return CoCalcResponse(text=response['output'], status=response['success'])
- except requests.exceptions.RequestException as e:
- # Handle exceptions that may occur during the request
- print(f"Error: {e}")
- return CoCalcResponse(text="", status=False)
-
- @classmethod
- def _initialize_session(cls, conversation_cookie: str) -> requests.Session:
- """Initialize a session with custom headers for the request."""
-
- session = requests.Session()
- headers = {
- "Accept": "*/*",
- "Accept-Language": "en-US,en;q=0.5",
- "Origin": "https://cocalc.com",
- "Referer": "https://cocalc.com/api/v2/openai/chatgpt",
- "Cookie": conversation_cookie,
- "User-Agent": UserAgent().random,
- }
- session.headers.update(headers)
-
- return session
-
- @staticmethod
- def _create_payload(prompt: str, system_prompt: str) -> dict:
- """Create the payload for the API request."""
-
- return {"input": prompt, "system": system_prompt, "tag": "next:index"}
diff --git a/gpt4free/cocalc/readme.md b/gpt4free/cocalc/readme.md deleted file mode 100644 index f0911155..00000000 --- a/gpt4free/cocalc/readme.md +++ /dev/null @@ -1,19 +0,0 @@ -### Example: `cocalc` <a name="example-cocalc"></a> - -```python -# import library -from gpt4free import cocalc - -cocalc.Completion.create(prompt="How are you!", cookie_input="cookieinput") ## Tutorial -``` - -### How to grab cookie input -```js -// input this into ur developer tools console and the exact response u get from this u put into ur cookieInput! -var cookies = document.cookie.split("; "); -var cookieString = ""; -for (var i = 0; i < cookies.length; i++) { - cookieString += cookies[i] + "; "; -} -console.log(cookieString); -``` diff --git a/gpt4free/forefront/README.md b/gpt4free/forefront/README.md index 887097ec..7a59fe8e 100644 --- a/gpt4free/forefront/README.md +++ b/gpt4free/forefront/README.md @@ -2,15 +2,18 @@ ```python from gpt4free import forefront + + # create an account -token = forefront.Account.create(logging=False) -print(token) +account_data = forefront.Account.create(logging=False) + # get a response for response in forefront.StreamingCompletion.create( - token=token, - prompt='hello world', - model='gpt-4' + account_data=account_data, + prompt='hello world', + model='gpt-4' ): print(response.choices[0].text, end='') print("") + ```
\ No newline at end of file diff --git a/gpt4free/forefront/__init__.py b/gpt4free/forefront/__init__.py index dbf1730b..240ee0a4 100644 --- a/gpt4free/forefront/__init__.py +++ b/gpt4free/forefront/__init__.py @@ -1,27 +1,31 @@ +import hashlib +from base64 import b64encode from json import loads -from xtempmail import Email from re import findall -from typing import Optional, Generator -from faker import Faker from time import time, sleep +from typing import Generator, Optional from uuid import uuid4 + +from Crypto.Cipher import AES +from Crypto.Random import get_random_bytes from fake_useragent import UserAgent +from mailgw_temporary_email import Email from requests import post from tls_client import Session -from .typing import ForeFrontResponse + +from .typing import ForeFrontResponse, AccountData class Account: @staticmethod - def create(proxy: Optional[str] = None, logging: bool = False): + def create(proxy: Optional[str] = None, logging: bool = False) -> AccountData: proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False - faker = Faker() - name = (faker.name().replace(' ', '_')).lower() start = time() - mail_client = Email(name=name) - mail_address = mail_client.email + mail_client = Email() + mail_client.register() + mail_address = mail_client.address client = Session(client_identifier='chrome110') client.proxies = proxies @@ -40,7 +44,7 @@ class Account: if logging: print(trace_token) except KeyError: - return 'Failed to create account!' + raise RuntimeError('Failed to create account!') response = client.post( f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.38.4', @@ -54,23 +58,26 @@ class Account: print(response.text) if 'sign_up_attempt' not in response.text: - return 'Failed to create account!' - verification_url = None - new_message = mail_client.get_new_message(5) - for msg in new_message: - verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', msg.text)[0] + raise RuntimeError('Failed to create account!') + + while True: + sleep(5) + message_id = mail_client.message_list()[0]['id'] + message = mail_client.message(message_id) + verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', message["text"])[0] if verification_url: break - - if verification_url is None or not verification_url: - raise RuntimeError('Error while obtaining verfication URL!') + if logging: print(verification_url) - response = client.get(verification_url) + client.get(verification_url) - response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4') + response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4').json() + session_data = response['response']['sessions'][0] - token = response.json()['response']['sessions'][0]['last_active_token']['jwt'] + user_id = session_data['user']['id'] + session_id = session_data['id'] + token = session_data['last_active_token']['jwt'] with open('accounts.txt', 'a') as f: f.write(f'{mail_address}:{token}\n') @@ -78,32 +85,32 @@ class Account: if logging: print(time() - start) - return token + return AccountData(token=token, user_id=user_id, session_id=session_id) class StreamingCompletion: @staticmethod def create( - token=None, + prompt: str, + account_data: AccountData, chat_id=None, - prompt='', action_type='new', default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default model='gpt-4', proxy=None ) -> Generator[ForeFrontResponse, None, None]: - if not token: - raise Exception('Token is required!') + token = account_data.token if not chat_id: chat_id = str(uuid4()) - proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else None + proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None + base64_data = b64encode((account_data.user_id + default_persona + chat_id).encode()).decode() + encrypted_signature = StreamingCompletion.__encrypt(base64_data, account_data.session_id) headers = { 'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com', 'accept': '*/*', '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', - 'authorization': 'Bearer ' + token, 'cache-control': 'no-cache', 'content-type': 'application/json', 'origin': 'https://chat.forefront.ai', @@ -115,6 +122,8 @@ class StreamingCompletion: 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'cross-site', + 'authorization': f"Bearer {token}", + 'X-Signature': encrypted_signature, 'user-agent': UserAgent().random, } @@ -128,7 +137,7 @@ class StreamingCompletion: } for chunk in post( - 'https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat', + 'https://streaming.tenant-forefront-default.knative.chi.coreweave.com/chat', headers=headers, proxies=proxies, json=json_data, @@ -155,13 +164,28 @@ class StreamingCompletion: } ) + @staticmethod + def __encrypt(data: str, key: str) -> str: + hash_key = hashlib.sha256(key.encode()).digest() + iv = get_random_bytes(16) + cipher = AES.new(hash_key, AES.MODE_CBC, iv) + encrypted_data = cipher.encrypt(StreamingCompletion.__pad_data(data.encode())) + return iv.hex() + encrypted_data.hex() + + @staticmethod + def __pad_data(data: bytes) -> bytes: + block_size = AES.block_size + padding_size = block_size - len(data) % block_size + padding = bytes([padding_size] * padding_size) + return data + padding + class Completion: @staticmethod def create( - token=None, + prompt: str, + account_data: AccountData, chat_id=None, - prompt='', action_type='new', default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default model='gpt-4', @@ -170,7 +194,7 @@ class Completion: text = '' final_response = None for response in StreamingCompletion.create( - token=token, + account_data=account_data, chat_id=chat_id, prompt=prompt, action_type=action_type, @@ -185,7 +209,6 @@ class Completion: if final_response: final_response.text = text else: - raise Exception('Unable to get the response, Please try again') + raise RuntimeError('Unable to get the response, Please try again') return final_response -
\ No newline at end of file diff --git a/gpt4free/forefront/typing.py b/gpt4free/forefront/typing.py index 23e90903..b572e2c2 100644 --- a/gpt4free/forefront/typing.py +++ b/gpt4free/forefront/typing.py @@ -24,3 +24,9 @@ class ForeFrontResponse(BaseModel): choices: List[Choice] usage: Usage text: str + + +class AccountData(BaseModel): + token: str + user_id: str + session_id: str diff --git a/gpt4free/you/__init__.py b/gpt4free/you/__init__.py index da22d05e..11847fb5 100644 --- a/gpt4free/you/__init__.py +++ b/gpt4free/you/__init__.py @@ -5,10 +5,13 @@ from uuid import uuid4 from fake_useragent import UserAgent from pydantic import BaseModel +from requests import RequestException +from retrying import retry from tls_client import Session +from tls_client.response import Response -class PoeResponse(BaseModel): +class YouResponse(BaseModel): text: Optional[str] = None links: List[str] = [] extra: Dict[str, Any] = {} @@ -31,7 +34,7 @@ class Completion: detailed: bool = False, debug: bool = False, proxy: Optional[str] = None, - ) -> PoeResponse: + ) -> YouResponse: if chat is None: chat = [] @@ -41,30 +44,29 @@ class Completion: client.headers = Completion.__get_headers() client.proxies = proxies - 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':' ''} - }, - ) + 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':' ''} + } + + try: + response = Completion.__make_request(client, params) + except Exception: + return Completion.__get_failure_response() 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() @@ -80,7 +82,7 @@ class Completion: # 'slots' : loads(slots) } - response = PoeResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"')) + response = YouResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"')) if include_links: response.links = json.loads(third_party_search_results)['search']['third_party_search_results'] @@ -108,5 +110,18 @@ class Completion: } @staticmethod - def __get_failure_response() -> PoeResponse: - return PoeResponse(text='Unable to fetch the response, Please try again.') + def __get_failure_response() -> YouResponse: + return YouResponse(text='Unable to fetch the response, Please try again.') + + @staticmethod + @retry( + wait_fixed=5000, + stop_max_attempt_number=5, + retry_on_exception=lambda e: isinstance(e, RequestException), + ) + def __make_request(client: Session, params: dict) -> Response: + response = client.get(f'https://you.com/api/streamingSearch', params=params) + if 'youChatToken' not in response.text: + print('retry') + raise RequestException('Unable to get the response from server') + return response diff --git a/requirements.txt b/requirements.txt index 17d5d4b5..940ba42f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,6 @@ https://github.com/AI-Yash/st-chat/archive/refs/pull/24/head.zip pydantic pymailtm Levenshtein -xtempmail -faker +retrying +mailgw_temporary_email +pycryptodome |