diff --git a/sdk/ai/azure-ai-generative/azure/ai/generative/index/_embeddings/__init__.py b/sdk/ai/azure-ai-generative/azure/ai/generative/index/_embeddings/__init__.py index c2938372b55d..6aa5846c8063 100644 --- a/sdk/ai/azure-ai-generative/azure/ai/generative/index/_embeddings/__init__.py +++ b/sdk/ai/azure-ai-generative/azure/ai/generative/index/_embeddings/__init__.py @@ -2,6 +2,7 @@ # --------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # --------------------------------------------------------- +# pylint: skip-file """Embeddings generation and management tools.""" import contextlib import copy diff --git a/sdk/ai/azure-ai-generative/azure/ai/generative/index/_utils/logging.py b/sdk/ai/azure-ai-generative/azure/ai/generative/index/_utils/logging.py index 7f6a47408fea..b6c56c305b7d 100644 --- a/sdk/ai/azure-ai-generative/azure/ai/generative/index/_utils/logging.py +++ b/sdk/ai/azure-ai-generative/azure/ai/generative/index/_utils/logging.py @@ -1,6 +1,7 @@ # --------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # --------------------------------------------------------- +# pylint: disable=W0125 """Logging utilities.""" import inspect import logging diff --git a/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/_callback_conversation_bot.py b/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/_callback_conversation_bot.py index 007201fe9baf..8cdf2f45ec8c 100644 --- a/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/_callback_conversation_bot.py +++ b/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/_callback_conversation_bot.py @@ -1,6 +1,7 @@ # --------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # --------------------------------------------------------- +#pylint: skip-file import copy from typing import List, Tuple @@ -29,8 +30,22 @@ async def generate_response( self.user_template, conversation_history, self.user_template_parameters ) msg_copy = copy.deepcopy(chat_protocol_message) - result = await self.callback(msg_copy) - + result = {} + try: + result = await self.callback(msg_copy) + except Exception as exc: + if "status_code" in dir(exc) and 400 <= exc.status_code < 500 and "response was filtered" in exc.message: + result = { + "messages": [{ + "content": ("Error: The response was filtered due to the prompt " + "triggering Azure OpenAI's content management policy. " + "Please modify your prompt and retry."), + "role": "assistant" + }], + "finish_reason": ["stop"], + "id": None, + "template_parameters": {} + } self.logger.info("Using user provided callback returning response.") time_taken = 0 @@ -54,6 +69,9 @@ def _to_chat_protocol(self, template, conversation_history, template_parameters) for _, m in enumerate(conversation_history): messages.append({"content": m.message, "role": m.role.value}) + if template_parameters.get("file_content", None) and any('File contents:' not in message['content'] for message in messages): + messages.append({"content": f"File contents: {template_parameters['file_content']}", "role": "user"}) + return { "template_parameters": template_parameters, "messages": messages, diff --git a/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/simulator.py b/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/simulator.py index f6a9162c30aa..2554ad75e489 100644 --- a/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/simulator.py +++ b/sdk/ai/azure-ai-generative/azure/ai/generative/synthetic/simulator/simulator/simulator.py @@ -194,7 +194,7 @@ def _join_conversation_starter(self, parameters, to_join): async def simulate_async( self, template: "Template", - max_conversation_turns: int, + max_conversation_turns: int = 2, parameters: Optional[List[dict]] = None, jailbreak: bool = False, api_call_retry_limit: int = 3, @@ -208,6 +208,7 @@ async def simulate_async( :keyword template: An instance of the Template class defining the conversation structure. :paramtype template: Template :keyword max_conversation_turns: The maximum number of conversation turns to simulate. + Defaults to 2, change only applies to chat templates. :paramtype max_conversation_turns: int :keyword parameters: A list of dictionaries containing the parameter values to be used in the simulations. Defaults to an empty list. @@ -239,7 +240,8 @@ async def simulate_async( if not isinstance(parameters, list): raise ValueError(f"Expect parameters to be a list of dictionary, but found {type(parameters)}") - + if "conversation" not in template.template_name: + max_conversation_turns = 2 if template.content_harm: self._ensure_service_dependencies() self.adversarial = True diff --git a/sdk/ai/azure-ai-generative/pyproject.toml b/sdk/ai/azure-ai-generative/pyproject.toml index 9e37165cc0f0..c12b3e784e21 100644 --- a/sdk/ai/azure-ai-generative/pyproject.toml +++ b/sdk/ai/azure-ai-generative/pyproject.toml @@ -13,7 +13,7 @@ strict_sphinx = true [tool.mypy] python_version = "3.10" -exclude = ["azure/ai/generative/index/_langchain/vendor", "tests", "setup.py", "samples", "azure/ai/generative/evaluate/pf_templates/built_in_metrics"] +exclude = ["azure/ai/generative/index/_langchain/vendor", "tests", "setup.py", "samples", "azure/ai/generative/evaluate/pf_templates/built_in_metrics", "azure/ai/generative/synthetic"] warn_unused_configs = true follow_imports = "skip" ignore_missing_imports = true