July 11th, 2024

Introducing Python Function Choice Behavior: Streamlining AI Model Configuration

 

Introducing Function Choice Behavior

Today, we’re excited to introduce the Function Choice Behavior, an enhancement over the existing Function Call Behavior. This new feature streamlines configuration and supports specifying multiple required functions, simplifying code and enhancing functionality.

One significant advantage of Function Choice Behavior is its abstraction at the base PromptExecutionSettings level, allowing it to work across various AI Connectors that support function calling, not just OpenAI models. This change aims to make function calling more straightforward for our users, easily extendable for new models, and provide the appropriate level of abstraction over how models accept the tool_choice and tools attributes.

Transitioning to Function Choice Behavior

The Semantic Kernel team has been diligently working on Function Choice Behavior, which will eventually replace Function Call Behavior. This update is non-breaking, meaning users currently using FunctionCallBehavior can continue to do so while transitioning to FunctionChoiceBehavior. Deprecation tags and documentation are provided to guide users through the migration process.

Function Choice Behavior is similar to Function Call Behavior but is more streamlined and includes defining filters. Users can define functions and behaviors in configuration files such as JSON or YAML. Here’s an example of how to specify execution settings:

JSON Example:

{
  "execution_settings": {
    "chat": {
      "function_choice_behavior": {
        "type": "auto",
        "maximum_auto_invoke_attempts": 5,
        "functions": [
          "time.date",
          "time.time",
          "math.Add"
        ]
      }
    }
  }
}

YAML Example:

execution_settings:
  chat:
    function_choice_behavior:
      type: auto
      maximum_auto_invoke_attempts: 5
      functions:
        - time.date
        - time.time
        - math.Add

In this example, the time.date, time.time, and math.Add functions are included in calls to the model, unless updated later using the filters dictionary, similar to how it was configured with FunctionCallBehavior.

Customizing Function Choice Behavior

We offer the following ways to customize the behavior depending on your use case:

  • To allow the model to decide whether to call functions and, if so, which functions to call, you can set FunctionChoiceBehavior.Auto() or function_choice_behavior="auto".
  • To force the model to always call one or more functions, you can set FunctionChoiceBehavior.Required() or function_choice_behavior="required". The model will then select which function(s) to call. Note that the model will always make a tool call for the function(s) specified.
  • To disable function calling and force the model to only generate a user-facing message, you can set FunctionChoiceBehavior.None() or function_choice_behavior="none". The model will know of the available functions to use but will not auto invoke any functions as part of answering the query.

Behind the Scenes

In Python, we leverage Pydantic model validators to facilitate this transition. If someone sets up Function Call Behavior, we map it to Function Choice Behavior automatically. Whenever Function Call Behavior is updated, the validator runs, ensuring a seamless configuration change to FunctionChoiceBehavior, resulting in cleaner code.

Configuring Execution Settings

For the most streamlined approach, you can configure execution settings as follows:

execution_settings = OpenAIChatPromptExecutionSettings(
    service_id="chat",
    max_tokens=2000,
    temperature=0.7,
    top_p=0.8,
    function_choice_behavior="auto",
)

This configuration includes all defined kernel plugins/functions with the call to the model. If you prefer to filter the available plugins/functions sent to the model, you can do so like this:

execution_settings = OpenAIChatPromptExecutionSettings(
    service_id="chat",
    max_tokens=2000,
    temperature=0.7,
    top_p=0.8,
    function_choice_behavior=FunctionChoiceBehavior.Auto(filters={"included_plugins": ["math", "time"]}),
)

Sample Use Cases

We have provided new samples showcasing how you can set up execution settings with Function Choice Behavior. These samples demonstrate various configurations and use cases to help you get started.

Check out the following links to view the code samples:

All of our concept samples have been updated to use FunctionChoiceBehavior, but we have unit tests that exercise the old behavior to ensure no breaking changes.

By adopting Function Choice Behavior, we’re making it easier for our users to work with function calling in a more efficient and flexible manner. Stay tuned for more updates and samples to help you make the most of this new feature!

 

Author

Evan Mattson
Senior Software Engineer

0 comments

Discussion are closed.