forked from xtekky/gpt4free
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
592 additions
and
560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,57 @@ | ||
import requests | ||
|
||
|
||
class Completion: | ||
def create(self, prompt="What is the square root of pi", | ||
system_prompt=("ASSUME I HAVE FULL ACCESS TO COCALC. ENCLOSE MATH IN $. " | ||
"INCLUDE THE LANGUAGE DIRECTLY AFTER THE TRIPLE BACKTICKS " | ||
"IN ALL MARKDOWN CODE BLOCKS. How can I do the following using CoCalc?")) -> str: | ||
# Initialize a session with custom headers | ||
session = self._initialize_session() | ||
|
||
# Set the data that will be submitted | ||
payload = self._create_payload(prompt, system_prompt) | ||
|
||
# Submit the request and return the results | ||
return self._submit_request(session, payload) | ||
|
||
def _initialize_session(self) -> 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', | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36', | ||
} | ||
session.headers.update(headers) | ||
|
||
return session | ||
|
||
def _create_payload(self, prompt: str, system_prompt: str) -> dict: | ||
"""Create the payload with the given prompts.""" | ||
|
||
return { | ||
"input": prompt, | ||
"system": system_prompt, | ||
"tag": "next:index" | ||
} | ||
|
||
def _submit_request(self, session: requests.Session, payload: dict) -> str: | ||
"""Submit the request to the API and return the response.""" | ||
|
||
response = session.post( | ||
"https://cocalc.com/api/v2/openai/chatgpt", json=payload).json() | ||
return response | ||
import requests | ||
|
||
class Completion: | ||
@staticmethod | ||
def create(prompt:str, cookieInput:str) -> str: | ||
# Initialize a session with custom headers | ||
session = Completion._initialize_session(cookieInput) | ||
|
||
# Set the data that will be submitted | ||
payload = Completion._create_payload(prompt, ("ASSUME I HAVE FULL ACCESS TO COCALC. ")) | ||
|
||
# Submit the request and return the results | ||
return Completion._submit_request(session, payload) | ||
|
||
@classmethod | ||
def _initialize_session(cls, conversationCookie) -> 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': conversationCookie, | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36', | ||
} | ||
session.headers.update(headers) | ||
|
||
return session | ||
|
||
@classmethod | ||
def _create_payload( | ||
cls, | ||
prompt: str, | ||
system_prompt: str | ||
) -> dict: | ||
|
||
return { | ||
"input": prompt, | ||
"system": system_prompt, | ||
"tag": "next:index" | ||
} | ||
|
||
@classmethod | ||
def _submit_request( | ||
cls, | ||
session: requests.Session, | ||
payload: dict | ||
) -> str: | ||
|
||
response = session.post( | ||
"https://cocalc.com/api/v2/openai/chatgpt", json=payload).json() | ||
return { | ||
"response":response["output"], | ||
"success":response["success"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
### Example: `cocalc` <a name="example-cocalc"></a> | ||
|
||
|
||
```python | ||
# import library | ||
import cocalc | ||
|
||
cocalc.Completion.create(prompt="How are you!", cookieInput="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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### Example: `forefront` (use like openai pypi package) <a name="example-forefront"></a> | ||
|
||
```python | ||
import forefront | ||
|
||
# create an account | ||
token = forefront.Account.create(logging=False) | ||
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 = '') | ||
print("") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
from json import loads | ||
from re import match | ||
from time import time, sleep | ||
from uuid import uuid4 | ||
|
||
from requests import post | ||
from tls_client import Session | ||
|
||
from forefront.mail import Mail | ||
from forefront.typing 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 = Mail(proxies) | ||
mail_token = None | ||
mail_adress = mail.get_mail() | ||
|
||
# print(mail_adress) | ||
|
||
client = Session(client_identifier='chrome110') | ||
client.proxies = proxies | ||
client.headers = { | ||
"origin": "https://accounts.forefront.ai", | ||
"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", | ||
} | ||
|
||
response = client.post('https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.32.6', | ||
data={ | ||
"email_address": mail_adress | ||
} | ||
) | ||
try: | ||
trace_token = response.json()['response']['id'] | ||
if logging: print(trace_token) | ||
except KeyError: | ||
return 'Failed to create account!' | ||
|
||
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 not 'sign_up_attempt' in response.text: | ||
return 'Failed to create account!' | ||
|
||
while True: | ||
sleep(1) | ||
for _ in mail.fetch_inbox(): | ||
print(mail.get_message_content(_["id"])) | ||
mail_token = match(r"(\d){5,6}", mail.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_adress}:{token}\n') | ||
|
||
if logging: print(time() - start) | ||
|
||
return token | ||
|
||
|
||
class StreamingCompletion: | ||
@staticmethod | ||
def create( | ||
token=None, | ||
chatId=None, | ||
prompt='', | ||
actionType='new', | ||
defaultPersona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default | ||
model='gpt-4') -> ForeFrontResponse: | ||
|
||
if not token: raise Exception('Token is required!') | ||
if not chatId: chatId = 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': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', | ||
} | ||
|
||
json_data = { | ||
'text': prompt, | ||
'action': actionType, | ||
'parentId': chatId, | ||
'workspaceId': chatId, | ||
'messagePersona': defaultPersona, | ||
'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 != None: | ||
yield ForeFrontResponse({ | ||
'id': chatId, | ||
'object': 'text_completion', | ||
'created': int(time()), | ||
'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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class ForeFrontResponse: | ||
class Completion: | ||
class Choices: | ||
def __init__(self, choice: dict) -> None: | ||
self.text = choice['text'] | ||
self.content = self.text.encode() | ||
self.index = choice['index'] | ||
self.logprobs = choice['logprobs'] | ||
self.finish_reason = choice['finish_reason'] | ||
|
||
def __repr__(self) -> str: | ||
return f'''<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>''' | ||
|
||
def __init__(self, choices: dict) -> None: | ||
self.choices = [self.Choices(choice) for choice in choices] | ||
|
||
class Usage: | ||
def __init__(self, usage_dict: dict) -> None: | ||
self.prompt_tokens = usage_dict['prompt_tokens'] | ||
self.completion_tokens = usage_dict['completion_tokens'] | ||
self.total_tokens = usage_dict['total_tokens'] | ||
|
||
def __repr__(self): | ||
return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>''' | ||
|
||
def __init__(self, response_dict: dict) -> None: | ||
self.response_dict = response_dict | ||
self.id = response_dict['id'] | ||
self.object = response_dict['object'] | ||
self.created = response_dict['created'] | ||
self.model = response_dict['model'] | ||
self.completion = self.Completion(response_dict['choices']) | ||
self.usage = self.Usage(response_dict['usage']) | ||
|
||
def json(self) -> dict: | ||
return self.response_dict |
Oops, something went wrong.