Fix issues

This commit is contained in:
Iyeoluwa Akinrinola
2025-05-12 16:10:45 +01:00
parent 47e02c9352
commit 7c61e98340
5 changed files with 122 additions and 23 deletions
+37 -21
View File
@@ -23,7 +23,7 @@ async def get_models():
Get available models in OpenWebUI-compatible format.
"""
models = model_service.get_available_models()
# Convert to OpenWebUI format
openwebui_models = []
for model in models:
@@ -36,7 +36,23 @@ async def get_models():
"root": model["id"],
"parent": None
})
# Debug log
print(f"OpenWebUI models: {json.dumps(openwebui_models, indent=2)}")
# Ensure we're returning a properly formatted list
if not openwebui_models:
# Return at least one default model if none are found
return [{
"id": "llama3.1",
"object": "model",
"created": int(time.time()),
"owned_by": "user",
"permission": [],
"root": "llama3.1",
"parent": None
}]
return openwebui_models
# Chat completions endpoint (OpenAI-compatible)
@@ -47,7 +63,7 @@ async def chat_completions(request: Request):
"""
# Parse request body
body = await request.json()
# Extract parameters
model_id = body.get("model", "llama3.1")
messages = body.get("messages", [])
@@ -58,26 +74,26 @@ async def chat_completions(request: Request):
frequency_penalty = body.get("frequency_penalty")
presence_penalty = body.get("presence_penalty")
stop = body.get("stop")
# Create a unique chat ID
chat_id = str(uuid.uuid4())
# Create a user ID (in a real implementation, this would come from authentication)
user_id = "openwebui-user"
# Create a new chat
chat_service.create_chat(user_id=user_id, title="API Chat", model_id=model_id)
# Extract the user's message (last user message in the array)
user_message = None
for msg in reversed(messages):
if msg.get("role") == "user":
user_message = msg.get("content")
break
if not user_message:
raise HTTPException(status_code=400, detail="No user message found")
# Get chat response
response = chat_service.get_chat_response(
chat_id=chat_id,
@@ -90,10 +106,10 @@ async def chat_completions(request: Request):
presence_penalty=presence_penalty,
stop_sequences=stop if isinstance(stop, list) else [stop] if stop else None
)
# Format response in OpenAI-compatible format
completion_id = f"chatcmpl-{str(uuid.uuid4())[:8]}"
openai_response = {
"id": completion_id,
"object": "chat.completion",
@@ -115,16 +131,16 @@ async def chat_completions(request: Request):
"total_tokens": -1
}
}
# Handle streaming if requested
if stream:
async def generate_stream():
# Yield the response in the SSE format
yield f"data: {json.dumps(openai_response)}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(generate_stream(), media_type="text/event-stream")
return openai_response
# Health check endpoint
@@ -143,27 +159,27 @@ async def ollama_generate(request: Request):
"""
# Parse request body
body = await request.json()
# Extract parameters
model_id = body.get("model", "llama3.1")
prompt = body.get("prompt", "")
# Create a unique chat ID
chat_id = str(uuid.uuid4())
# Create a user ID (in a real implementation, this would come from authentication)
user_id = "openwebui-user"
# Create a new chat
chat_service.create_chat(user_id=user_id, title="API Chat", model_id=model_id)
# Get chat response
response = chat_service.get_chat_response(
chat_id=chat_id,
message=prompt,
user_id=user_id
)
# Format response in Ollama-compatible format
ollama_response = {
"model": model_id,
@@ -171,5 +187,5 @@ async def ollama_generate(request: Request):
"response": response.get("content", ""),
"done": True
}
return ollama_response