jekalmin / extended_openai_conversation

Home Assistant custom component of conversation agent. It uses OpenAI to control your devices.
916 stars 129 forks source link

"Use Tools" #127

Closed TBTHEDBA closed 8 months ago

TBTHEDBA commented 8 months ago

What function does the selection "Use Tools" have in the menu?

jekalmin commented 8 months ago

Short answer: operation should be same as before.

Details

Tools are supported from OpenAI, and function calling is deprecated and has to be converted to tools.

We can convert it to tools and not use deprecated function calling, but there might be the case users want to use function calling instead of tools (such as model of LocalAI doesn't support tools). Use Tools is the option for users to choose this functionality.

It also supports parallel function calling.

Currently supported tools are only function calling, but more tools will probably be added.

스크린샷 2024-01-27 오후 10 35 46

Also, there's difference in message history. See example below.

Example

스크린샷 2024-01-27 오후 10 13 18

Message History

Functions

[
    {
        "role": "system",
        "content": "I want you to act as smart home manager of Home Assistant.\nI will provide ...",
    },
    {"role": "user", "content": "turn on livingroom and bedroom light"},
    {
        "role": "function",
        "name": "execute_services",
        "content": "[{'success': True}, {'success': True}]",
    },
    {
        "content": "Both living room and bedroom lights have been turned on.",
        "role": "assistant",
    }
]

Tools

[
    {
        "role": "system",
        "content": "I want you to act as smart home manager of Home Assistant.\nI will provide ...",
    },
    {"role": "user", "content": "turn on livingroom and bedroom light"},
    {
        "role": "assistant",
        "tool_calls": [
            {
                "id": "call_YImAIIgVyMCwiWw9fW7dC7fz",
                "function": {
                    "arguments": '{"list": [{"domain": "input_boolean", "service": "turn_on", "service_data": {"entity_id": "input_boolean.bedroom_light"}}]}',
                    "name": "execute_services",
                },
                "type": "function",
            },
            {
                "id": "call_wj9opVEXTiipYCXvKrtI1WgJ",
                "function": {
                    "arguments": '{"list": [{"domain": "input_boolean", "service": "turn_on", "service_data": {"entity_id": "input_boolean.livingroom_light"}}]}',
                    "name": "execute_services",
                },
                "type": "function",
            },
        ],
    },
    {
        "tool_call_id": "call_YImAIIgVyMCwiWw9fW7dC7fz",
        "role": "tool",
        "name": "execute_services",
        "content": "[{'success': True}]",
    },
    {
        "tool_call_id": "call_wj9opVEXTiipYCXvKrtI1WgJ",
        "role": "tool",
        "name": "execute_services",
        "content": "[{'success': True}]",
    },
    {
        "content": "Both the living room and bedroom lights have been turned on.",
        "role": "assistant",
    }
]

As you may see in the example, although result is the same, message history is different.

Using functions is more concise, but arguments of function are not stored in history. Hence LLM doesn't know what arguments of function was before. If you say "turn back off", it sometimes turns off only one light instead of two lights because LLM assumes again what lights to be turned off from message history.

Using tools is more verbose, but arguments of function are stored in history. Although it uses more tokens, it's clear that what LLM has done before because function call information is stored in message history. In this way, LLM should be smarter in searching what it has done before.

TBTHEDBA commented 8 months ago

Thank you for the write up! It's odd that they still push and support function use in custom GPTs with action schema. I built a custom GPT to interact with my home assistant environment using OpenAI API 3.0.0.

rgsaura commented 5 months ago

Hey @TBTHEDBA, could you explain more about your custom GPT? I'm trying to do the same 😄