From 6bcfdaaaf0329be9636c8b58a3966b751569b13e Mon Sep 17 00:00:00 2001 From: "smonux (aider)" Date: Sun, 6 Oct 2024 11:11:00 +0200 Subject: [PATCH] fix: resolve undefined names 'call_next' and 'response' in handle_nonstreaming_response --- backend/open_webui/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 10f964fac..d43ae03cb 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -432,6 +432,9 @@ def update_body_request(request: Request, ] return None +from starlette.middleware import Middleware +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint + async def handle_nonstreaming_response(request: Request, response: Response, tools: dict) -> Response: response_dict = json.loads(response) body = json.loads(request._body) @@ -721,14 +724,14 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware): update_body_request(request, body) first_response = await call_next(request) if not isinstance(first_response, StreamingResponse): - response = await handle_nonstreaming_response(request, response, tools) + response = await handle_nonstreaming_response(request, first_response, tools) return response content_type = first_response.headers["Content-Type"] is_openai = "text/event-stream" in content_type is_ollama = "application/x-ndjson" in content_type if not is_openai and not is_ollama: - return response + return first_response def wrap_item(item): return f"data: {item}\n\n" if is_openai else f"{item}\n"