wilson989 commited on
Commit
c39533f
1 Parent(s): f7383de

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import string
4
+ import time
5
+ #from typing import Any
6
+
7
+ import g4f
8
+ from fastapi import FastAPI, Request
9
+ from fastapi.responses import StreamingResponse
10
+
11
+ from g4f import ChatCompletion
12
+ from loguru import logger
13
+ from starlette.middleware.cors import CORSMiddleware
14
+
15
+ import nest_asyncio
16
+ import os # Importo el módulo os para usar la variable de entorno
17
+
18
+ nest_asyncio.apply()
19
+
20
+ app = FastAPI()
21
+
22
+ app.add_middleware(
23
+ CORSMiddleware,
24
+ allow_origins=["*"],
25
+ allow_methods=["*"],
26
+ allow_headers=["*"],
27
+ )
28
+
29
+
30
+ @app.post("/chat/completions")
31
+ @app.post("/v1/chat/completions")
32
+ async def chat_completions(request: Request):
33
+ req_data = await request.json()
34
+ stream = req_data.get("stream", False)
35
+ model = req_data.get("model", "gpt-3.5-turbo")
36
+ messages = req_data.get("messages")
37
+ temperature = req_data.get("temperature", 1.0)
38
+ top_p = req_data.get("top_p", 1.0)
39
+ max_tokens = req_data.get("max_tokens", 0)
40
+
41
+ logger.info(
42
+ f"chat_completions: stream: {stream}, model: {model}, temperature: {temperature}, top_p: {top_p}, max_tokens: {max_tokens}"
43
+ )
44
+
45
+ response = await gen_resp(max_tokens, messages, model, stream, temperature, top_p)
46
+
47
+ completion_id = "".join(random.choices(string.ascii_letters + string.digits, k=28))
48
+ completion_timestamp = int(time.time())
49
+
50
+ if not stream:
51
+ logger.info(f"chat_completions: response: {response}")
52
+ return {
53
+ "id": f"chatcmpl-{completion_id}",
54
+ "object": "chat.completion",
55
+ "created": completion_timestamp,
56
+ "model": model,
57
+ "choices": [
58
+ {
59
+ "index": 0,
60
+ "message": {
61
+ "role": "assistant",
62
+ "content": response,
63
+ },
64
+ "finish_reason": "stop",
65
+ }
66
+ ],
67
+ "usage": {
68
+ "prompt_tokens": None,
69
+ "completion_tokens": None,
70
+ "total_tokens": None,
71
+ },
72
+ }
73
+
74
+ def streaming():
75
+ for chunk in response:
76
+ completion_data = {
77
+ "id": f"chatcmpl-{completion_id}",
78
+ "object": "chat.completion.chunk",
79
+ "created": completion_timestamp,
80
+ "model": model,
81
+ "choices": [
82
+ {
83
+ "index": 0,
84
+ "delta": {
85
+ "content": chunk,
86
+ },
87
+ "finish_reason": None,
88
+ }
89
+ ],
90
+ }
91
+
92
+ content = json.dumps(completion_data, separators=(",", ":"))
93
+ yield f"data: {content}\n\n"
94
+ time.sleep(0)
95
+
96
+ end_completion_data: dict[str, Any] = {
97
+ "id": f"chatcmpl-{completion_id}",
98
+ "object": "chat.completion.chunk",
99
+ "created": completion_timestamp,
100
+ "model": model,
101
+ "choices": [
102
+ {
103
+ "index": 0,
104
+ "delta": {},
105
+ "finish_reason": "stop",
106
+ }
107
+ ],
108
+ }
109
+ content = json.dumps(end_completion_data, separators=(",", ":"))
110
+ yield f"data: {content}\n\n"
111
+
112
+ return StreamingResponse(streaming(), media_type="text/event-stream")
113
+
114
+
115
+ async def gen_resp(max_tokens, messages, model, stream, temperature, top_p):
116
+ # Obtengo el valor de MAX_ATTEMPTS desde la variable de entorno o uso un valor por defecto de 10
117
+ MAX_ATTEMPTS = int(os.getenv("MAX_ATTEMPTS", 10))
118
+ attempts = 0
119
+ while True:
120
+ try:
121
+ response = ChatCompletion.create(
122
+ model=model,
123
+ stream=stream,
124
+ messages=messages,
125
+ temperature=temperature,
126
+ top_p=top_p,
127
+ max_tokens=max_tokens,
128
+ system_prompt="",
129
+ provider=g4f.Provider.Bing,
130
+ )
131
+ return response
132
+ except Exception as e:
133
+ logger.error(f"gen_resp: Exception: {e}")
134
+ attempts += 1
135
+ if attempts >= MAX_ATTEMPTS:
136
+ 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."
137
+