1 """Config flow for OpenAI Conversation integration."""
3 from __future__
import annotations
6 from types
import MappingProxyType
10 import voluptuous
as vol
39 RECOMMENDED_CHAT_MODEL,
40 RECOMMENDED_MAX_TOKENS,
41 RECOMMENDED_TEMPERATURE,
45 _LOGGER = logging.getLogger(__name__)
47 STEP_USER_DATA_SCHEMA = vol.Schema(
49 vol.Required(CONF_API_KEY): str,
53 RECOMMENDED_OPTIONS = {
54 CONF_RECOMMENDED:
True,
55 CONF_LLM_HASS_API: llm.LLM_API_ASSIST,
56 CONF_PROMPT: llm.DEFAULT_INSTRUCTIONS_PROMPT,
61 """Validate the user input allows us to connect.
63 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
65 client = openai.AsyncOpenAI(api_key=data[CONF_API_KEY])
66 await hass.async_add_executor_job(client.with_options(timeout=10.0).models.list)
70 """Handle a config flow for OpenAI Conversation."""
75 self, user_input: dict[str, Any] |
None =
None
76 ) -> ConfigFlowResult:
77 """Handle the initial step."""
78 if user_input
is None:
80 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA
83 errors: dict[str, str] = {}
87 except openai.APIConnectionError:
88 errors[
"base"] =
"cannot_connect"
89 except openai.AuthenticationError:
90 errors[
"base"] =
"invalid_auth"
92 _LOGGER.exception(
"Unexpected exception")
93 errors[
"base"] =
"unknown"
98 options=RECOMMENDED_OPTIONS,
102 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
107 config_entry: ConfigEntry,
109 """Create the options flow."""
114 """OpenAI config flow options handler."""
116 def __init__(self, config_entry: ConfigEntry) ->
None:
117 """Initialize options flow."""
119 CONF_RECOMMENDED,
False
123 self, user_input: dict[str, Any] |
None =
None
124 ) -> ConfigFlowResult:
125 """Manage the options."""
128 if user_input
is not None:
130 if user_input[CONF_LLM_HASS_API] ==
"none":
131 user_input.pop(CONF_LLM_HASS_API)
138 CONF_RECOMMENDED: user_input[CONF_RECOMMENDED],
139 CONF_PROMPT: user_input[CONF_PROMPT],
140 CONF_LLM_HASS_API: user_input[CONF_LLM_HASS_API],
146 data_schema=vol.Schema(schema),
152 options: dict[str, Any] | MappingProxyType[str, Any],
154 """Return a schema for OpenAI completion options."""
155 hass_apis: list[SelectOptionDict] = [
166 for api
in llm.async_get_apis(hass)
169 schema: VolDictType = {
173 "suggested_value": options.get(
174 CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT
180 description={
"suggested_value": options.get(CONF_LLM_HASS_API)},
184 CONF_RECOMMENDED, default=options.get(CONF_RECOMMENDED,
False)
188 if options.get(CONF_RECOMMENDED):
195 description={
"suggested_value": options.get(CONF_CHAT_MODEL)},
196 default=RECOMMENDED_CHAT_MODEL,
200 description={
"suggested_value": options.get(CONF_MAX_TOKENS)},
201 default=RECOMMENDED_MAX_TOKENS,
205 description={
"suggested_value": options.get(CONF_TOP_P)},
206 default=RECOMMENDED_TOP_P,
210 description={
"suggested_value": options.get(CONF_TEMPERATURE)},
211 default=RECOMMENDED_TEMPERATURE,
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
None __init__(self, ConfigEntry config_entry)
last_rendered_recommended
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
None validate_input(HomeAssistant hass, dict[str, Any] data)
VolDictType openai_config_option_schema(HomeAssistant hass, dict[str, Any]|MappingProxyType[str, Any] options)