jekalmin / extended_openai_conversation

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

Feature Request: Grocy example to consume products #113

Open RL-Fields opened 8 months ago

RL-Fields commented 8 months ago

It’d be great to be able to consume Grocy items by name. Currently there is an example to execute chores by ID, and I’ve used this as base to consume products by ID. However, it’s impossible to remember the ID numbers of all the products

`- spec: name: consume_product_from_stock description: Use this function to consume a product in Home Assistant. parameters: type: object properties: product_id: type: string description: The ID of the product to be executed. amount: type: string description: the number to consume required:

jekalmin commented 8 months ago

Thanks for reporting an issue. Although I'm not using grocy, the way to resolve is same as others. You need to let model know mapping of id and name.

Try to add a function like below, and see if get_inventory_stock and consume_product_from_stock is called sequentially. (make sure Maximum function calls per conversation option to be greater than 2)

Function

- spec:
    name: get_inventory_stock
    description: Use this function to retrieve the inventory entries data.
    parameters:
      type: object
      properties: {}
  function:
    type: template
    value_template: >-
      {% set data = states['sensor.grocy_stock'].attributes['products'] | list %}
      ```csv
      name,id,product_group_id,available_amount,amount_aggregated,amount_opened,amount_opened_aggregated,is_aggregated_amount,best_before_date
      {% for product in data -%}
      {{ product['name'] }},{{ product['id'] }},{{ product['product_group_id'] }},
      {%- if product['available_amount'] == 0 -%}
      {{ product['amount_aggregated'] }},
      {%- else -%}
      {{ product['available_amount'] }},
      {%- endif -%}
      {{ product['amount_aggregated'] }},{{ product['amount_opened'] }},{{ product['amount_opened_aggregated'] }},{{ product['is_aggregated_amount'] }},{{ product['best_before_date'] }}
      {% endfor -%}

If it doesn't work as expected, try by adding it in prompt rather than function.

**Prompt**

Product: {% set data = states['sensor.grocy_stock'].attributes['products'] | list %}

name,id,product_group_id,available_amount,amount_aggregated,amount_opened,amount_opened_aggregated,is_aggregated_amount,best_before_date
{% for product in data -%}
{{ product['name'] }},{{ product['id'] }},{{ product['product_group_id'] }},
{%- if product['available_amount'] == 0 -%}
{{ product['amount_aggregated'] }},
{%- else -%}
{{ product['available_amount'] }},
{%- endif -%}
{{ product['amount_aggregated'] }},{{ product['amount_opened'] }},{{ product['amount_opened_aggregated'] }},{{ product['is_aggregated_amount'] }},{{ product['best_before_date'] }}
{% endfor -%}