Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Content Filter Exception not happening on Function Invoke #10011

Open
afrancoc2000 opened this issue Dec 18, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@afrancoc2000
Copy link

Describe the bug
I need to run a prompt I have in a jinja template and check it for filtered content by catching the exception in case it fails but when I run kernel.invoke I only receive KernelInvokeException instead of a ContentFilterAIException.

To Reproduce
Steps to reproduce the behavior:

This is my code:

self.kernel.add_service(AzureChatCompletion())
prompt = self.jinja2.get_prompt_string(file)
self.kernel.add_function(
    prompt=prompt,
    function_name=service_name,
    plugin_name=service_name,
    template_format="jinja2"
)
try:
    result = await self.kernel.invoke(function_name=service_id, plugin_name=service_id, **params)
except ContentFilterAIException as ex:
    self.logger.error('Content filter result', ex.content_filter_result)
    raise Exception(e) from e
except KernelInvokeException as ex:
    self.logger.exception(f"Kernel invoke exception: {ex.__cause__}")
    raise Exception(ex) from ex
except Exception as e:
    self.logger.exception(f"Error getting chat completion response: {e.__cause__}")
    raise Exception(e)

I get KernelInvokeException instead of ContentFilterAIException.

Expected behavior
I need the ContentFilterAIException to be able to handle the error properly.

Platform

  • OS: Windows
  • IDE: VS Code
  • Language: Python
  • Source: semantic-kernel 1.17.0

Additional context
I tried using this code #4802 but is not valid any more.

@afrancoc2000 afrancoc2000 added the bug Something isn't working label Dec 18, 2024
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Dec 18, 2024
@sphenry sphenry removed the triage label Dec 19, 2024
@moonbox3
Copy link
Contributor

Hi @afrancoc2000, the only time the ContentFilterAIException is raised is from a response from the model, as shown here:

https://github.com/microsoft/semantic-kernel/blob/926a59095b8b9a4a2f0b9cf616caf9a1045b360f/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_handler.py#L94C13-L98C26

Do you have the proper configuration set in your Azure OpenAI deployment to filter the prompt? https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter?tabs=warning%2Cuser-prompt%2Cpython-new

For context, what message does the KernelInvokeException include?

@moonbox3 moonbox3 self-assigned this Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel
Projects
Status: No status
Development

No branches or pull requests

4 participants