Spaces:
Running
Running
File size: 1,778 Bytes
d2211b9 d3375bf d2211b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import os
import json
import requests
from typing import Dict, get_type_hints
url = 'https://ai.fakeopen.com/v1/'
model = [
'gpt-3.5-turbo',
'gpt-3.5-turbo-0613'
'gpt-3.5-turbo-16k',
'gpt-3.5-turbo-16k-0613',
]
supports_stream = True
needs_auth = False
def _create_completion(model: str, messages: list, stream: bool, **kwargs):
headers = {
'Content-Type': 'application/json',
'accept': 'text/event-stream',
'Cache-Control': 'no-cache',
'Proxy-Connection': 'keep-alive',
'Authorization': f"Bearer {os.environ.get('FAKE_OPEN_KEY', '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')}",
}
json_data = {
'messages': messages,
'temperature': 1.0,
'model': model,
'stream': stream,
}
response = requests.post(
'https://ai.fakeopen.com/v1/chat/completions', headers=headers, json=json_data, stream=True
)
for token in response.iter_lines():
decoded = token.decode('utf-8')
if decoded == '[DONE]':
break
if decoded.startswith('data: '):
data_str = decoded.replace('data: ', '')
if data_str != '[DONE]':
data = json.loads(data_str)
if 'choices' in data and 'delta' in data['choices'][0] and 'content' in data['choices'][0]['delta']:
yield data['choices'][0]['delta']['content']
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + '(%s)' % ', '.join(
[f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|