Skip to content

Home Assistant Service to interact with OpenAI compatible endpoints.

License

Notifications You must be signed in to change notification settings

tobus3000/openai_service

Repository files navigation

Home Assistant OpenAI Service

hacs_badge

This Service for Home Assistant allows you to generate text responses using OpenAI's GPT-3 model or a locally managed LLM.

Head to This Link to get your API key from OpenAI.

Note: If you don't like to pay for the cloud OpenAI service, you can run your own LLM.

Installation

These instructions assume that you already have a working installation of the HACS (Home Assistant Community Store) in your home assistant setup.

Step 1

Add this repository to HACS. https://github.com/tobus3000/openai_service

Step 2

Search for "OpenAI Service" in the HACS - Home Assistant Community Store.
Click "Download".

Step 3

Restart Home Assistant.

Step 4 (optional)

Go to "Settings" -> "Devices & services" -> click the "OpenAI Service" integration.
Click the "Configure" button to fine-tune some aspects of your service.

Usage

Use the "Developer tools" -> "Services" in Home Assistant to use the service.
The Service Response contains the raw response but also a list of individual sentences.
Each entry under sentences contains the text, a language guess and a confidence score from 0-1.

Once you are familiar with the response format you can take it a notch further and build your own automations.
You can for example:

  1. Define a input_text.gpt_input Helper
  2. Create an automation that triggers when the input field state changes.
  3. Automation action could be a simple:
    service: openai_service.send_request
    metadata: {}
    data:
      message: "{{ states('input_text.gpt_input') }}"
    response_variable: gpt_response
  4. followed by the TTS service call for the response (i.e. something like this..).
    service: tts.speak
    metadata: {}
    data:
      cache: false
      media_player_entity_id: media_player.mpd
      message: "{{ gpt_response.response }}"
    target:
      entity_id: tts.piper

You can also iterate over each text in the sentences list and call the correct TTS engine based on the language attribute.
This might become handy when you use any other language than english because the LLM responses do have a tendency to fall back to english at times...
Selecting the TTS engine based on the language of each sentence could therefor make sense... But you are free to do whatever floats your boat...

Example Service payload (German)

Entwicklerwerkzeuge -> Dienste -> OpenAI Service: Sende eine OpenAI Abfrage

Service

service: openai_service.send_request
data:
  message: Wann wird es dunkel?
  mood: Du antwortest immer auf Deutsch.
  temperature: 0.7
  max_tokens: 500

Antwort

response: >
  Wann es dunkel wird, hängt vom Tagesstandort und der Jahreszeit ab. In
  Deutschland beginnt die Nacht allgemein um 20 Uhr (8 pm) im Sommer und um 16
  Uhr (4 pm) im Winter. Die genaue Zeit variiert je nach Breitegrad und Saison.
  Um die genauen Daten für dein spezielles Datum und Ort zu erhalten, kann ich
  dich auf die Webseite der German Meteorological Service (DWD) verweisen. Sie
  bietet eine interessante Funktion zur Berechnung der Sonnenaufgänge und
  -untergänge an:
  [Sonne](https://www.dwd.de/DE/meteo/sonne/sol-startende_und_endezeit.html).
  Hier kannst du deinen Ort eingeben und das Datum auswählen, um die genauen
  Zeitpunkte für Sonnenaufgang und -untergang zu erfahren.


  In english: When will it be dark?
sentences:
  - text: Wann es dunkel wird, hängt vom Tagesstandort und der Jahreszeit ab.
    language: de
    confidence: 1
  - text: >-
      In Deutschland beginnt die Nacht allgemein um 20 Uhr (8 pm) im Sommer und
      um 16 Uhr (4 pm) im Winter.
    language: de
    confidence: 1
  - text: Die genaue Zeit variiert je nach Breitegrad und Saison.
    language: de
    confidence: 1
  - text: >-
      Um die genauen Daten für dein spezielles Datum und Ort zu erhalten, kann
      ich dich auf die Webseite der German Meteorological Service (DWD)
      verweisen.
    language: de
    confidence: 1
  - text: >-
      Sie bietet eine interessante Funktion zur Berechnung der Sonnenaufgänge
      und -untergänge an:
      [Sonne](https://www.dwd.de/DE/meteo/sonne/sol-startende_und_endezeit.html).
    language: de
    confidence: 1
  - text: >-
      Hier kannst du deinen Ort eingeben und das Datum auswählen, um die genauen
      Zeitpunkte für Sonnenaufgang und -untergang zu erfahren.
    language: de
    confidence: 1
  - text: "In english: When will it be dark?"
    language: en
    confidence: 0.9999994849812891

Example Service payload (English)

Developer tools -> Services -> OpenAI Service: Send OpenAI request

Service

service: openai_service.send_request
data:
  message: How far is the moon from earth?
  mood: You are a helpful assistant. Your answers are short and precise.
  temperature: 0.2
  max_tokens: 500

Response

response: >-
  The average distance from the Moon to Earth is about 238,900 miles (384,400
  kilometers). This measurement varies because the Moon's orbit around Earth is
  not a perfect circle.
sentences:
  - text: >-
      The average distance from the Moon to Earth is about 238,900 miles
      (384,400 kilometers).
    language: en
    confidence: 1
  - text: >-
      This measurement varies because the Moon's orbit around Earth is not a
      perfect circle.
    language: en
    confidence: 1