Skip to content

Commit

Permalink
feat(agents-api): Add some custom error messages to base_evaluate (#758)
Browse files Browse the repository at this point in the history
I added some error messages in base_evaluate to help users identify some
specific errors.
<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Adds custom error messages to `base_evaluate` in `base_evaluate.py`
for Jinja template misuse and variable name misspellings, using
`thefuzz` for suggestions.
> 
>   - **Error Handling**:
> - Introduces `EvaluateError` class in `base_evaluate.py` to provide
custom error messages.
> - Detects Jinja template misuse and suggests using Python expressions.
> - Uses `thefuzz` to suggest correct variable names when
`NameNotDefined` error occurs.
>   - **Dependencies**:
>     - Adds `thefuzz` to `pyproject.toml` for fuzzy string matching.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup>
for 5a25b84. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
Ahmad-mtos and creatorrr authored Oct 28, 2024
1 parent abb2087 commit 7892cd7
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 9 deletions.
47 changes: 39 additions & 8 deletions agents-api/agents_api/activities/task_steps/base_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,33 @@
from beartype import beartype
from box import Box
from openai import BaseModel
from simpleeval import NameNotDefined
from temporalio import activity
from thefuzz import fuzz

from ...common.storage_handler import auto_blob_store
from ...env import testing
from ..utils import get_evaluator


class EvaluateError(Exception):
def __init__(self, error, expression, values):
error_message = error.message if hasattr(error, "message") else str(error)
message = error_message

# Catch a possible jinja template error
if "unhashable" in error_message and "{{" in expression:
message += "\nSuggestion: It seems like you used a jinja template, did you mean to use a python expression?"

# Catch a possible misspell in a variable name
if isinstance(error, NameNotDefined):
misspelledName = error_message.split("'")[1]
for variableName in values.keys():
if fuzz.ratio(variableName, misspelledName) >= 90.0:
message += f"\nDid you mean '{variableName}' instead of '{misspelledName}'?"
super().__init__(message)


@auto_blob_store
@beartype
async def base_evaluate(
Expand Down Expand Up @@ -47,22 +67,32 @@ async def base_evaluate(

evaluator = get_evaluator(names=values, extra_functions=extra_lambdas)

chosen_expression = ""

try:
result = None
match exprs:
case str():
chosen_expression = exprs
result = evaluator.eval(exprs)
case list():
result = [evaluator.eval(expr) for expr in exprs]
result = []
for expr in exprs:
chosen_expression = expr
result.append(evaluator.eval(expr))
case dict() as d if all(
isinstance(v, dict) or isinstance(v, str) for v in d.values()
):
result = {
k: {ik: evaluator.eval(iv) for ik, iv in v.items()}
if isinstance(v, dict)
else evaluator.eval(v)
for k, v in d.items()
}
result = {}
for k, v in d.items():
if isinstance(v, str):
chosen_expression = v
result[k] = evaluator.eval(v)
else:
result[k] = {}
for k1, v1 in v.items():
chosen_expression = v1
result[k][k1] = evaluator.eval(v1)
case _:
raise ValueError(f"Invalid expression: {exprs}")

Expand All @@ -71,7 +101,8 @@ async def base_evaluate(
except BaseException as e:
if activity.in_activity():
activity.logger.error(f"Error in base_evaluate: {e}")
raise
newException = EvaluateError(e, chosen_expression, values)
raise newException from e


# Note: This is here just for clarity. We could have just imported base_evaluate directly
Expand Down
116 changes: 115 additions & 1 deletion agents-api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agents-api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ xxhash = "^3.5.0"
spacy = "^3.8.2"
en-core-web-sm = {url = "https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl"}
msgpack = "^1.1.0"
thefuzz = "^0.22.1"
gunicorn = "^23.0.0"
uvloop = "^0.21.0"
[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 7892cd7

Please sign in to comment.