Some more tweaks for LLM API docs (#2205)

This commit is contained in:
Paulus Schoutsen 2024-06-05 00:15:50 -04:00 committed by GitHub
parent 477b03870c
commit 7abb5fb061
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -105,7 +105,7 @@ class MyConversationEntity(conversation.ConversationEntity):
llm_api = await llm.async_get_api( llm_api = await llm.async_get_api(
self.hass, self.hass,
self.entry.options[CONF_LLM_HASS_API], self.entry.options[CONF_LLM_HASS_API],
llm.ToolContext( llm.LLMContext(
platform=DOMAIN, platform=DOMAIN,
context=user_input.context, context=user_input.context,
user_prompt=user_input.text, user_prompt=user_input.text,
@ -164,6 +164,10 @@ class MyConversationEntity(conversation.ConversationEntity):
response = tool_response response = tool_response
``` ```
## Best practices
If your conversation entity allows the user to maintain conversation history using the `conversation_id`, make sure to re-generate the prompt for each interaction and override it in the history that is passed for the follow-up command. This allows the user to always be able to query the latest state of the home.
## Creating your own API ## Creating your own API
To create your own API, you need to create a class that inherits from `API` and implement the `async_get_tools` method. The `async_get_tools` method should return a list of `Tool` objects that represent the functionality that you want to expose to the LLM. To create your own API, you need to create a class that inherits from `API` and implement the `async_get_tools` method. The `async_get_tools` method should return a list of `Tool` objects that represent the functionality that you want to expose to the LLM.
@ -191,7 +195,7 @@ class TimeTool(llm.Tool):
}) })
async def async_call( async def async_call(
self, hass: HomeAssistant, tool_input: ToolInput, tool_context: ToolContext self, hass: HomeAssistant, tool_input: ToolInput, llm_context: LLMContext
) -> JsonObjectType: ) -> JsonObjectType:
"""Call the tool.""" """Call the tool."""
if "timezone" in tool_input.tool_args: if "timezone" in tool_input.tool_args:
@ -255,12 +259,12 @@ class MyAPI(API):
name="My own API", name="My own API",
) )
async def async_get_api_instance(self, tool_context: ToolContext) -> APIInstance: async def async_get_api_instance(self, llm_context: LLMContext) -> APIInstance:
"""Return the instance of the API.""" """Return the instance of the API."""
return APIInstance( return APIInstance(
api=self, api=self,
api_prompt="Call the tools to fetch data from Home Assistant.", api_prompt="Call the tools to fetch data from Home Assistant.",
tool_context=tool_context, llm_context=llm_context,
tools=[TimeTool()], tools=[TimeTool()],
) )
``` ```
@ -278,5 +282,5 @@ The `llm.APIInstance` class has the following attributes:
|-------------------|---------|---------------------------------------------------------------------------------------------------------| |-------------------|---------|---------------------------------------------------------------------------------------------------------|
| `api` | API | The API object. Required. | | `api` | API | The API object. Required. |
| `api_prompt` | string | Instructions for LLM on how to use the LLM tools. Required. | | `api_prompt` | string | Instructions for LLM on how to use the LLM tools. Required. |
| `tool_context` | ToolContext | The context of the tool call. Required. | | `llm_context` | LLMContext | The context of the tool call. Required. |
| `tools` | list[Tool] | The tools that are available in this API. Required. | | `tools` | list[Tool] | The tools that are available in this API. Required. |