import os import time import json import random #import requests #from gevent import pywsgi import g4f from fastapi import FastAPI, Request, Response from fastapi.responses import JSONResponse, StreamingResponse from fastapi.middleware.cors import CORSMiddleware import logging import uvicorn from g4f import ChatCompletion #from contextlib import closing # Importa el módulo nest_asyncio import nest_asyncio g4f.debug.logging = True nest_asyncio.apply() app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) LOG = logging.getLogger(__name__) @app.post("/chat/completions") @app.post("/v1/chat/completions") @app.post("/") async def chat_completions(request: Request): data = await request.json() streaming = data.get('stream', False) model = data.get('model', 'gpt-4-32k') messages = data.get('messages') response = ChatCompletion.create(model=model, stream=streaming, messages=messages, provider=g4f.Provider.Bing) if not streaming: while 'curl_cffi.requests.errors.RequestsError' in response: response = ChatCompletion.create(model=model, stream=streaming, messages=messages, provider=g4f.Provider.Bing) completion_timestamp = int(time.time()) completion_id = ''.join(random.choices( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) return JSONResponse({ 'id': 'chatcmpl-%s' % completion_id, 'object': 'chat.completion', 'created': completion_timestamp, 'model': model, 'usage': { 'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0 }, 'choices': [{ 'message': { 'role': 'assistant', 'content': response }, 'finish_reason': 'stop', 'index': 0 }] }) def stream(): completion_data = { 'id': '', 'object': 'chat.completion.chunk', 'created': 0, 'model': 'gpt-4-32k', 'choices': [ { 'delta': { 'content': "" }, 'index': 0, 'finish_reason': None } ] } for token in response: completion_id = ''.join( random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28)) completion_timestamp = int(time.time()) completion_data['id'] = f'chatcmpl-{completion_id}' completion_data['created'] = completion_timestamp completion_data['choices'][0]['delta']['content'] = token if token.startswith("an error occured"): completion_data['choices'][0]['delta']['content'] = "Server Response Error, please try again.\n" completion_data['choices'][0]['delta']['stop'] = "error" yield 'data: %s\n\ndata: [DONE]\n\n' % json.dumps(completion_data, separators=(',' ':')) return yield 'data: %s\n\n' % json.dumps(completion_data, separators=(',' ':')) time.sleep(0) completion_data['choices'][0]['finish_reason'] = "stop" completion_data['choices'][0]['delta']['content'] = "" yield 'data: %s\n\n' % json.dumps(completion_data, separators=(',' ':')) yield 'data: [DONE]\n\n' return StreamingResponse(stream(), media_type='text/event-stream') # if __name__ == '__main__': # uvicorn.run("app:app", host=site_config['host'], port=site_config['port'])