update usesless, add function to retrieve token

This commit is contained in:
ezerinz 2023-05-21 19:28:19 +08:00
parent 8449728aa3
commit 7f4322e106
No known key found for this signature in database
GPG Key ID: 8713BDED6DE33C4D
3 changed files with 109 additions and 22 deletions

View File

@ -1,23 +1,32 @@
ai.usesless.com
to do:
- use random user agent in header
- make the code better I guess (?)
### Example: `usesless` <a name="example-usesless"></a>
### create account
<p>create account first, this will create account.txt that contains mail and token</p>
```python
import usesless
usesless.Account.create(logging=True)
````
### completion
<p>insert token from account.txt</p>
```python
import usesless
message_id = ""
token = <TOKENHERE> # usesless.Account.create(logging=True)
while True:
prompt = input("Question: ")
if prompt == "!stop":
break
req = usesless.Completion.create(prompt=prompt, parentMessageId=message_id)
req = usesless.Completion.create(prompt=prompt, parentMessageId=message_id, token=token)
print(f"Answer: {req['text']}")
message_id = req["id"]
```
````

View File

@ -1,22 +1,87 @@
import time
import re
import json
import requests
import fake_useragent
import names
from mailgw_temporary_email import Email
from password_generator import PasswordGenerator
class Account:
@staticmethod
def create(logging: bool = False):
mail_client = Email()
mail_client.register()
mail_address = mail_client.address
pwo = PasswordGenerator()
pwo.minlen = 8
password = pwo.generate()
session = requests.Session()
if logging:
print(f"email: {mail_address}")
register_url = "https://ai.usesless.com/api/cms/auth/local/register"
register_json = {
"username": names.get_first_name(),
"password": password,
"email": mail_address,
}
headers = {
"authority": "ai.usesless.com",
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.5",
"cache-control": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": fake_useragent.UserAgent().random,
}
register = session.post(register_url, json=register_json, headers=headers)
if logging:
if register.status_code == 200:
print("register success")
else:
print("there's a problem with account creation, try again")
if register.status_code != 200:
quit()
while True:
time.sleep(5)
message_id = mail_client.message_list()[0]["id"]
message = mail_client.message(message_id)
verification_url = re.findall(
r"http:\/\/ai\.usesless\.com\/api\/cms\/auth\/email-confirmation\?confirmation=\w.+\w\w",
message["text"],
)[0]
if verification_url:
break
session.get(verification_url)
login_json = {"identifier": mail_address, "password": password}
login_request = session.post(
url="https://ai.usesless.com/api/cms/auth/local", json=login_json
)
token = login_request.json()["jwt"]
if logging:
print(f"token: {token}")
with open("accounts.txt", "w") as f:
f.write(f"{mail_address}\n")
f.write(f"{token}")
return token
class Completion:
headers = {
"authority": "ai.usesless.com",
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.5",
"cache-control": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0",
}
@staticmethod
def create(
token: str,
systemMessage: str = "You are a helpful assistant",
prompt: str = "",
parentMessageId: str = "",
@ -24,7 +89,17 @@ class Completion:
temperature: float = 1,
model: str = "gpt-3.5-turbo",
):
print(parentMessageId, prompt)
headers = {
"authority": "ai.usesless.com",
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.5",
"cache-control": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": fake_useragent.UserAgent().random,
"Authorization": f"Bearer {token}",
}
json_data = {
"openaiKey": "",
@ -41,14 +116,14 @@ class Completion:
}
url = "https://ai.usesless.com/api/chat-process"
request = requests.post(url, headers=Completion.headers, json=json_data)
request = requests.post(url, headers=headers, json=json_data)
content = request.content
response = Completion.__response_to_json(content)
return response
@classmethod
def __response_to_json(cls, text) -> dict:
def __response_to_json(cls, text) -> str:
text = str(text.decode("utf-8"))
split_text = text.rsplit("\n", 1)[1]

View File

@ -0,0 +1,3 @@
import usesless
usesless.Account.create(logging=True)