Skip to content

BlaXun/home_assistant_mistral_ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

alt text

Table of Contents
  1. About The Project
  2. What is Mistral AI
  3. Example Use-Cases
  4. Getting Started
  5. Usage

About The Project

This is a custom integration for Home Assistant which allows for sending prompts to Mistral AI

(back to top)

What is 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.

(back to top)

Example Use-Cases

  • Let mistral ai decide wether you should open the windows. Send the outside temperature and humidity to mistral ai along with the avarage temperature and humidity of all your rooms.
  • Get dynamic push notifications created by the AI instead of the same static sentences you normall define.
  • Feed mistral ai with your workout data to get a quick summary
  • Use mistral ai coding model to let it generate jinja + markdown to populate your dashboard
  • Generate template sensor code using mistral coding model
  • Getting Started

    Before you can use the integration check the prerequisites. Once thats done, follow up with the installation

    Prerequisites

    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.

    (back to top)

    Installation

    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.

    Usage

    The integration offers three key-parts

  • A service called mistral_ai_api.send_prompt
  • An event with the identifier mistral_ai_response
  • The sensor.mistral_ai_api-Entity
  • Service

    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"

    Event

    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

    Sensor Entity

    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

    idleNot 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_promptThe last response that was sent to mistral ai
    last_responseThe last response from mistral ai
    identifierThe last identifier belonging to the prompt and response
    timestampA timestamp that is refreshed whenever a prompt was sent or a response was received

    (back to top)

    Releases

    No releases published

    Packages

    No packages published

    Languages