From 920fe19608ba06ed8c2b4c9a23944af35cf24e56 Mon Sep 17 00:00:00 2001 From: Raju Komati Date: Fri, 28 Apr 2023 00:40:43 +0530 Subject: added main module for accessing all services --- openai_rev/forefront/README.md | 15 ++++ openai_rev/forefront/__init__.py | 180 +++++++++++++++++++++++++++++++++++++++ openai_rev/forefront/mail.py | 52 +++++++++++ openai_rev/forefront/models.py | 26 ++++++ 4 files changed, 273 insertions(+) create mode 100644 openai_rev/forefront/README.md create mode 100644 openai_rev/forefront/__init__.py create mode 100644 openai_rev/forefront/mail.py create mode 100644 openai_rev/forefront/models.py (limited to 'openai_rev/forefront') diff --git a/openai_rev/forefront/README.md b/openai_rev/forefront/README.md new file mode 100644 index 00000000..94089faa --- /dev/null +++ b/openai_rev/forefront/README.md @@ -0,0 +1,15 @@ +### Example: `forefront` (use like openai pypi package) + +```python + +from openai_rev import forefront + +# create an account +token = forefront.Account.create(logging=True) +print(token) + +# get a response +for response in forefront.StreamingCompletion.create(token=token, + prompt='hello world', model='gpt-4'): + print(response.completion.choices[0].text, end='') +``` \ No newline at end of file diff --git a/openai_rev/forefront/__init__.py b/openai_rev/forefront/__init__.py new file mode 100644 index 00000000..bef10e9e --- /dev/null +++ b/openai_rev/forefront/__init__.py @@ -0,0 +1,180 @@ +from json import loads +from re import match +from time import time, sleep +from uuid import uuid4 + +from altair.vegalite.v3 import Generator +from fake_useragent import UserAgent +from requests import post +from tls_client import Session + +from .mail import Mail +from .models import ForeFrontResponse + + +class Account: + @staticmethod + def create(proxy=None, logging=False): + proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False + + start = time() + + mail_client = Mail(proxies) + mail_token = None + mail_address = mail_client.get_mail() + + # print(mail_address) + + client = Session(client_identifier='chrome110') + client.proxies = proxies + client.headers = { + 'origin': 'https://accounts.forefront.ai', + 'user-agent': UserAgent().random, + } + + response = client.post( + 'https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.32.6', + data={'email_address': mail_address}, + ) + + trace_token = response.json()['response']['id'] + if logging: + print(trace_token) + + response = client.post( + f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.32.6', + data={ + 'strategy': 'email_code', + }, + ) + + if logging: + print(response.text) + + if 'sign_up_attempt' not in response.text: + return 'Failed to create account!' + + while True: + sleep(1) + for _ in mail_client.fetch_inbox(): + print(mail_client.get_message_content(_['id'])) + mail_token = match(r'(\d){5,6}', mail_client.get_message_content(_['id'])).group(0) + + if mail_token: + break + + if logging: + print(mail_token) + + response = client.post( + f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/attempt_verification?_clerk_js_version=4.38.4', + data={'code': mail_token, 'strategy': 'email_code'}, + ) + + if logging: + print(response.json()) + + token = response.json()['client']['sessions'][0]['last_active_token']['jwt'] + + with open('accounts.txt', 'a') as f: + f.write(f'{mail_address}:{token}\n') + + if logging: + print(time() - start) + + return token + + +class StreamingCompletion: + @staticmethod + def create( + token=None, + chat_id=None, + prompt='', + action_type='new', + default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default + model='gpt-4', + ) -> Generator[ForeFrontResponse, None, None]: + if not token: + raise Exception('Token is required!') + if not chat_id: + chat_id = str(uuid4()) + + 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', + 'pragma': 'no-cache', + 'referer': 'https://chat.forefront.ai/', + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'cross-site', + 'user-agent': UserAgent().random, + } + + json_data = { + 'text': prompt, + 'action': action_type, + 'parentId': chat_id, + 'workspaceId': chat_id, + 'messagePersona': default_persona, + 'model': model, + } + + for chunk in post( + 'https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat', + headers=headers, + json=json_data, + stream=True, + ).iter_lines(): + if b'finish_reason":null' in chunk: + data = loads(chunk.decode('utf-8').split('data: ')[1]) + token = data['choices'][0]['delta'].get('content') + + if token is not None: + yield ForeFrontResponse( + **{ + 'id': chat_id, + 'object': 'text_completion', + 'created': int(time()), + 'text': token, + 'model': model, + 'choices': [{'text': token, 'index': 0, 'logprobs': None, 'finish_reason': 'stop'}], + 'usage': { + 'prompt_tokens': len(prompt), + 'completion_tokens': len(token), + 'total_tokens': len(prompt) + len(token), + }, + } + ) + + +class Completion: + @staticmethod + def create( + token=None, + chat_id=None, + prompt='', + action_type='new', + default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default + model='gpt-4', + ) -> ForeFrontResponse: + final_response = None + for response in StreamingCompletion.create( + token=token, + chat_id=chat_id, + prompt=prompt, + action_type=action_type, + default_persona=default_persona, + model=model, + ): + final_response = response + + return final_response diff --git a/openai_rev/forefront/mail.py b/openai_rev/forefront/mail.py new file mode 100644 index 00000000..2c00051c --- /dev/null +++ b/openai_rev/forefront/mail.py @@ -0,0 +1,52 @@ +from random import choices +from string import ascii_letters + +from requests import Session + + +class Mail: + def __init__(self, proxies: dict = None) -> None: + self.client = Session() + self.client.proxies = proxies + self.client.headers = { + "host": "api.mail.tm", + "connection": "keep-alive", + "sec-ch-ua": "\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"", + "accept": "application/json, text/plain, */*", + "content-type": "application/json", + "sec-ch-ua-mobile": "?0", + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36", + "sec-ch-ua-platform": "\"macOS\"", + "origin": "https://mail.tm", + "sec-fetch-site": "same-site", + "sec-fetch-mode": "cors", + "sec-fetch-dest": "empty", + "referer": "https://mail.tm/", + "accept-encoding": "gzip, deflate, br", + "accept-language": "en-GB,en-US;q=0.9,en;q=0.8", + } + + def get_mail(self) -> str: + token = ''.join(choices(ascii_letters, k=14)).lower() + init = self.client.post( + "https://api.mail.tm/accounts", json={"address": f"{token}@bugfoo.com", "password": token} + ) + + if init.status_code == 201: + resp = self.client.post("https://api.mail.tm/token", json={**init.json(), "password": token}) + + self.client.headers['authorization'] = 'Bearer ' + resp.json()['token'] + + return f"{token}@bugfoo.com" + + else: + raise Exception("Failed to create email") + + def fetch_inbox(self): + return self.client.get(f"https://api.mail.tm/messages").json()["hydra:member"] + + def get_message(self, message_id: str): + return self.client.get(f"https://api.mail.tm/messages/{message_id}").json() + + def get_message_content(self, message_id: str): + return self.get_message(message_id)["text"] diff --git a/openai_rev/forefront/models.py b/openai_rev/forefront/models.py new file mode 100644 index 00000000..23e90903 --- /dev/null +++ b/openai_rev/forefront/models.py @@ -0,0 +1,26 @@ +from typing import Any, List + +from pydantic import BaseModel + + +class Choice(BaseModel): + text: str + index: int + logprobs: Any + finish_reason: str + + +class Usage(BaseModel): + prompt_tokens: int + completion_tokens: int + total_tokens: int + + +class ForeFrontResponse(BaseModel): + id: str + object: str + created: int + model: str + choices: List[Choice] + usage: Usage + text: str -- cgit v1.2.3