Skip to content

Commit

Permalink
Merge pull request #1097 from guardrails-ai/improve-callable-messages
Browse files Browse the repository at this point in the history
add docs to message about prompt failures
  • Loading branch information
dtam authored Sep 30, 2024
2 parents 32a1ce1 + e4a2d24 commit a7b43dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
16 changes: 8 additions & 8 deletions guardrails/classes/llm/prompt_callable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from guardrails.classes.llm.llm_response import LLMResponse


CALLABLE_FAILURE_SUFFIX = """Make sure that `fn` can be called as a function
that accepts a prompt string, **kwargs, and returns a string.
If you're using a custom LLM callable, please see docs
here: https://go.guardrailsai.com/B1igEy3""" # noqa


class PromptCallableException(Exception):
pass

Expand Down Expand Up @@ -29,17 +35,11 @@ def __call__(self, *args, **kwargs) -> LLMResponse:
except Exception as e:
raise PromptCallableException(
"The callable `fn` passed to `Guard(fn, ...)` failed"
f" with the following error: `{e}`. "
"Make sure that `fn` can be called as a function that"
" takes in a single prompt string "
"and returns a string."
f" with the following error: `{e}`. {CALLABLE_FAILURE_SUFFIX}"
)
if not isinstance(result, LLMResponse):
raise PromptCallableException(
"The callable `fn` passed to `Guard(fn, ...)` returned"
f" a non-string value: {result}. "
"Make sure that `fn` can be called as a function that"
" takes in a single prompt string "
"and returns a string."
f" a non-string value: {result}. {CALLABLE_FAILURE_SUFFIX}"
)
return result
11 changes: 3 additions & 8 deletions guardrails/llm_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from guardrails.errors import UserFacingException
from guardrails.classes.llm.llm_response import LLMResponse
from guardrails.classes.llm.prompt_callable import (
CALLABLE_FAILURE_SUFFIX,
PromptCallableBase,
PromptCallableException,
)
Expand Down Expand Up @@ -903,18 +904,12 @@ async def __call__(self, *args, **kwargs) -> LLMResponse:
except Exception as e:
raise PromptCallableException(
"The callable `fn` passed to `Guard(fn, ...)` failed"
f" with the following error: `{e}`. "
"Make sure that `fn` can be called as a function that"
" takes in a single prompt string "
"and returns a string."
f" with the following error: `{e}`. {CALLABLE_FAILURE_SUFFIX}"
)
if not isinstance(result, LLMResponse):
raise PromptCallableException(
"The callable `fn` passed to `Guard(fn, ...)` returned"
f" a non-string value: {result}. "
"Make sure that `fn` can be called as a function that"
" takes in a single prompt string "
"and returns a string."
f" a non-string value: {result}. {CALLABLE_FAILURE_SUFFIX}"
)
return result

Expand Down

0 comments on commit a7b43dd

Please sign in to comment.