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: Bug: VertexAIChatCompletion can't handle classes or unions in kernel functions. #8110

Closed
q-stefanmuscalu opened this issue Aug 14, 2024 · 2 comments · Fixed by #8674
Assignees
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@q-stefanmuscalu
Copy link

Describe the bug
Kernel functions can't handle classes or unions when using VertexAIChatCompletion

To Reproduce
Steps to reproduce the behavior:

  1. Enable auto function calling
    VertexAIChatPromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto())
  2. Define a plugin that returns a class:
class PersonDetails(KernelBaseModel):
    age: int
    address: str

class SearchPlugin:

    @kernel_function(
        name="SearchPerson",
        description="Search details of a person given their id."
    )
    def search_person(
        self,
        person_id: Annotated[str, "The person ID to search"]
    ) -> Annotated[PersonDetails, "The details of the person"]:
        print(f"\n\n called with person id {person_id} \n\n")
        return PersonDetails(age=42, address="123 Main Street London UK")
  1. Add the plugin to the kernel and ask it to search for something:
    kernel = Kernel()

    chat_function = kernel.add_function(
        prompt="{{$chat_history}}{{$user_input}}",
        plugin_name="ChatBot",
        function_name="Chat",
    )

    kernel.add_plugin(SearchPlugin(), plugin_name="SearchPlugin")
    
        chat_completion = VertexAIChatCompletion(
        project_id="???",
        region="???",
        gemini_model_id="gemini-1.5-pro"
    )

    kernel.add_service(chat_completion)

    kernel_args = KernelArguments(
        settings=VertexAIChatPromptExecutionSettings(
            function_choice_behavior=FunctionChoiceBehavior.Auto(
                filters={"excluded_plugins": ["ChatBot"]}
            )
        )
    )

    kernel_args["user_input"] = "Search for person id 9b3f6e40"
    kernel_args["chat_history"] = ChatHistory()

    result = await kernel.invoke(chat_function, arguments=kernel_args)
  1. See error:
.venv/lib/python3.10/site-packages/semantic_kernel/functions/kernel_function_from_prompt.py", line 177, in _invoke_internal
    raise FunctionExecutionException(f"Error occurred while invoking function {self.name}: {exc}") from exc
semantic_kernel.exceptions.function_exceptions.FunctionExecutionException: Error occurred while invoking function Chat: Unable to coerce value: PersonDetails(age=42, address='123 Main Street London UK')

Setting the person_id to a class instead of a string results in a different error:

    def search_person(
        self,
        person_id: Annotated[PersonId, "The person ID to search"]
    ) -> Annotated[str, "The details of the person"]:

Error:

.venv/lib/python3.10/site-packages/pydantic/main.py", line 568, in model_validate
    return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 1 validation error for PersonId
  Input should be a valid dictionary or instance of PersonId [type=model_type, input_value='9b3f6e40', input_type=str]

A different issue, but similar to the above:
I get a similar error if I use a union in the function argument:

    def search_person(
        self,
        person_id: Annotated[str | int, "The person ID to search"]
    ) -> Annotated[str, "The details of the person"]

Error:

.venv/lib/python3.10/site-packages/semantic_kernel/functions/kernel_function_from_prompt.py", line 177, in _invoke_internal
    raise FunctionExecutionException(f"Error occurred while invoking function {self.name}: {exc}") from exc
semantic_kernel.exceptions.function_exceptions.FunctionExecutionException: Error occurred while invoking function Chat: Protocol message Schema has no "anyOf" field.

Expected behavior
I would expect the functions to handle classes and unions.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

  • OS: Mac
  • IDE: PyCharm
  • Language: Python
  • Source: SK version 1.5.0

Additional context
Gemini with VertexAI, Python 3.10, Gemini model gemini-1.5-pro

@q-stefanmuscalu q-stefanmuscalu added the bug Something isn't working label Aug 14, 2024
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Aug 14, 2024
@moonbox3 moonbox3 removed the triage label Aug 14, 2024
@TaoChenOSU
Copy link
Contributor

TaoChenOSU commented Sep 11, 2024

Thanks for reporting! I was able to reproduce the issues and working on fixing them.

There are 3 issues reported:

  1. The return parameter of a function is a complex type: This is a bug and will be fixed.
  2. The input parameter of a function is a complex type: This is not reproducible.
  3. The input of the parameter of a function is a union type: Unfortunately, Google function calling currently only supports the following types: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/function-calling#schema

@q-stefanmuscalu
Copy link
Author

@TaoChenOSU thank you for looking into this! I will try the next release and let you know if the complex input parameter is still an issue.

github-merge-queue bot pushed a commit that referenced this issue Sep 11, 2024
…el functions (#8674)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Address #8110

### Description
1. Bug fix: parse function result to string
2. Integration tests added against complex type in function calling.

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
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: Sprint: Done
4 participants