Skip to content

Commit

Permalink
[python] improve error handling in complete()
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Sep 16, 2024
1 parent b2bd708 commit 577b307
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.7.7

- Throw errors in `complete()` when tool fails to allow proper handling

## 2.7.6

- `extract()` can now return original content with `language = "*"`
Expand Down
18 changes: 16 additions & 2 deletions python/hal9/complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ def complete_openai(completion, messages = [], tools = [], show = True):

if tool_args:
if tool_name in tools:
error = False
try:
result = str(tools[tool_name](**tool_args))
except Exception as e:
error = True
result = str(e)
print(result)

messages.append({ "role": "function", "name": tool_name, "content": result})

if error:
raise Exception(result)

return content + result

Expand Down Expand Up @@ -121,7 +126,13 @@ def complete_llama(completion, messages = [], tools = [], show = True):
function_name = tool_call.function.name
function_to_call = tools[function_name]
function_args = json.loads(tool_call.function.arguments)
response = str(function_to_call(**function_args))

error = False
try:
response = str(function_to_call(**function_args))
except Exception as e:
error = True
response = str(e)

messages.append(
{
Expand All @@ -132,6 +143,9 @@ def complete_llama(completion, messages = [], tools = [], show = True):
}
)

if error:
raise Exception(response)

if show:
print(response)
else:
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.7.6"
version = "2.7.7"
description = ""
authors = ["Javier Luraschi <javier@hal9.ai>"]
readme = "README.md"
Expand Down

0 comments on commit 577b307

Please sign in to comment.