ollama 使用自动生成的函数调用不起作用,

ercv8c1e  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(60)

问题是什么?

#!/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()

使用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

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

使用运行中的llama3和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):


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

然后是一堆其他来自user_proxy (to chatbot)的空消息,最后退出。

操作系统

Docker

GPU

Nvidia

CPU

Intel

Ollama版本

ollama/ollama:0.1.32

ycl3bljg

ycl3bljg1#

我相信Ollama不支持直接调用函数。我使用LiteLLM + Ollama。
请参阅此页面以获取示例。

相关问题