Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
@@ -5,7 +6,6 @@ import time
|
|
5 |
# from typing import Any
|
6 |
|
7 |
import g4f
|
8 |
-
from g4f import ChatCompletion, Provider
|
9 |
from fastapi import FastAPI, Request
|
10 |
from fastapi.responses import StreamingResponse
|
11 |
|
@@ -15,6 +15,7 @@ from starlette.middleware.cors import CORSMiddleware
|
|
15 |
|
16 |
import nest_asyncio
|
17 |
import os # Importo el módulo os para usar la variable de entorno
|
|
|
18 |
|
19 |
nest_asyncio.apply()
|
20 |
|
@@ -33,7 +34,7 @@ app.add_middleware(
|
|
33 |
async def chat_completions(request: Request):
|
34 |
req_data = await request.json()
|
35 |
stream = req_data.get("stream", False)
|
36 |
-
model = req_data.get("model", "gpt-
|
37 |
messages = req_data.get("messages")
|
38 |
temperature = req_data.get("temperature", 1.0)
|
39 |
top_p = req_data.get("top_p", 1.0)
|
@@ -130,9 +131,13 @@ async def gen_resp(max_tokens, messages, model, stream, temperature, top_p):
|
|
130 |
provider=g4f.Provider.Bing,
|
131 |
)
|
132 |
return response
|
|
|
|
|
|
|
|
|
|
|
133 |
except Exception as e:
|
134 |
logger.error(f"gen_resp: Exception: {e}")
|
135 |
attempts += 1
|
136 |
if attempts >= MAX_ATTEMPTS:
|
137 |
return "Lo siento, no he podido generar una respuesta de chat. Por favor, revisa tu conexión a Internet y la configuración de la API y vuelve a intentarlo."
|
138 |
-
|
|
|
1 |
+
# Importamos varios módulos y librerías necesarios para el funcionamiento del script, como json, random, string, time, g4f, fastapi, starlette, requests y anyio.
|
2 |
import json
|
3 |
import random
|
4 |
import string
|
|
|
6 |
# from typing import Any
|
7 |
|
8 |
import g4f
|
|
|
9 |
from fastapi import FastAPI, Request
|
10 |
from fastapi.responses import StreamingResponse
|
11 |
|
|
|
15 |
|
16 |
import nest_asyncio
|
17 |
import os # Importo el módulo os para usar la variable de entorno
|
18 |
+
import requests # Importo la librería requests para hacer peticiones HTTP
|
19 |
|
20 |
nest_asyncio.apply()
|
21 |
|
|
|
34 |
async def chat_completions(request: Request):
|
35 |
req_data = await request.json()
|
36 |
stream = req_data.get("stream", False)
|
37 |
+
model = req_data.get("model", "gpt-4-32k")
|
38 |
messages = req_data.get("messages")
|
39 |
temperature = req_data.get("temperature", 1.0)
|
40 |
top_p = req_data.get("top_p", 1.0)
|
|
|
131 |
provider=g4f.Provider.Bing,
|
132 |
)
|
133 |
return response
|
134 |
+
except requests.exceptions.ContentTypeError as e: # Capturo el error de tipo ContentTypeError y muestro un mensaje adecuado al usuario. Esta es la parte que he modificado.
|
135 |
+
logger.error(f"gen_resp: Exception: {e}")
|
136 |
+
attempts += 1
|
137 |
+
if attempts >= MAX_ATTEMPTS:
|
138 |
+
return "Lo siento, no he podido generar una respuesta de chat. La API de Bing ha devuelto un tipo de contenido inesperado. Por favor, revisa la documentación de la API y vuelve a intentarlo."
|
139 |
except Exception as e:
|
140 |
logger.error(f"gen_resp: Exception: {e}")
|
141 |
attempts += 1
|
142 |
if attempts >= MAX_ATTEMPTS:
|
143 |
return "Lo siento, no he podido generar una respuesta de chat. Por favor, revisa tu conexión a Internet y la configuración de la API y vuelve a intentarlo."
|
|