Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
-
import openai
|
4 |
from fastapi import FastAPI, Request
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
# Load
|
9 |
with open("kb.json") as f:
|
10 |
kb = json.load(f)
|
11 |
|
@@ -17,8 +17,8 @@ system_prompt += (
|
|
17 |
"'I'm not sure about that. Let me connect you with a human agent.'"
|
18 |
)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
@app.post("/ask")
|
24 |
async def ask(request: Request):
|
@@ -34,7 +34,7 @@ async def ask(request: Request):
|
|
34 |
]
|
35 |
|
36 |
try:
|
37 |
-
response =
|
38 |
model="gpt-4o-mini",
|
39 |
messages=messages,
|
40 |
temperature=0.0,
|
@@ -42,7 +42,7 @@ async def ask(request: Request):
|
|
42 |
)
|
43 |
answer = response.choices[0].message.content.strip()
|
44 |
except Exception as e:
|
45 |
-
print(f"
|
46 |
answer = "Sorry, I'm having trouble answering right now."
|
47 |
|
48 |
return {"reply": answer}
|
|
|
1 |
import os
|
2 |
import json
|
|
|
3 |
from fastapi import FastAPI, Request
|
4 |
+
from openai import OpenAI
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Load kb.json
|
9 |
with open("kb.json") as f:
|
10 |
kb = json.load(f)
|
11 |
|
|
|
17 |
"'I'm not sure about that. Let me connect you with a human agent.'"
|
18 |
)
|
19 |
|
20 |
+
# Initialize OpenAI client
|
21 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), api_base=os.getenv("OPENAI_API_BASE", "https://fast.typegpt.net/v1"))
|
22 |
|
23 |
@app.post("/ask")
|
24 |
async def ask(request: Request):
|
|
|
34 |
]
|
35 |
|
36 |
try:
|
37 |
+
response = client.chat.completions.create(
|
38 |
model="gpt-4o-mini",
|
39 |
messages=messages,
|
40 |
temperature=0.0,
|
|
|
42 |
)
|
43 |
answer = response.choices[0].message.content.strip()
|
44 |
except Exception as e:
|
45 |
+
print(f"OpenAI API error: {e}")
|
46 |
answer = "Sorry, I'm having trouble answering right now."
|
47 |
|
48 |
return {"reply": answer}
|