diff --git a/app/backend/approaches/chatreadretrieveread.py b/app/backend/approaches/chatreadretrieveread.py index c570f59e..e0f8970e 100644 --- a/app/backend/approaches/chatreadretrieveread.py +++ b/app/backend/approaches/chatreadretrieveread.py @@ -10,6 +10,7 @@ import openai from openai import AsyncAzureOpenAI +from openai import BadRequestError from approaches.approach import Approach from azure.search.documents import SearchClient from azure.search.documents.models import RawVectorQuery @@ -200,7 +201,10 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any] if filter_reasons: error_message = "The generated content was filtered due to triggering Azure OpenAI's content filtering system. Reason(s): The response contains content flagged as " + ", ".join(filter_reasons) raise ValueError(error_message) - + except BadRequestError as e: + log.error(f"Error generating optimized keyword search: {str(e.body['message'])}") + yield json.dumps({"error": f"Error generating optimized keyword search: {str(e.body['message'])}"}) + "\n" + return except Exception as e: log.error(f"Error generating optimized keyword search: {str(e)}") yield json.dumps({"error": f"Error generating optimized keyword search: {str(e)}"}) + "\n" diff --git a/app/backend/approaches/chatwebretrieveread.py b/app/backend/approaches/chatwebretrieveread.py index f51f25c1..a081a7b9 100644 --- a/app/backend/approaches/chatwebretrieveread.py +++ b/app/backend/approaches/chatwebretrieveread.py @@ -12,6 +12,7 @@ from azure.core.credentials import AzureKeyCredential import openai from openai import AzureOpenAI +from openai import BadRequestError from openai import AsyncAzureOpenAI from approaches.approach import Approach from core.messagebuilder import MessageBuilder @@ -143,6 +144,10 @@ async def run(self, history: Sequence[dict[str, str]],overrides: dict[str, Any], try: query_resp = await self.make_chat_completion(messages) + except BadRequestError as e: + log.error(f"Error generating optimized keyword search: {str(e.body['message'])}") + yield json.dumps({"error": f"Error generating optimized keyword search: {str(e.body['message'])}"}) + "\n" + return except Exception as e: log.error(f"Error generating optimized keyword search: {str(e)}") yield json.dumps({"error": f"Error generating optimized keyword search: {str(e)}"}) + "\n"