Spaces:
Running
Running
troubleshoot chatui
Browse files- app/main.py +66 -0
app/main.py
CHANGED
@@ -169,6 +169,72 @@ async def root():
|
|
169 |
}
|
170 |
}
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
# Mount Gradio at a specific path instead of root to avoid conflicts
|
173 |
demo = create_gradio_interface()
|
174 |
app.mount("/ui", demo)
|
|
|
169 |
}
|
170 |
}
|
171 |
|
172 |
+
# Add this to your FastAPI app after the existing routes
|
173 |
+
|
174 |
+
@app.post("/v1/chat/completions")
|
175 |
+
async def openai_compatible_endpoint(request: Request):
|
176 |
+
"""OpenAI-compatible endpoint that ChatUI might prefer"""
|
177 |
+
try:
|
178 |
+
data = await request.json()
|
179 |
+
logger.info(f"OpenAI endpoint received: {data}")
|
180 |
+
|
181 |
+
# Extract messages from OpenAI format
|
182 |
+
messages = data.get("messages", [])
|
183 |
+
user_message = ""
|
184 |
+
|
185 |
+
for msg in reversed(messages):
|
186 |
+
if msg.get("role") == "user":
|
187 |
+
user_message = msg.get("content", "")
|
188 |
+
break
|
189 |
+
|
190 |
+
if not user_message:
|
191 |
+
return {"error": "No user message found"}
|
192 |
+
|
193 |
+
result = process_chatfed_query_core(query=user_message)
|
194 |
+
|
195 |
+
# Return in OpenAI format
|
196 |
+
return {
|
197 |
+
"choices": [{
|
198 |
+
"message": {
|
199 |
+
"role": "assistant",
|
200 |
+
"content": result.result
|
201 |
+
},
|
202 |
+
"finish_reason": "stop"
|
203 |
+
}],
|
204 |
+
"model": "chatfed",
|
205 |
+
"usage": {"total_tokens": 0}
|
206 |
+
}
|
207 |
+
|
208 |
+
except Exception as e:
|
209 |
+
logger.error(f"Error in OpenAI endpoint: {str(e)}")
|
210 |
+
return {"error": str(e)}
|
211 |
+
|
212 |
+
# Also add a simple text endpoint
|
213 |
+
@app.post("/simple-chat")
|
214 |
+
async def simple_chat_endpoint(request: Request):
|
215 |
+
"""Simple endpoint that accepts any text input"""
|
216 |
+
try:
|
217 |
+
data = await request.json()
|
218 |
+
logger.info(f"Simple chat received: {data}")
|
219 |
+
|
220 |
+
# Try to extract text from various possible fields
|
221 |
+
text = (data.get("text") or
|
222 |
+
data.get("query") or
|
223 |
+
data.get("prompt") or
|
224 |
+
data.get("input") or
|
225 |
+
str(data))
|
226 |
+
|
227 |
+
result = process_chatfed_query_core(query=text)
|
228 |
+
|
229 |
+
return {
|
230 |
+
"response": result.result,
|
231 |
+
"metadata": result.metadata
|
232 |
+
}
|
233 |
+
|
234 |
+
except Exception as e:
|
235 |
+
logger.error(f"Error in simple chat: {str(e)}")
|
236 |
+
return {"error": str(e)}
|
237 |
+
|
238 |
# Mount Gradio at a specific path instead of root to avoid conflicts
|
239 |
demo = create_gradio_interface()
|
240 |
app.mount("/ui", demo)
|