microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps

Home Page:https://aka.ms/semantic-kernel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python: Bug: Kernel functions cannot have optional arguments.

q-stefanmuscalu opened this issue · comments

Describe the bug
I want to write a kernel function with an optional argument. It does not work because semantic kernel ignores optional arguments when calling the /chat/completions endpoint.

To Reproduce
Steps to reproduce the behavior:

  1. Define a kernel function like this:
class SearchPlugin():

    @kernel_function(
        name="Search",
        description="Search for people",
    )
    def find_person(
        self,
        first_name: str,
        last_name: str,
        country_code: Optional[str]
    ) -> str:
        return "nothing found"
  1. Ask the agent to search for john doe
  2. Observe the error saying An error occurred while invoking the function SearchPlugin-Search: SearchPlugin.find_person() missing 1 required positional argument: 'country_code'"

The error happens because the metadata sent to the LLM is missing the country_code argument:

{
    "type": "function",
    "function": {
        "name": "SearchPlugin-Search",
        "description": "Search for people",
        "parameters": {
            "type": "object",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                }
            },
            "required": [
                "first_name",
                "last_name"
            ]
        }
    }
}

Expected behavior
I would expect the metadata to look like this

{
    "type": "function",
    "function": {
        "name": "SearchPlugin-Search",
        "description": "Search for people",
        "parameters": {
            "type": "object",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                },
                "country_code": {
                    "type": "string"
                }
            },
            "required": [
                "first_name",
                "last_name"
            ]
        }
    }
}

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

Platform

  • OS: Mac
  • IDE: PyCharm
  • Language: Python
  • Source: semantic kernel 1.2.0, python 3.11, Azure Open AI

Additional context
Add any other context about the problem here.