microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
30.73k stars 4.48k forks source link

[Bug]: Function calling with Azure requires Parameters, which should be optional. #1051

Open jspv opened 8 months ago

jspv commented 8 months ago

Describe the bug

Per the OpenAPI docs "Omitting parameters defines a function with an empty parameter list.", omitting Parameters works fine when using an OpenAI model direct, but when using an Azure OpenAI model autogen returns BadRequestError: Error code: 400 - {'error': {'message': "'parameters' is a required property - 'functions.0'", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Steps to reproduce

the following code will demonstrate the problem.

from config import settings

from autogen import (
    Agent,
    AssistantAgent,
    UserProxyAgent,
    GroupChat,
    GroupChatManager,
)

config_list = [{'api_key': '**your azure key**',
                'api_type': 'azure', 
                'model': 'gpt-4', 
                'api_version': '2023-09-01-preview', 
                # 'api_version': '2023-12-01-preview',
                'base_url': '**your deployment**'}]

# Note - was testing the most recent api_version, no difference

# config_list = [{'api_key': '**your OpenAI api key**',
#                 'model': 'gpt-4-1106-preview'}]

from datetime import datetime

def get_current_time(location="not used"):
    current_time = datetime.now()
    current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S")
    return current_time_str

def is_termination_msg(message: dict) -> bool:
    """Returns True if the message contains the word TERMINATE"""
    return "TERMINATE" in message["content"]

# version with parameters
functions = [
        {
        "name": "get_current_time",
        "description": "retrieves the current time as a string",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "location to get the time for"
                }
            }
        }
        }
]

# version without parameters
functions = [
        {
        "name": "get_current_time",
        "description": "retrieves the current time as a string",
        }
]

llm_config = {"config_list": config_list, "cache_seed": 42, "stream": True}
llm_config_with_functions = {"config_list": config_list, 
                             "cache_seed": 42, 
                             "stream": True, 
                             "functions": functions}

assistant = AssistantAgent(
            "assistant", 
            is_termination_msg=is_termination_msg,
            llm_config=llm_config
        )

writer = AssistantAgent(
    system_message=(
        "You are a writer of academic papers, you will be given a topic or"
        " prompt and you will provide an academic paper on that topic.  You"
        " work closely with an editor, when your editor rejects a paper, you"
        " will be given feedback on how to improve the paper and you will be"
        " expected to improve the paper and resubmit it to the editor.  You will"
        " always start your papers with the current date so make sure to get that"
        " date as the first thing you do."
    ),
    name="writer",
    llm_config=llm_config_with_functions,
    is_termination_msg=is_termination_msg,
)

editor = AssistantAgent(
    name="editor",
    system_message=(
        "You are a skilled editor of academic papers, has high standards and"
        " expects papers to be at a Masters level.  When papers are presented"
        " to you by a writer, you will either accept the paper or reject it. "
        " If you reject the paper, you will state immediately 'I reject this"
        " paper' and you then will provide specific feedback to the writer on"
        " how to improve the paper.  If you accept the paper, you will"
        " immediately reply with the single word 'TERMINATE'.  If the writer"
        " later sumbits an updated version, review it again and provide"
        " feedback. To be accepted, the paper must have sufficient lenght, be"
        " free of grammatical errors, have referernces and citations, and"
        " demonstrate both creativity and a mastery of the subject matter. "
        " Papers should be at least 4000 words, if papers are not long enough,"
        " provide suggestions on which paragraps and topics to expand the"
        " paper.  Be specific and explicit.  Again, if you accept the paper,"
        " you will reply with the word 'TERMINATE' in all capitals, this is"
        " very important."
    ),
    llm_config=llm_config_with_functions
)

user_proxy = UserProxyAgent(
    name="User_proxy",
    system_message="A human admin.",
    human_input_mode="TERMINATE",
    is_termination_msg=is_termination_msg,
)

user_proxy.register_function(
    function_map={
        "get_current_time": get_current_time,
    }
)

groupchat = GroupChat(
    agents=[user_proxy, writer, editor],
    messages=[],
    max_round=12,
)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)

user_proxy.initiate_chat(manager, clear_history=True,  message = "platypus, how long they live and why they die")

Expected Behavior

Regardless of which config_list, calling the function with no parameters should be supported.

Screenshots and logs

No response

Additional Information

Autogen: 0.2.2 b1adac5 (latest main)

rickyloynd-microsoft commented 8 months ago

@kevin666aa

davorrunje commented 8 months ago

We could inject an optional parameter in the @register_for_llm decorator (once #1018 is merged). That's very easy to implement.

thinkall commented 2 months ago

Hi @jspv, is the issue resolved with latest AutoGen?