Create Liaobots.py
Browse files
g4f/Provider/Providers/Liaobots.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import requests
|
| 4 |
+
from ...typing import sha256, Dict, get_type_hints
|
| 5 |
+
import requests
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
url = 'https://liaobots.com'
|
| 9 |
+
model = ['gpt-3.5-turbo-16k', 'gpt-4']
|
| 10 |
+
supports_stream = True
|
| 11 |
+
needs_auth = False
|
| 12 |
+
working = True
|
| 13 |
+
|
| 14 |
+
models = {
|
| 15 |
+
'gpt-4': {
|
| 16 |
+
"id": "gpt-4",
|
| 17 |
+
"name": "GPT-4",
|
| 18 |
+
"maxLength": 24000,
|
| 19 |
+
"tokenLimit": 8000
|
| 20 |
+
},
|
| 21 |
+
'gpt-3.5-turbo-16k': {
|
| 22 |
+
"id": "gpt-3.5-turbo-16k",
|
| 23 |
+
"name": "GPT-3.5-16k",
|
| 24 |
+
"maxLength": 48000,
|
| 25 |
+
"tokenLimit": 16000
|
| 26 |
+
},
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_response():
|
| 31 |
+
try:
|
| 32 |
+
url = "https://ka1kuk-fastapi.hf.space/authCode"
|
| 33 |
+
headers = {"Content-Type": "application/json"}
|
| 34 |
+
response = requests.get(url, headers=headers)
|
| 35 |
+
if response.status_code == 500:
|
| 36 |
+
print("Received status code 500. Retrying...")
|
| 37 |
+
time.sleep(5)
|
| 38 |
+
return get_response()
|
| 39 |
+
return response.json()
|
| 40 |
+
except requests.RequestException as e:
|
| 41 |
+
print(f"Error occurred: {e}")
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _create_completion(model: str, messages: list, stream: bool, **kwargs):
|
| 47 |
+
|
| 48 |
+
print(kwargs)
|
| 49 |
+
authCode = get_response()
|
| 50 |
+
|
| 51 |
+
headers = {
|
| 52 |
+
'authority': 'liaobots.com',
|
| 53 |
+
'content-type': 'application/json',
|
| 54 |
+
'origin': 'https://liaobots.com',
|
| 55 |
+
'referer': 'https://liaobots.com/',
|
| 56 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
| 57 |
+
'x-auth-code': authCode
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
json_data = {
|
| 61 |
+
'conversationId': str(uuid.uuid4()),
|
| 62 |
+
'model': models[model],
|
| 63 |
+
'messages': messages,
|
| 64 |
+
'key': '',
|
| 65 |
+
'prompt': "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.",
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
response = requests.post('https://liaobots.com/api/chat',
|
| 69 |
+
headers=headers, json=json_data, stream=True)
|
| 70 |
+
|
| 71 |
+
for token in response.iter_content(chunk_size=2046):
|
| 72 |
+
yield (token.decode('utf-8'))
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
| 76 |
+
'(%s)' % ', '.join(
|
| 77 |
+
[f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|