Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
from fastapi import FastAPI, Form, Request
|
3 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
4 |
import os
|
@@ -8,9 +7,9 @@ import random
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
# Load environment variables
|
12 |
MODEL = "gpt-4o-mini"
|
13 |
-
API_URL = os.getenv("API_URL")
|
14 |
DISABLED = os.getenv("DISABLED") == 'True'
|
15 |
OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS", "").split(",")
|
16 |
NUM_THREADS = int(os.getenv("NUM_THREADS", 1))
|
@@ -131,6 +130,12 @@ async def chat(input: str = Form(...), top_p: float = Form(1.0), temperature: fl
|
|
131 |
if DISABLED:
|
132 |
return StreamingResponse(iter(["Usage limit reached."]), media_type="text/plain")
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
payload = {
|
135 |
"model": MODEL,
|
136 |
"messages": [{"role": "user", "content": input}],
|
@@ -159,6 +164,8 @@ async def chat(input: str = Form(...), top_p: float = Form(1.0), temperature: fl
|
|
159 |
chunk_json = json.loads(chunk_data[6:])
|
160 |
if "choices" in chunk_json and "delta" in chunk_json["choices"][0] and "content" in chunk_json["choices"][0]["delta"]:
|
161 |
yield chunk_json["choices"][0]["delta"]["content"]
|
|
|
|
|
162 |
except Exception as e:
|
163 |
yield f"Error: {str(e)}"
|
164 |
|
|
|
|
|
1 |
from fastapi import FastAPI, Form, Request
|
2 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
3 |
import os
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# Load environment variables with defaults
|
11 |
MODEL = "gpt-4o-mini"
|
12 |
+
API_URL = os.getenv("API_URL", "https://api.openai.com/v1/chat/completions") # Default to OpenAI API
|
13 |
DISABLED = os.getenv("DISABLED") == 'True'
|
14 |
OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS", "").split(",")
|
15 |
NUM_THREADS = int(os.getenv("NUM_THREADS", 1))
|
|
|
130 |
if DISABLED:
|
131 |
return StreamingResponse(iter(["Usage limit reached."]), media_type="text/plain")
|
132 |
|
133 |
+
if not API_URL:
|
134 |
+
return StreamingResponse(iter(["Error: API_URL is not set in the environment."]), media_type="text/plain")
|
135 |
+
|
136 |
+
if not OPENAI_API_KEYS or OPENAI_API_KEYS == [""]:
|
137 |
+
return StreamingResponse(iter(["Error: No valid OPENAI_API_KEYS provided."]), media_type="text/plain")
|
138 |
+
|
139 |
payload = {
|
140 |
"model": MODEL,
|
141 |
"messages": [{"role": "user", "content": input}],
|
|
|
164 |
chunk_json = json.loads(chunk_data[6:])
|
165 |
if "choices" in chunk_json and "delta" in chunk_json["choices"][0] and "content" in chunk_json["choices"][0]["delta"]:
|
166 |
yield chunk_json["choices"][0]["delta"]["content"]
|
167 |
+
except requests.exceptions.MissingSchema:
|
168 |
+
yield "Error: Invalid API_URL. Please provide a valid URL (e.g., https://api.openai.com/v1/chat/completions)."
|
169 |
except Exception as e:
|
170 |
yield f"Error: {str(e)}"
|
171 |
|