|
| 1 | +--- |
| 2 | +title: AI Task entity |
| 3 | +sidebar_label: AI Task |
| 4 | +--- |
| 5 | + |
| 6 | +An AI Task entity provides a framework for AI-powered task execution in Home Assistant. It enables integrations to offer AI capabilities for generating data, content, or performing structured tasks based on natural language instructions. |
| 7 | + |
| 8 | +A AI Task entity is derived from the [`homeassistant.components.ai_task.AITaskEntity`](https://github.com/home-assistant/core/blob/dev/homeassistant/components/ai_task/entity.py). The entity state tracks the timestamp of the last activity for monitoring purposes. |
| 9 | + |
| 10 | +## Properties |
| 11 | + |
| 12 | +_This entity has no properties._ |
| 13 | + |
| 14 | +## Supported features |
| 15 | + |
| 16 | +Supported features are defined by using values in the `AITaskEntityFeature` enum and are combined using the bitwise or (`|`) operator. |
| 17 | + |
| 18 | +| Value | Description |
| 19 | +| ----- | ----------- |
| 20 | +| `GENERATE_DATA` | The entity can generate data based on natural language instructions. |
| 21 | + |
| 22 | +## Methods |
| 23 | + |
| 24 | +### Generate Data |
| 25 | + |
| 26 | +This method handles data generation tasks based on natural language instructions. |
| 27 | + |
| 28 | +```python |
| 29 | +from homeassistant.components.ai_task import AITaskEntity, GenDataTask, GenDataTaskResult |
| 30 | + |
| 31 | +class MyAITaskEntity(AITaskEntity): |
| 32 | + """Represent an AI Task entity.""" |
| 33 | + |
| 34 | + async def _async_generate_data( |
| 35 | + self, task: GenDataTask, chat_log: ChatLog |
| 36 | + ) -> GenDataTaskResult: |
| 37 | + """Handle a generate data task.""" |
| 38 | + # Process the task instructions and generate appropriate data |
| 39 | + # Use the chat_log to maintain conversation context. A common |
| 40 | + # pattern is to share an implementation between conversation and AI |
| 41 | + # task entities to process the chat log. |
| 42 | + # await self._async_handle_chat_log( |
| 43 | + # chat_log, |
| 44 | + # task.structure, |
| 45 | + # task.attachments |
| 46 | + # ) |
| 47 | + |
| 48 | + text = ... |
| 49 | + if not task.structure: |
| 50 | + return GenDataTaskResult( |
| 51 | + conversation_id=chat_log.conversation_id, |
| 52 | + data=text |
| 53 | + ) |
| 54 | + |
| 55 | + data = ... # process the text to match the structure |
| 56 | + return GenDataTaskResult( |
| 57 | + conversation_id=chat_log.conversation_id, |
| 58 | + data=data |
| 59 | + ) |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +A `GenDataTask` object contains the following data: |
| 64 | + |
| 65 | +| Name | Type | Description |
| 66 | +| ---- | ---- | ----------- |
| 67 | +| `task_name` | `str` | Name/identifier for the task |
| 68 | +| `instructions` | `str` | Natural language instructions for the AI |
| 69 | +| `structure` | `vol.Schema` \| `None` | Optional schema for structured output validation |
| 70 | +| `attachments` | `list[PlayMediaWithId]` | List of attachments to include in the task. |
| 71 | + |
| 72 | +## Structured Output Schema |
| 73 | + |
| 74 | +The `structure` parameter allows you to define the expected format of the generated data using Home Assistant's [selector system](https://www.home-assistant.io/docs/blueprint/selectors/): |
| 75 | + |
| 76 | +```python |
| 77 | +{ |
| 78 | + "yes_no_field": { |
| 79 | + "description": "Description of the field", |
| 80 | + "required": True/False, # Optional, defaults to False |
| 81 | + "selector": { |
| 82 | + "boolean": {} # Selector type |
| 83 | + } |
| 84 | + }, |
| 85 | + "text_field": { |
| 86 | + "description": "Description of the text field", |
| 87 | + "required": True/False, # Optional, defaults to False |
| 88 | + "selector": { |
| 89 | + "text": {} # Selector type |
| 90 | + } |
| 91 | + }, |
| 92 | + "number_field": { |
| 93 | + "description": "Description of the number field", |
| 94 | + "required": True/False, # Optional, defaults to False |
| 95 | + "selector": { |
| 96 | + "number": { |
| 97 | + "min": 18, # Optional minimum value |
| 98 | + "max": 100, # Optional maximum value |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | +} |
| 103 | +``` |
0 commit comments