N.Achyuth Reddy
commited on
Commit
·
abf83da
1
Parent(s):
09a84dd
Create Better.py
Browse files
g4f/Provider/Providers/Better.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from typing import Dict, get_type_hints
|
| 5 |
+
|
| 6 |
+
url = 'https://openai-proxy-api.vercel.app/v1/'
|
| 7 |
+
model = [
|
| 8 |
+
'gpt-3.5-turbo',
|
| 9 |
+
'gpt-3.5-turbo-0613',
|
| 10 |
+
'gpt-3.5-turbo-16k',
|
| 11 |
+
'gpt-3.5-turbo-16k-0613',
|
| 12 |
+
'gpt-4',
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
supports_stream = True
|
| 16 |
+
needs_auth = False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _create_completion(model: str, messages: list, stream: bool, **kwargs):
|
| 20 |
+
headers = {
|
| 21 |
+
'Content-Type': 'application/json',
|
| 22 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58',
|
| 23 |
+
'Referer': 'https://chat.ylokh.xyz/',
|
| 24 |
+
'Origin': 'https://chat.ylokh.xyz',
|
| 25 |
+
'Connection': 'keep-alive',
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
json_data = {
|
| 29 |
+
'messages': messages,
|
| 30 |
+
'temperature': 1.0,
|
| 31 |
+
'model': model,
|
| 32 |
+
'stream': stream,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
response = requests.post(
|
| 36 |
+
'https://openai-proxy-api.vercel.app/v1/chat/completions', headers=headers, json=json_data, stream=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
for token in response.iter_lines():
|
| 40 |
+
decoded = token.decode('utf-8')
|
| 41 |
+
if decoded.startswith('data: '):
|
| 42 |
+
data_str = decoded.replace('data: ', '')
|
| 43 |
+
data = json.loads(data_str)
|
| 44 |
+
if 'choices' in data and 'delta' in data['choices'][0]:
|
| 45 |
+
delta = data['choices'][0]['delta']
|
| 46 |
+
content = delta.get('content', '')
|
| 47 |
+
finish_reason = delta.get('finish_reason', '')
|
| 48 |
+
|
| 49 |
+
if finish_reason == 'stop':
|
| 50 |
+
break
|
| 51 |
+
if content:
|
| 52 |
+
yield content
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + '(%s)' % ', '.join(
|
| 56 |
+
[f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
| 57 |
+
|