Files
ha-text-ai/README.md
T

349 lines
11 KiB
Markdown
Raw Normal View History

2024-11-22 01:57:21 +03:00
# 🤖 HA Text AI for Home Assistant
2024-11-14 18:39:06 +03:00
2024-11-19 19:03:51 +03:00
<div align="center">
2024-11-18 10:59:06 +03:00
2024-11-25 23:45:28 +03:00
![GitHub release](https://img.shields.io/github/release/smkrv/ha-text-ai.svg?style=flat-square) ![GitHub downloads](https://img.shields.io/github/downloads/smkrv/ha-text-ai/total.svg?style=flat-square) ![GitHub stars](https://img.shields.io/github/stars/smkrv/ha-text-ai.svg?style=social) ![GitHub last commit](https://img.shields.io/github/last-commit/smkrv/ha-text-ai.svg?style=flat-square) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) [![hacs_badge](https://img.shields.io/badge/HACS-Custom-41BDF5.svg?style=flat-square)](https://github.com/hacs/integration)
2024-11-19 19:30:42 +03:00
2024-11-23 18:59:27 +03:00
<img src="https://github.com/smkrv/ha-text-ai/blob/3e3ec45b195c92989434fde40ae110027f4ea124/misc/icons/icon.png" alt="HA Text AI" width="140"/>
2024-11-22 11:39:43 +03:00
2024-11-22 02:04:20 +03:00
### Advanced AI Integration for Home Assistant with multi-provider support
2024-11-18 10:59:06 +03:00
</div>
<p align="center">
2024-11-19 19:03:51 +03:00
Transform your smart home experience with powerful AI assistance powered by multiple AI providers including OpenAI GPT and Anthropic Claude models. Get intelligent responses, automate complex scenarios, and enhance your home automation with advanced natural language processing.
2024-11-25 23:43:05 +03:00
2024-11-18 10:59:06 +03:00
</p>
2024-11-18 10:54:24 +03:00
2024-11-25 16:55:12 +03:00
---
2024-11-25 23:48:55 +03:00
2024-11-25 23:43:05 +03:00
> [!IMPORTANT]
2024-11-25 17:03:29 +03:00
> 🚧 ALPHA VERSION 🚧
2024-11-25 23:50:33 +03:00
> Expect: potential bugs, frequent changes, incomplete features.
> 🤝 Community Driven
2024-11-25 23:49:42 +03:00
>
2024-11-25 23:50:33 +03:00
> <a href="https://community.home-assistant.io/t/ha-text-ai-transforming-home-automation-with-multi-provider-language-models/799741"><img src="https://img.shields.io/badge/Community-blue?style=for-the-badge&logo=homeassistant&logoColor=white&color=03a9f4"/></a>
2024-11-25 16:40:55 +03:00
2024-11-19 12:45:26 +03:00
## 🌟 Features
2024-11-14 18:39:06 +03:00
2024-11-19 19:03:51 +03:00
- 🧠 **Multi-Provider AI Integration**:
- Support for OpenAI GPT models
- Anthropic Claude integration
- Custom API endpoints
- Flexible model selection
2024-11-22 02:00:42 +03:00
2024-11-19 19:03:51 +03:00
- 💬 **Advanced Language Processing**:
2024-11-19 14:06:32 +03:00
- Context-aware responses
- Multi-turn conversations
2024-11-19 19:03:51 +03:00
- Custom system instructions
2024-11-19 14:06:32 +03:00
- Natural conversation flow
2024-11-22 02:00:42 +03:00
2024-11-19 19:03:51 +03:00
- 📝 **Enhanced Memory Management**:
2024-11-19 14:06:32 +03:00
- Persistent conversation history
- Context-aware responses
- Customizable history limits
2024-11-19 19:03:51 +03:00
- Model-specific filtering
2024-11-22 02:00:42 +03:00
2024-11-19 19:03:51 +03:00
-**Performance Optimization**:
2024-11-19 14:06:32 +03:00
- Efficient token usage
2024-11-19 19:03:51 +03:00
- Smart rate limiting
2024-11-19 14:06:32 +03:00
- Response caching
2024-11-19 19:03:51 +03:00
- Request interval control
2024-11-22 02:00:42 +03:00
2024-11-19 14:06:32 +03:00
- 🎯 **Advanced Customization**:
2024-11-19 19:03:51 +03:00
- Per-request model selection
- Adjustable parameters
2024-11-19 14:06:32 +03:00
- Custom system prompts
2024-11-19 19:03:51 +03:00
- Temperature control
2024-11-22 02:00:42 +03:00
2024-11-19 14:06:32 +03:00
- 🔒 **Enhanced Security**:
- Secure API key storage
- Rate limiting protection
- Error handling
2024-11-19 19:03:51 +03:00
- Usage monitoring
2024-11-22 02:00:42 +03:00
2024-11-19 19:03:51 +03:00
- 🎨 **Improved User Experience**:
2024-11-19 14:06:32 +03:00
- Intuitive configuration UI
- Detailed sensor attributes
- Rich service interface
2024-11-19 19:03:51 +03:00
- Model selection UI
2024-11-22 02:00:42 +03:00
2024-11-19 14:06:32 +03:00
- 🔄 **Automation Integration**:
- Event-driven responses
- Conditional logic support
- Template compatibility
2024-11-19 19:03:51 +03:00
- Model-specific automation
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
## 📋 Prerequisites
2024-11-18 10:59:06 +03:00
2024-11-22 01:56:36 +03:00
- Home Assistant 2023.11 or later
- Active API key from:
2024-11-19 19:03:51 +03:00
- OpenAI ([Get key](https://platform.openai.com/account/api-keys))
- Anthropic ([Get key](https://console.anthropic.com/))
2024-11-25 16:57:14 +03:00
- OpenRouter ([Get key](https://openrouter.ai/keys))
2024-11-19 12:45:26 +03:00
- Python 3.9 or newer
2024-11-19 14:06:32 +03:00
- Stable internet connection
2024-11-18 10:59:06 +03:00
2024-11-22 01:56:36 +03:00
### Configuration Options
- API Provider (OpenAI/Anthropic)
- API Key (provider-specific)
- Model Selection (flexible, provider-specific models)
- Temperature (Creativity control, 0.0-2.0)
- Max Tokens (Response length limit)
- Request Interval (API call throttling)
- Custom API Endpoint (optional)
2024-11-25 16:37:57 +03:00
#### ⓘ Potentially Compatible Providers
2024-11-25 16:34:36 +03:00
The integration is designed to be flexible and may work with other providers offering OpenAI-compatible APIs:
- Groq
- Together AI
- Perplexity AI
- Mistral AI
- Google AI
- Local AI servers (like Ollama)
- Custom OpenAI-compatible endpoints
2024-11-25 16:57:14 +03:00
#### Additional Notes
2024-11-25 16:34:36 +03:00
- Not all providers guarantee full compatibility
- Performance may vary between providers
- Check individual provider's documentation
2024-11-25 16:37:57 +03:00
- Ensure your API key has sufficient credits/quota
2024-11-25 16:34:36 +03:00
2024-11-25 16:57:14 +03:00
#### Provider Compatibility Requirements
2024-11-25 16:34:36 +03:00
To be compatible, a provider should support:
- OpenAI-like REST API structure
- JSON request/response format
- Standard authentication method
- Similar model parameter handling
2024-11-19 14:06:32 +03:00
## ⚡ Installation
2024-11-22 01:56:36 +03:00
### HACS Installation (Recommended)
2024-11-25 17:10:38 +03:00
<a href="https://my.home-assistant.io/redirect/hacs_repository/?owner=smkrv&repository=ha-text-ai&category=Integration"><img src="https://my.home-assistant.io/badges/hacs_repository.svg" width="170" height="auto"></a>
2024-11-22 01:56:36 +03:00
1. Open HACS in Home Assistant
2. Click on "Integrations"
3. Click "..." in top right corner
4. Select "Custom repositories"
5. Add repository URL: `https://github.com/smkrv/ha-text-ai`
6. Choose "Integration" as category
7. Click "Download"
8. Restart Home Assistant
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### Manual Installation
2024-11-19 14:06:32 +03:00
1. Download the latest release
2. Extract and copy `custom_components/ha_text_ai` to your `custom_components` directory
2024-11-19 12:45:26 +03:00
3. Restart Home Assistant
2024-11-19 14:06:32 +03:00
4. Add configuration via UI or YAML
## ⚙️ Configuration
### Via UI (Recommended)
1. Go to Settings → Devices & Services
2. Click "Add Integration"
3. Search for "HA Text AI"
4. Follow the configuration steps
### Via YAML
2024-11-18 10:59:06 +03:00
```yaml
2024-11-19 00:51:57 +03:00
ha_text_ai:
2024-11-22 01:56:36 +03:00
api_provider: openai # or anthropic
2024-11-19 19:03:51 +03:00
api_key: !secret ai_api_key
2024-11-22 01:57:21 +03:00
model: gpt-4o-mini
2024-11-19 14:06:32 +03:00
temperature: 0.7
max_tokens: 1000
request_interval: 1.0
2024-11-19 19:03:51 +03:00
api_endpoint: https://api.openai.com/v1 # optional, for custom endpoints
system_prompt: |
You are a home automation expert assistant.
Focus on practical and efficient solutions.
2024-11-18 10:59:06 +03:00
```
2024-11-19 12:45:26 +03:00
## 🛠️ Available Services
### ask_question
2024-11-18 10:59:06 +03:00
```yaml
2024-11-19 00:51:57 +03:00
service: ha_text_ai.ask_question
2024-11-18 10:59:06 +03:00
data:
2024-11-19 12:45:26 +03:00
question: "What's the optimal temperature for sleeping?"
2024-11-19 19:03:51 +03:00
model: "claude-3-sonnet" # optional
2024-11-19 12:45:26 +03:00
temperature: 0.5 # optional
max_tokens: 500 # optional
2024-11-25 15:42:04 +03:00
context_messages: 10 #optional, number of previous messages to include in context, default: 5
2024-11-19 19:03:51 +03:00
system_prompt: "You are a sleep optimization expert" # optional
2024-11-18 10:59:06 +03:00
```
2024-11-19 12:45:26 +03:00
### set_system_prompt
```yaml
service: ha_text_ai.set_system_prompt
data:
2024-11-19 14:06:32 +03:00
prompt: |
You are a home automation expert focused on:
1. Energy efficiency
2. Comfort optimization
3. Security considerations
Provide practical, actionable advice.
2024-11-19 12:45:26 +03:00
```
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### clear_history
```yaml
service: ha_text_ai.clear_history
```
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### get_history
```yaml
service: ha_text_ai.get_history
data:
limit: 5 # optional
2024-11-25 15:42:04 +03:00
filter_model: "gpt-4o" # optional
2024-11-19 12:45:26 +03:00
```
2024-11-25 17:59:17 +03:00
### 🏷️ HA Text AI Sensor Naming Convention
#### Sensor Name Structure
```yaml
# Always starts with 'sensor.ha_text_ai_'
# You define only the part after the underscore
sensor.ha_text_ai_YOUR_UNIQUE_SUFFIX
# Examples:
sensor.ha_text_ai_gpt # GPT-based sensor
sensor.ha_text_ai_claude # Claude-based sensor
sensor.ha_text_ai_gpt # Custom suffix
```
#### Response Retrieval
```yaml
# Use your specific sensor name
{{ state_attr('sensor.ha_text_ai_gpt', 'response') }}
```
#### Practical Usage
```yaml
automation:
- alias: "AI Response with Custom Sensor"
action:
- service: ha_text_ai.ask_question
data:
question: "Home automation advice"
- service: notify.mobile
data:
message: >
AI Tip:
{{ state_attr('sensor.ha_text_ai_gpt', 'response') }}
```
### 💡 Naming Rules
- Prefix is always `sensor.ha_text_ai_`
- Add your unique identifier after the underscore
- Use lowercase
- No spaces allowed
- Keep it descriptive but concise
### 🔍 HA Text AI Sensor Attributes
#### Model and Provider Information
```yaml
# Model details
{{ state_attr('sensor.ha_text_ai_gpt', 'Model') }} # gpt-4o
{{ state_attr('sensor.ha_text_ai_gpt', 'Api provider') }} # openai
{{ state_attr('sensor.ha_text_ai_gpt', 'Last model') }} # gpt-4o
```
#### System Status
```yaml
# Operational status
{{ state_attr('sensor.ha_text_ai_gpt', 'Api status') }} # ready
{{ state_attr('sensor.ha_text_ai_gpt', 'Is processing') }} # false
{{ state_attr('sensor.ha_text_ai_gpt', 'Is rate limited') }} # false
{{ state_attr('sensor.ha_text_ai_gpt', 'Endpoint status') }} # ready
```
#### Performance Metrics
```yaml
# Request and performance statistics
{{ state_attr('sensor.ha_text_ai_gpt', 'Successful requests') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Failed requests') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Average latency') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Max latency') }} # 0
```
#### Conversation and Token Usage
```yaml
# Conversation and token details
{{ state_attr('sensor.ha_text_ai_gpt', 'History size') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Total tokens') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Prompt tokens') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Completion tokens') }} # 0
```
#### Last Interaction Details
```yaml
# Last interaction information
{{ state_attr('sensor.ha_text_ai_gpt', 'Response') }} # Last AI response
{{ state_attr('sensor.ha_text_ai_gpt', 'Question') }} # Last asked question
{{ state_attr('sensor.ha_text_ai_gpt', 'Last timestamp') }} # Timestamp
```
#### System Health
```yaml
# System health and maintenance
{{ state_attr('sensor.ha_text_ai_gpt', 'Total errors') }} # 0
{{ state_attr('sensor.ha_text_ai_gpt', 'Is maintenance') }} # false
{{ state_attr('sensor.ha_text_ai_gpt', 'Uptime') }} # 547,58
```
### 💡 Pro Tips
- Always check attribute existence
- Use these attributes for monitoring and automation
- Some values might be 0 or empty initially
2024-11-19 14:06:32 +03:00
## 📘 FAQ
2024-11-19 19:03:51 +03:00
**Q: Which AI providers are supported?**
A: Currently OpenAI (GPT models) and Anthropic (Claude models) are supported, with more providers planned.
**Q: How can I reduce API costs?**
A: Use GPT-3.5-Turbo or Claude-3-Sonnet for most queries, implement caching, and optimize token usage.
2024-11-22 01:56:36 +03:00
**Q: Are there limitations on the number of requests?**
A: Depends on your API provider's plan. We recommend monitoring usage and implementing request throttling via `request_interval` configuration.
2024-11-19 19:31:26 +03:00
2024-11-22 01:56:36 +03:00
**Q: Can I use custom models?**
2024-11-19 19:03:51 +03:00
A: Yes, you can configure custom endpoints and use any compatible model by specifying it in the configuration.
**Q: How do I switch between different AI providers?**
A: Simply change the model parameter in your configuration or service calls to use the desired provider's model.
2024-11-19 14:06:32 +03:00
**Q: How can I reduce API costs?**
A: Use GPT-3.5-Turbo for most queries, implement caching, and optimize token usage.
**Q: Is my data secure?**
2024-11-22 02:00:42 +03:00
A: Yes, your data is secure. The system operates entirely on your local machine, keeping your data under your control. API keys are stored securely and all external communications use encrypted connections.
2024-11-19 14:06:32 +03:00
2024-11-25 15:42:04 +03:00
**Q: How do context messages work?**
A: Context messages allow the AI to remember and reference previous conversation history. By default, 5 previous messages are included, but you can customize this from 1 to 20 messages to control the conversation depth and token usage.
2024-11-19 12:45:26 +03:00
## 🤝 Contributing
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
Contributions welcome! Please read our [Contributing Guide](CONTRIBUTING.md).
2024-11-18 10:59:06 +03:00
1. Fork the repository
2024-11-19 14:06:32 +03:00
2. Create feature branch (`git checkout -b feature/Enhancement`)
3. Commit changes (`git commit -m 'Add Enhancement'`)
4. Push branch (`git push origin feature/Enhancement`)
5. Open Pull Request
2024-11-19 12:45:26 +03:00
## 📝 License
2024-11-19 14:06:32 +03:00
MIT License - see [LICENSE](LICENSE) for details.
2024-11-14 18:39:06 +03:00
2024-11-18 10:54:24 +03:00
---
2024-11-14 18:39:06 +03:00
2024-11-18 10:59:06 +03:00
<div align="center">
2024-11-14 18:39:06 +03:00
2024-11-19 12:45:26 +03:00
Made with ❤️ for the Home Assistant Community
2024-11-14 18:39:06 +03:00
2024-11-19 14:06:32 +03:00
[Report Bug](https://github.com/smkrv/ha-text-ai/issues) · [Request Feature](https://github.com/smkrv/ha-text-ai/issues)
2024-11-18 10:59:06 +03:00
</div>