1 """Conversation support for Anthropic."""
3 from collections.abc
import Callable
5 from typing
import Any, Literal, cast
8 from anthropic._types
import NOT_GIVEN
9 from anthropic.types
import (
19 import voluptuous
as vol
20 from voluptuous_openapi
import convert
32 from .
import AnthropicConfigEntry
40 RECOMMENDED_CHAT_MODEL,
41 RECOMMENDED_MAX_TOKENS,
42 RECOMMENDED_TEMPERATURE,
46 MAX_TOOL_ITERATIONS = 10
51 config_entry: AnthropicConfigEntry,
52 async_add_entities: AddEntitiesCallback,
54 """Set up conversation entities."""
60 tool: llm.Tool, custom_serializer: Callable[[Any], Any] |
None
62 """Format tool specification."""
65 description=tool.description
or "",
66 input_schema=convert(tool.parameters, custom_serializer=custom_serializer),
73 """Convert from class to TypedDict."""
74 param_content: list[TextBlockParam | ToolUseBlockParam] = []
76 for message_content
in message.content:
77 if isinstance(message_content, TextBlock):
78 param_content.append(TextBlockParam(type=
"text", text=message_content.text))
79 elif isinstance(message_content, ToolUseBlock):
83 id=message_content.id,
84 name=message_content.name,
85 input=message_content.input,
89 return MessageParam(role=message.role, content=param_content)
95 """Anthropic conversation agent."""
97 _attr_has_entity_name =
True
100 def __init__(self, entry: AnthropicConfigEntry) ->
None:
101 """Initialize the agent."""
103 self.history: dict[str, list[MessageParam]] = {}
106 identifiers={(DOMAIN, entry.entry_id)},
107 manufacturer=
"Anthropic",
109 entry_type=dr.DeviceEntryType.SERVICE,
111 if self.
entryentry.options.get(CONF_LLM_HASS_API):
113 conversation.ConversationEntityFeature.CONTROL
118 """Return a list of supported languages."""
122 """When entity is added to Home Assistant."""
124 self.
entryentry.async_on_unload(
129 self, user_input: conversation.ConversationInput
131 """Process a sentence."""
132 options = self.
entryentry.options
133 intent_response = intent.IntentResponse(language=user_input.language)
134 llm_api: llm.APIInstance |
None =
None
135 tools: list[ToolParam] |
None =
None
136 user_name: str |
None =
None
137 llm_context = llm.LLMContext(
139 context=user_input.context,
140 user_prompt=user_input.text,
141 language=user_input.language,
142 assistant=conversation.DOMAIN,
143 device_id=user_input.device_id,
146 if options.get(CONF_LLM_HASS_API):
148 llm_api = await llm.async_get_api(
150 options[CONF_LLM_HASS_API],
153 except HomeAssistantError
as err:
154 LOGGER.error(
"Error getting LLM API: %s", err)
155 intent_response.async_set_error(
156 intent.IntentResponseErrorCode.UNKNOWN,
157 f
"Error preparing LLM API: {err}",
160 response=intent_response, conversation_id=user_input.conversation_id
163 _format_tool(tool, llm_api.custom_serializer)
for tool
in llm_api.tools
166 if user_input.conversation_id
is None:
167 conversation_id = ulid.ulid_now()
170 elif user_input.conversation_id
in self.history:
171 conversation_id = user_input.conversation_id
172 messages = self.history[conversation_id]
180 ulid.ulid_to_bytes(user_input.conversation_id)
181 conversation_id = ulid.ulid_now()
183 conversation_id = user_input.conversation_id
189 and user_input.context.user_id
191 user := await self.
hasshass.auth.async_get_user(user_input.context.user_id)
194 user_name = user.name
200 + options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT),
204 "ha_name": self.
hasshass.config.location_name,
205 "user_name": user_name,
206 "llm_context": llm_context,
212 except TemplateError
as err:
213 LOGGER.error(
"Error rendering prompt: %s", err)
214 intent_response.async_set_error(
215 intent.IntentResponseErrorCode.UNKNOWN,
216 f
"Sorry, I had a problem with my template: {err}",
219 response=intent_response, conversation_id=conversation_id
223 prompt_parts.append(llm_api.api_prompt)
225 prompt =
"\n".join(prompt_parts)
228 messages = [*messages, MessageParam(role=
"user", content=user_input.text)]
230 LOGGER.debug(
"Prompt: %s", messages)
231 LOGGER.debug(
"Tools: %s", tools)
232 trace.async_conversation_trace_append(
233 trace.ConversationTraceEventType.AGENT_DETAIL,
234 {
"system": prompt,
"messages": messages},
237 client = self.
entryentry.runtime_data
240 for _iteration
in range(MAX_TOOL_ITERATIONS):
242 response = await client.messages.create(
243 model=options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL),
245 tools=tools
or NOT_GIVEN,
246 max_tokens=options.get(CONF_MAX_TOKENS, RECOMMENDED_MAX_TOKENS),
248 temperature=options.get(CONF_TEMPERATURE, RECOMMENDED_TEMPERATURE),
250 except anthropic.AnthropicError
as err:
251 intent_response.async_set_error(
252 intent.IntentResponseErrorCode.UNKNOWN,
253 f
"Sorry, I had a problem talking to Anthropic: {err}",
256 response=intent_response, conversation_id=conversation_id
259 LOGGER.debug(
"Response %s", response)
263 if response.stop_reason !=
"tool_use" or not llm_api:
266 tool_results: list[ToolResultBlockParam] = []
267 for tool_call
in response.content:
268 if isinstance(tool_call, TextBlock):
269 LOGGER.info(tool_call.text)
271 if not isinstance(tool_call, ToolUseBlock):
274 tool_input = llm.ToolInput(
275 tool_name=tool_call.name,
276 tool_args=cast(dict[str, Any], tool_call.input),
279 "Tool call: %s(%s)", tool_input.tool_name, tool_input.tool_args
283 tool_response = await llm_api.async_call_tool(tool_input)
284 except (HomeAssistantError, vol.Invalid)
as e:
285 tool_response = {
"error": type(e).__name__}
287 tool_response[
"error_text"] =
str(e)
289 LOGGER.debug(
"Tool response: %s", tool_response)
291 ToolResultBlockParam(
293 tool_use_id=tool_call.id,
294 content=json.dumps(tool_response),
298 messages.append(MessageParam(role=
"user", content=tool_results))
300 self.history[conversation_id] = messages
302 for content
in response.content:
303 if isinstance(content, TextBlock):
304 intent_response.async_set_speech(content.text)
308 response=intent_response, conversation_id=conversation_id
312 self, hass: HomeAssistant, entry: ConfigEntry
314 """Handle options update."""
316 await hass.config_entries.async_reload(entry.entry_id)
conversation.ConversationResult async_process(self, conversation.ConversationInput user_input)
list[str]|Literal["*"] supported_languages(self)
None _async_entry_update_listener(self, HomeAssistant hass, ConfigEntry entry)
None async_added_to_hass(self)
None __init__(self, AnthropicConfigEntry entry)
MessageParam _message_convert(Message message)
ToolParam _format_tool(llm.Tool tool, Callable[[Any], Any]|None custom_serializer)
None async_setup_entry(HomeAssistant hass, AnthropicConfigEntry config_entry, AddEntitiesCallback async_add_entities)