├── INFO.md
├── README.md
├── custom_components
└── openai_response
│ ├── __init__.py
│ ├── manifest.json
│ └── sensor.py
└── misc
├── Capture1.jpg
├── Card.PNG
├── ChatGPT.png
└── ChatGPT_image.PNG
/INFO.md:
--------------------------------------------------------------------------------
1 |
5 |
6 | # Home Assistant OpenAI Response Sensor
7 |
8 | [](https://github.com/custom-components/hacs)
9 |
10 | This custom component for Home Assistant allows you to generate text responses using OpenAI's GPT-3 model.
11 |
12 | Head to **[This Link](https://platform.openai.com/account/api-keys)** to get you API key from OpenAI.
13 |
14 |
16 |
17 |
18 |
19 | ## Installation
20 | **1.** Copy the **openai_response** folder to your Home Assistant's custom_components directory. If you don't have a **custom_components** directory, create one in the same directory as your **configuration.yaml** file.
21 |
22 | **2.** Add the following lines to your Home Assistant **configuration.yaml** file:
23 |
24 | ```yaml
25 | sensor:
26 | - platform: openai_response
27 | api_key: YOUR_OPENAI_API_KEY
28 | model: "text-davinci-003" # Optional, defaults to "text-davinci-003"
29 | name: "hassio_openai_response" # Optional, defaults to "hassio_openai_response"
30 | ```
31 | Replace **YOUR_OPENAI_API_KEY** with your actual OpenAI API key.
32 |
33 | **3.** Restart Home Assistant.
34 |
35 | ## Usage
36 | Create an **input_text.gpt_input** entity in Home Assistant to serve as the input for the GPT-3 model. Add the following lines to your configuration.yaml file:
37 |
38 | ```yaml
39 | input_text:
40 | gpt_input:
41 | name: GPT-3 Input
42 | ```
43 | Note you can also create this input_text via the device helpers page!
44 |
45 | If you are creating via YAML, you will need to restart again to activate the new entity,
46 |
47 | To generate a response from GPT-3, update the **input_text.gpt_input** entity with the text you want to send to the model. The generated response will be available as an attribute of the **sensor.hassio_openai_response** entity.
48 |
49 | ## Example
50 | To display the GPT-3 input and response in your Home Assistant frontend, add the following to your **ui-lovelace.yaml** file or create a card in the Lovelace UI:
51 |
52 | ```yaml
53 | type: grid
54 | square: false
55 | columns: 1
56 | cards:
57 | - type: entities
58 | entities:
59 | - entity: input_text.gpt_input
60 | - type: markdown
61 | content: '{{ state_attr(''sensor.hassio_openai_response'', ''response_text'') }}'
62 | title: ChatGPT Response
63 | ```
64 | Now you can type your text in the GPT-3 Input field, and the generated response will be displayed in the response card.
65 |
66 |
68 |
69 | ## License
70 | This project is licensed under the MIT License - see the **[LICENSE](https://chat.openai.com/LICENSE)** file for details.
71 |
72 | **Disclaimer:** This project is not affiliated with or endorsed by OpenAI. Use the GPT-3 API at your own risk, and be aware of the API usage costs associated with the OpenAI API.
73 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
5 |
6 | # Home Assistant OpenAI Response Sensor
7 |
8 | [](https://github.com/custom-components/hacs)
9 |
10 | This custom component for Home Assistant allows you to generate text responses using OpenAI's GPT-3 model.
11 |
12 | Head to **[This Link](https://platform.openai.com/account/api-keys)** to get you API key from OpenAI.
13 |
14 |
16 |
17 |
18 |
19 | ## Installation
20 | **1.**
21 | **(Manual)** Copy the **openai_response** folder to your Home Assistant's custom_components directory. If you don't have a **custom_components** directory, create one in the same directory as your **configuration.yaml** file.
22 |
23 | **(HACS)** Add this repository to HACS. https://github.com/Hassassistant/openai_response
24 |
25 | **2.** Add the following lines to your Home Assistant **configuration.yaml** file:
26 |
27 | ```yaml
28 | sensor:
29 | - platform: openai_response
30 | api_key: YOUR_OPENAI_API_KEY
31 | model: "text-davinci-003" # Optional, defaults to "text-davinci-003"
32 | name: "hassio_openai_response" # Optional, defaults to "hassio_openai_response"
33 | ```
34 | Replace **YOUR_OPENAI_API_KEY** with your actual OpenAI API key.
35 |
36 | **3.** Restart Home Assistant.
37 |
38 | ## Usage
39 | Create an **input_text.gpt_input** entity in Home Assistant to serve as the input for the GPT-3 model. Add the following lines to your configuration.yaml file:
40 |
41 | ```yaml
42 | input_text:
43 | gpt_input:
44 | name: GPT-3 Input
45 | ```
46 | Note you can also create this input_text via the device helpers page!
47 |
48 | If you are creating via YAML, you will need to restart again to activate the new entity,
49 |
50 | To generate a response from GPT-3, update the **input_text.gpt_input** entity with the text you want to send to the model. The generated response will be available as an attribute of the **sensor.hassio_openai_response** entity.
51 |
52 | ## Example
53 | To display the GPT-3 input and response in your Home Assistant frontend, add the following to your **ui-lovelace.yaml** file or create a card in the Lovelace UI:
54 |
55 | ```yaml
56 | type: grid
57 | square: false
58 | columns: 1
59 | cards:
60 | - type: entities
61 | entities:
62 | - entity: input_text.gpt_input
63 | - type: markdown
64 | content: '{{ state_attr(''sensor.hassio_openai_response'', ''response_text'') }}'
65 | title: ChatGPT Response
66 | ```
67 | Now you can type your text in the GPT-3 Input field, and the generated response will be displayed in the response card.
68 |
69 |
71 |
72 | ## License
73 | This project is licensed under the MIT License - see the **[LICENSE](https://chat.openai.com/LICENSE)** file for details.
74 |
75 | **Disclaimer:** This project is not affiliated with or endorsed by OpenAI. Use the GPT-3 API at your own risk, and be aware of the API usage costs associated with the OpenAI API.
76 |
--------------------------------------------------------------------------------
/custom_components/openai_response/__init__.py:
--------------------------------------------------------------------------------
1 | """OpenAI Response custom component."""
2 |
--------------------------------------------------------------------------------
/custom_components/openai_response/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "domain": "openai_response",
3 | "name": "OpenAI Response",
4 | "documentation": "https://github.com/Hassassistant/openai_response",
5 | "dependencies": [],
6 | "codeowners": ["@hassassistant"],
7 | "requirements": ["openai==0.27.0"],
8 | "extra_quality_scale": "platinum",
9 | "version": "0.1.0"
10 | }
11 |
12 |
--------------------------------------------------------------------------------
/custom_components/openai_response/sensor.py:
--------------------------------------------------------------------------------
1 | import openai
2 | import voluptuous as vol
3 | from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
4 | from homeassistant.const import CONF_API_KEY, CONF_NAME
5 | import homeassistant.helpers.config_validation as cv
6 | from homeassistant.core import callback
7 |
8 |
9 | CONF_MODEL = "model"
10 | DEFAULT_NAME = "hassio_openai_response"
11 | DEFAULT_MODEL = "text-davinci-003"
12 |
13 | PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
14 | {
15 | vol.Required(CONF_API_KEY): cv.string,
16 | vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
17 | vol.Optional(CONF_MODEL, default=DEFAULT_MODEL): cv.string,
18 | }
19 | )
20 |
21 | async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
22 | api_key = config[CONF_API_KEY]
23 | name = config[CONF_NAME]
24 | model = config[CONF_MODEL]
25 |
26 | openai.api_key = api_key
27 |
28 | async_add_entities([OpenAIResponseSensor(hass, name, model)], True)
29 |
30 |
31 | def generate_openai_response_sync(model, prompt, temperature, max_tokens, top_p, frequency_penalty, presence_penalty):
32 | return openai.Completion.create(
33 | model=model,
34 | prompt=prompt,
35 | temperature=temperature,
36 | max_tokens=max_tokens,
37 | top_p=top_p,
38 | frequency_penalty=frequency_penalty,
39 | presence_penalty=presence_penalty
40 | )
41 |
42 |
43 | class OpenAIResponseSensor(SensorEntity):
44 | def __init__(self, hass, name, model):
45 | self._hass = hass
46 | self._name = name
47 | self._model = model
48 | self._state = None
49 | self._response_text = ""
50 |
51 | @property
52 | def name(self):
53 | return self._name
54 |
55 | @property
56 | def state(self):
57 | return self._state
58 |
59 | @property
60 | def extra_state_attributes(self):
61 | return {"response_text": self._response_text}
62 |
63 | async def async_generate_openai_response(self, entity_id, old_state, new_state):
64 | new_text = new_state.state
65 | if new_text:
66 | response = await self._hass.async_add_executor_job(
67 | generate_openai_response_sync,
68 | self._model,
69 | new_text,
70 | 0.9,
71 | 964,
72 | 1,
73 | 0,
74 | 0
75 | )
76 | self._response_text = response["choices"][0]["text"]
77 | self._state = "response_received"
78 | self.async_write_ha_state()
79 |
80 | async def async_added_to_hass(self):
81 | self.async_on_remove(
82 | self._hass.helpers.event.async_track_state_change(
83 | "input_text.gpt_input", self.async_generate_openai_response
84 | )
85 | )
86 |
87 | async def async_update(self):
88 | pass
89 |
--------------------------------------------------------------------------------
/misc/Capture1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hassassistant/openai_response/d8c21f68d22021d26a5fdbfc8d9fa26ad171fc2c/misc/Capture1.jpg
--------------------------------------------------------------------------------
/misc/Card.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hassassistant/openai_response/d8c21f68d22021d26a5fdbfc8d9fa26ad171fc2c/misc/Card.PNG
--------------------------------------------------------------------------------
/misc/ChatGPT.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hassassistant/openai_response/d8c21f68d22021d26a5fdbfc8d9fa26ad171fc2c/misc/ChatGPT.png
--------------------------------------------------------------------------------
/misc/ChatGPT_image.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hassassistant/openai_response/d8c21f68d22021d26a5fdbfc8d9fa26ad171fc2c/misc/ChatGPT_image.PNG
--------------------------------------------------------------------------------