Skip to content

Commit f897a72

Browse files
authored
Fix Google AI not using correct config options after subentries migration (home-assistant#147493)
1 parent 0bbb168 commit f897a72

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

homeassistant/components/google_generative_ai_conversation/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from homeassistant.helpers.typing import ConfigType
3636

3737
from .const import (
38-
CONF_CHAT_MODEL,
3938
CONF_PROMPT,
4039
DOMAIN,
4140
FILE_POLLING_INTERVAL_SECONDS,
@@ -190,7 +189,7 @@ def _init_client() -> Client:
190189

191190
client = await hass.async_add_executor_job(_init_client)
192191
await client.aio.models.get(
193-
model=entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL),
192+
model=RECOMMENDED_CHAT_MODEL,
194193
config={"http_options": {"timeout": TIMEOUT_MILLIS}},
195194
)
196195
except (APIError, Timeout) as err:

homeassistant/components/google_generative_ai_conversation/entity.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ async def _async_handle_chat_log(
337337
tools = tools or []
338338
tools.append(Tool(google_search=GoogleSearch()))
339339

340-
model_name = self.entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL)
340+
model_name = options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL)
341341
# Avoid INVALID_ARGUMENT Developer instruction is not enabled for <model>
342342
supports_system_instruction = (
343343
"gemma" not in model_name
@@ -389,47 +389,13 @@ async def _async_handle_chat_log(
389389

390390
if tool_results:
391391
messages.append(_create_google_tool_response_content(tool_results))
392-
generateContentConfig = GenerateContentConfig(
393-
temperature=self.entry.options.get(
394-
CONF_TEMPERATURE, RECOMMENDED_TEMPERATURE
395-
),
396-
top_k=self.entry.options.get(CONF_TOP_K, RECOMMENDED_TOP_K),
397-
top_p=self.entry.options.get(CONF_TOP_P, RECOMMENDED_TOP_P),
398-
max_output_tokens=self.entry.options.get(
399-
CONF_MAX_TOKENS, RECOMMENDED_MAX_TOKENS
400-
),
401-
safety_settings=[
402-
SafetySetting(
403-
category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
404-
threshold=self.entry.options.get(
405-
CONF_HATE_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
406-
),
407-
),
408-
SafetySetting(
409-
category=HarmCategory.HARM_CATEGORY_HARASSMENT,
410-
threshold=self.entry.options.get(
411-
CONF_HARASSMENT_BLOCK_THRESHOLD,
412-
RECOMMENDED_HARM_BLOCK_THRESHOLD,
413-
),
414-
),
415-
SafetySetting(
416-
category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
417-
threshold=self.entry.options.get(
418-
CONF_DANGEROUS_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
419-
),
420-
),
421-
SafetySetting(
422-
category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
423-
threshold=self.entry.options.get(
424-
CONF_SEXUAL_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
425-
),
426-
),
427-
],
428-
tools=tools or None,
429-
system_instruction=prompt if supports_system_instruction else None,
430-
automatic_function_calling=AutomaticFunctionCallingConfig(
431-
disable=True, maximum_remote_calls=None
432-
),
392+
generateContentConfig = self.create_generate_content_config()
393+
generateContentConfig.tools = tools or None
394+
generateContentConfig.system_instruction = (
395+
prompt if supports_system_instruction else None
396+
)
397+
generateContentConfig.automatic_function_calling = (
398+
AutomaticFunctionCallingConfig(disable=True, maximum_remote_calls=None)
433399
)
434400

435401
if not supports_system_instruction:
@@ -472,3 +438,40 @@ async def _async_handle_chat_log(
472438

473439
if not chat_log.unresponded_tool_results:
474440
break
441+
442+
def create_generate_content_config(self) -> GenerateContentConfig:
443+
"""Create the GenerateContentConfig for the LLM."""
444+
options = self.subentry.data
445+
return GenerateContentConfig(
446+
temperature=options.get(CONF_TEMPERATURE, RECOMMENDED_TEMPERATURE),
447+
top_k=options.get(CONF_TOP_K, RECOMMENDED_TOP_K),
448+
top_p=options.get(CONF_TOP_P, RECOMMENDED_TOP_P),
449+
max_output_tokens=options.get(CONF_MAX_TOKENS, RECOMMENDED_MAX_TOKENS),
450+
safety_settings=[
451+
SafetySetting(
452+
category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
453+
threshold=options.get(
454+
CONF_HATE_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
455+
),
456+
),
457+
SafetySetting(
458+
category=HarmCategory.HARM_CATEGORY_HARASSMENT,
459+
threshold=options.get(
460+
CONF_HARASSMENT_BLOCK_THRESHOLD,
461+
RECOMMENDED_HARM_BLOCK_THRESHOLD,
462+
),
463+
),
464+
SafetySetting(
465+
category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
466+
threshold=options.get(
467+
CONF_DANGEROUS_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
468+
),
469+
),
470+
SafetySetting(
471+
category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
472+
threshold=options.get(
473+
CONF_SEXUAL_BLOCK_THRESHOLD, RECOMMENDED_HARM_BLOCK_THRESHOLD
474+
),
475+
),
476+
],
477+
)

0 commit comments

Comments
 (0)