Table of Contents
This is a custom integration for Home Assistant which allows for sending prompts to Mistral AI
Mistral AI is a generative AI created in europe (france). It offers different models of their ai, each specialized for different topics. This ai had high scores in several benchmarks, even beating previous versions of Chat GPT. Right now using Mistral AI is free.
Before you can use the integration check the prerequisites. Once thats done, follow up with the installation
In order to be able to use this integration you must sign-up with Mistral AI. This is currently free! Once you got your account you can request an API-Key. If you wish to use agents you can also define those in the developer portal. Each agent has its unique id which you can use in this integration.
Download the files from this repository and place the mistral_ai_api directory into your custom_components directory. Next, add the following entry to your configuration.yaml
mistral_ai_api:
name: "Mistral AI API"
api_key: !secret mistral_token
Once that's done add an entry for mistral_token to your secrets.yaml The token is the token your received from registering with mistral ai.
Now, restart Home Assistant and you should be good to go.
The integration offers three key-parts
You can call the send_prompt-command like this
action: mistral_ai_api.send_prompt
metadata: {}
data:
prompt: Whats the weather
identifier: my-question-123
Or like this
action: mistral_ai_api.send_prompt
metadata: {}
data:
prompt: Give me a jinja2 template to show some data in a nice formatted way. Make sure to only return the code as-is so I can use it directly in a markdown. Don't add any explanation, just return the code.
model: codestral-latest
identifier: my-question-123
conversation_id: my-conversation
This will send the prompt to mistral ai. Once mistral ai sent back the response the mentioned event will be thrown and the sensor-Entity will get its state and attribute updated.
As you can see the mistral_ai_api.send_prompt takes additional (optional) arguments
prompt | The prompt to send to mistral |
conversation_id | optional If you want to have a real conversation with the model (not just one answer to one prompt and then be done) you need to supply a conversation id. This is used as a filename so, please be careful with deciding for a name. |
model | The mistral ai model to be used. Not required when using an agent |
agent_id | optional The id of an agent you want to utilize |
identifier | optional A custom identifier which will be returned in the event so you know which prompt the answer belonged to |
timeout | optional Timeout in seconds which defines how long home assistant should wait for an answer before terminating the request. Value of 60 seconds seems fine. |
In addition to this there is another service called "mistral_ai_api.retrieve_last" This service will return the data for the last communication with mistral ai. The data consists of the following
last_prompt | The prompt that was last sent |
last_response | The last response returned by mistral |
identifier | The last identifier assigned to the communication |
timestamp | The last set timestamp |
This example code shows how to use the service action in a script in home assistant
alias: "[Mistral AI] Retrieve last communication"
sequence:
- action: mistral_ai_api.retrieve_last
metadata: {}
data: {}
response_variable: result
- variables:
identifier: "{{result.identifier}}"
last_prompt: "{{result.last_prompt}}"
last_response: "{{result.last_response}}"
timestamp: "{{result.timestamp}}"
description: "Retrieves the last communication with mistral"
You can react to that in an automation and do whatever you want with the result
alias: "[Mistral AI] React on mistral ai response"
description: ""
triggers:
- trigger: event
event_type: mistral_ai_response
conditions: []
actions:
- action: notify.mobile_app_iphone_5
metadata: {}
data:
message: >-
{{trigger.event.data.identifier}} - {{trigger.event.data.agent_id}} -
{{trigger.event.data.response}}
mode: single
In addition to the event the integration also ships with a Sensor. The id of the sensor is sensor.mistral_ai_api. This sensor can have one of two states
idle | Not doing anything. This is the state it starts with |
processingb> | A prompt was sent to mistral ai. Now waiting for the response |
Given these two states one could create an automation that reacts to the change of the state from idle to processing.
Next up there are a couple of attributes for this entity which are as follows
last_prompt | The last response that was sent to mistral ai |
last_response | The last response from mistral ai |
identifier | The last identifier belonging to the prompt and response |
timestamp | A timestamp that is refreshed whenever a prompt was sent or a response was received |