[GH-ISSUE #3927] function calling with autogen does not work #2433

Closed
opened 2026-04-12 12:44:54 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @patrickwasp on GitHub (Apr 26, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/3927

What is the issue?

#!/usr/local/bin/python3.12

from typing import Literal

from pydantic import BaseModel, Field
from typing_extensions import Annotated

import autogen
from autogen.cache import Cache

# MODEL_NAME = "gpt-3.5-turbo"
# API_URL = "https://api.openai.com/v1/"
# API_KEY = "sk-XYZ"

MODEL_NAME = "llama3:8k"
API_URL = "http://10.4.4.207:11434/v1"
API_KEY = "ollama"

config_list = [
    {
        "model": MODEL_NAME,
        "base_url": API_URL,
        "api_key": API_KEY,
    }
]

llm_config = {
    "config_list": config_list,
    "timeout": 120,
}

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    is_termination_msg=lambda x: x.get("content", "")
    and x.get("content", "").rstrip().endswith("TERMINATE"),
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
)

chatbot = autogen.AssistantAgent(
    name="chatbot",
    system_message="For currency exchange tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",
    llm_config=llm_config,
)

CurrencySymbol = Literal["USD", "EUR"]


def exchange_rate(
    base_currency: CurrencySymbol, quote_currency: CurrencySymbol
) -> float:
    if base_currency == quote_currency:
        return 1.0
    elif base_currency == "USD" and quote_currency == "EUR":
        return 1 / 1.1
    elif base_currency == "EUR" and quote_currency == "USD":
        return 1.1
    else:
        raise ValueError(f"Unknown currencies {base_currency}, {quote_currency}")


class Currency(BaseModel):
    currency: Annotated[CurrencySymbol, Field(..., description="Currency symbol")]
    amount: Annotated[float, Field(0, description="Amount of currency", ge=0)]


@user_proxy.register_for_execution()
@chatbot.register_for_llm(description="Currency exchange calculator.")
def currency_calculator(
    base: Annotated[Currency, "Base currency: amount and currency symbol"],
    quote_currency: Annotated[CurrencySymbol, "Quote currency symbol"] = "USD",
) -> Currency:
    quote_amount = exchange_rate(base.currency, quote_currency) * base.amount
    return Currency(amount=quote_amount, currency=quote_currency)


def main():
    with Cache.disk() as cache:
        user_proxy.initiate_chat(
            chatbot,
            message="How much is 112.23 Euros in US Dollars?",
            summary_method="last_msg",
            cache=cache,
        )


if __name__ == "__main__":
    main()

output using gpt-3.5-turbo:

user_proxy (to chatbot):

How much is 112.23 Euros in US Dollars?

--------------------------------------------------------------------------------
chatbot (to user_proxy):

***** Suggested tool call (call_TdfMydJ9TeKBbz8QRE5ZHl2k): currency_calculator *****
Arguments: 
{"base":{"currency":"EUR","amount":112.23},"quote_currency":"USD"}
************************************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION currency_calculator...
user_proxy (to chatbot):

user_proxy (to chatbot):

***** Response from calling tool (call_TdfMydJ9TeKBbz8QRE5ZHl2k) *****
{"currency":"USD","amount":123.45300000000002}
**********************************************************************

--------------------------------------------------------------------------------
chatbot (to user_proxy):

112.23 Euros is equivalent to 123.45 US Dollars.

--------------------------------------------------------------------------------
user_proxy (to chatbot):



--------------------------------------------------------------------------------
chatbot (to user_proxy):

TERMINATE

--------------------------------------------------------------------------------

output using llama3 running with ollama:

user_proxy (to chatbot):

How much is 112.23 Euros in US Dollars?

--------------------------------------------------------------------------------
chatbot (to user_proxy):

To convert 112.23 Euros to US Dollars, I can use the provided function:

According to the exchange rate, 1 EUR is approximately equal to 1.22 USD.

Converting 112.23 EUR, we get:
112.23 EUR * 1.22 USD/EUR = 136.88 USD

So, 112.23 Euros are equivalent to approximately 136.88 US Dollars.

Please let me know if I should continue with the next task or reply TERMINATE when the currency exchange is done.

--------------------------------------------------------------------------------
user_proxy (to chatbot):



--------------------------------------------------------------------------------

followed by a bunch of other empty messages from user_proxy (to chatbot) before exiting.

OS

Docker

GPU

Nvidia

CPU

Intel

Ollama version

ollama/ollama:0.1.32

Originally created by @patrickwasp on GitHub (Apr 26, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/3927 ### What is the issue? ```python #!/usr/local/bin/python3.12 from typing import Literal from pydantic import BaseModel, Field from typing_extensions import Annotated import autogen from autogen.cache import Cache # MODEL_NAME = "gpt-3.5-turbo" # API_URL = "https://api.openai.com/v1/" # API_KEY = "sk-XYZ" MODEL_NAME = "llama3:8k" API_URL = "http://10.4.4.207:11434/v1" API_KEY = "ollama" config_list = [ { "model": MODEL_NAME, "base_url": API_URL, "api_key": API_KEY, } ] llm_config = { "config_list": config_list, "timeout": 120, } user_proxy = autogen.UserProxyAgent( name="user_proxy", is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"), human_input_mode="NEVER", max_consecutive_auto_reply=10, ) chatbot = autogen.AssistantAgent( name="chatbot", system_message="For currency exchange tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.", llm_config=llm_config, ) CurrencySymbol = Literal["USD", "EUR"] def exchange_rate( base_currency: CurrencySymbol, quote_currency: CurrencySymbol ) -> float: if base_currency == quote_currency: return 1.0 elif base_currency == "USD" and quote_currency == "EUR": return 1 / 1.1 elif base_currency == "EUR" and quote_currency == "USD": return 1.1 else: raise ValueError(f"Unknown currencies {base_currency}, {quote_currency}") class Currency(BaseModel): currency: Annotated[CurrencySymbol, Field(..., description="Currency symbol")] amount: Annotated[float, Field(0, description="Amount of currency", ge=0)] @user_proxy.register_for_execution() @chatbot.register_for_llm(description="Currency exchange calculator.") def currency_calculator( base: Annotated[Currency, "Base currency: amount and currency symbol"], quote_currency: Annotated[CurrencySymbol, "Quote currency symbol"] = "USD", ) -> Currency: quote_amount = exchange_rate(base.currency, quote_currency) * base.amount return Currency(amount=quote_amount, currency=quote_currency) def main(): with Cache.disk() as cache: user_proxy.initiate_chat( chatbot, message="How much is 112.23 Euros in US Dollars?", summary_method="last_msg", cache=cache, ) if __name__ == "__main__": main() ``` output using gpt-3.5-turbo: ```python3.12 autogen_tools_example.py user_proxy (to chatbot): How much is 112.23 Euros in US Dollars? -------------------------------------------------------------------------------- chatbot (to user_proxy): ***** Suggested tool call (call_TdfMydJ9TeKBbz8QRE5ZHl2k): currency_calculator ***** Arguments: {"base":{"currency":"EUR","amount":112.23},"quote_currency":"USD"} ************************************************************************************ -------------------------------------------------------------------------------- >>>>>>>> EXECUTING FUNCTION currency_calculator... user_proxy (to chatbot): user_proxy (to chatbot): ***** Response from calling tool (call_TdfMydJ9TeKBbz8QRE5ZHl2k) ***** {"currency":"USD","amount":123.45300000000002} ********************************************************************** -------------------------------------------------------------------------------- chatbot (to user_proxy): 112.23 Euros is equivalent to 123.45 US Dollars. -------------------------------------------------------------------------------- user_proxy (to chatbot): -------------------------------------------------------------------------------- chatbot (to user_proxy): TERMINATE -------------------------------------------------------------------------------- ``` output using llama3 running with ollama: ``` user_proxy (to chatbot): How much is 112.23 Euros in US Dollars? -------------------------------------------------------------------------------- chatbot (to user_proxy): To convert 112.23 Euros to US Dollars, I can use the provided function: According to the exchange rate, 1 EUR is approximately equal to 1.22 USD. Converting 112.23 EUR, we get: 112.23 EUR * 1.22 USD/EUR = 136.88 USD So, 112.23 Euros are equivalent to approximately 136.88 US Dollars. Please let me know if I should continue with the next task or reply TERMINATE when the currency exchange is done. -------------------------------------------------------------------------------- user_proxy (to chatbot): -------------------------------------------------------------------------------- ``` followed by a bunch of other empty messages from `user_proxy (to chatbot)` before exiting. ### OS Docker ### GPU Nvidia ### CPU Intel ### Ollama version ollama/ollama:0.1.32
GiteaMirror added the bug label 2026-04-12 12:44:54 -05:00
Author
Owner

@marklysze commented on GitHub (Apr 28, 2024):

I believe Ollama doesn't support function calling directly. I use LiteLLM + Ollama.

See this page for an example.

<!-- gh-comment-id:2081430435 --> @marklysze commented on GitHub (Apr 28, 2024): I believe Ollama doesn't support function calling directly. I use LiteLLM + Ollama. [See this page for an example](https://microsoft.github.io/autogen/docs/topics/non-openai-models/local-litellm-ollama/#example-with-function-calling).
Author
Owner

@jmorganca commented on GitHub (Jul 26, 2024):

Hi there, this should be supported now - note that OpenAI-compatible endpoint does not support streaming yet but it is being worked on

<!-- gh-comment-id:2251744068 --> @jmorganca commented on GitHub (Jul 26, 2024): Hi there, this should be supported now - note that OpenAI-compatible endpoint does not support streaming yet but it is being worked on
Author
Owner

@opqpop commented on GitHub (Jul 30, 2024):

Thanks @jmorganca, we're trying to move away from LiteLLM + Ollama to support function calling in autogen. Do you perhaps have a link or example on how to get it working?

I tried following the "Autogen" example @ https://ollama.com/blog/openai-compatibility but it just hangs. I was only able to get chat completions working.

<!-- gh-comment-id:2257331366 --> @opqpop commented on GitHub (Jul 30, 2024): Thanks @jmorganca, we're trying to move away from LiteLLM + Ollama to support function calling in autogen. Do you perhaps have a link or example on how to get it working? I tried following the "Autogen" example @ https://ollama.com/blog/openai-compatibility but it just hangs. I was only able to get chat completions working.
Author
Owner

@marklysze commented on GitHub (Jul 30, 2024):

Thanks @jmorganca, we're trying to move away from LiteLLM + Ollama to support function calling in autogen. Do you perhaps have a link or example on how to get it working?

I tried following the "Autogen" example @ https://ollama.com/blog/openai-compatibility but it just hangs. I was only able to get chat completions working.

Hey @opqpop, I'm working on an Ollama client class in AutoGen, it has tool calling using Ollama's tool calling as well as a manually handled tool calling option.

See:
https://github.com/microsoft/autogen/pull/3056

<!-- gh-comment-id:2257357255 --> @marklysze commented on GitHub (Jul 30, 2024): > Thanks @jmorganca, we're trying to move away from LiteLLM + Ollama to support function calling in autogen. Do you perhaps have a link or example on how to get it working? > > I tried following the "Autogen" example @ https://ollama.com/blog/openai-compatibility but it just hangs. I was only able to get chat completions working. Hey @opqpop, I'm working on an Ollama client class in AutoGen, it has tool calling using Ollama's tool calling as well as a manually handled tool calling option. See: https://github.com/microsoft/autogen/pull/3056
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#2433