Skip to content

Commit

Permalink
[#35] add logging, fix exception messages when failed to load functio…
Browse files Browse the repository at this point in the history
…ns or parse arguments
  • Loading branch information
jekalmin authored and jekalmin committed Dec 6, 2023
1 parent 5fa3b2f commit ef4d140
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
22 changes: 10 additions & 12 deletions custom_components/extended_openai_conversation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
CallServiceError,
FunctionNotFound,
NativeNotFound,
FunctionLoadFailed,
ParseArgumentsFailed,
)

from .helpers import (
Expand Down Expand Up @@ -171,14 +173,8 @@ async def async_process(
return conversation.ConversationResult(
response=intent_response, conversation_id=conversation_id
)
except (
EntityNotFound,
ServiceNotFound,
CallServiceError,
EntityNotExposed,
FunctionNotFound,
NativeNotFound,
) as err:
except HomeAssistantError as err:
_LOGGER.error(err, exc_info=err)
intent_response = intent.IntentResponse(language=user_input.language)
intent_response.async_set_error(
intent.IntentResponseErrorCode.UNKNOWN,
Expand Down Expand Up @@ -242,9 +238,8 @@ def get_functions(self):
for function in setting["function"].values():
convert_to_template(function, hass=self.hass)
return result
except Exception as e:
_LOGGER.error("Failed to load functions", e)
return []
except:
raise FunctionLoadFailed()

async def query(
self,
Expand Down Expand Up @@ -323,7 +318,10 @@ async def execute_function(
function,
):
function_executor = FUNCTION_EXECUTORS[function["function"]["type"]]
arguments = json.loads(message["function_call"]["arguments"])
try:
arguments = json.loads(message["function_call"]["arguments"])
except json.decoder.JSONDecodeError:
raise ParseArgumentsFailed(message["function_call"]["arguments"])

result = await function_executor.execute(
self.hass, function["function"], arguments, user_input, exposed_entities
Expand Down
31 changes: 31 additions & 0 deletions custom_components/extended_openai_conversation/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,34 @@ def __init__(self, name: str) -> None:
def __str__(self) -> str:
"""Return string representation."""
return f"native function '{self.name}' does not exist"


class FunctionLoadFailed(HomeAssistantError):
"""When function load failed."""

def __init__(self) -> None:
"""Initialize error."""
super().__init__(
self,
"failed to load functions. Verify functions are valid in a yaml format",
)

def __str__(self) -> str:
"""Return string representation."""
return "failed to load functions. Verify functions are valid in a yaml format"


class ParseArgumentsFailed(HomeAssistantError):
"""When parse arguments failed."""

def __init__(self, arguments: str) -> None:
"""Initialize error."""
super().__init__(
self,
f"failed to parse arguments `{arguments}`. Increase maximum token to avoid the issue.",
)
self.arguments = arguments

def __str__(self) -> str:
"""Return string representation."""
return f"failed to parse arguments `{self.arguments}`. Increase maximum token to avoid the issue."
3 changes: 2 additions & 1 deletion custom_components/extended_openai_conversation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,9 @@ async def execute(

q = Template(query, hass).async_render(template_arguments)
_LOGGER.info("Rendered query: %s", q)

with sqlite3.connect(db_url, uri=True) as conn:
cursor = conn.execute(q)
cursor = conn.cursor().execute(q)
names = [description[0] for description in cursor.description]

if function.get("single") is True:
Expand Down

0 comments on commit ef4d140

Please sign in to comment.