DSPy: The framework for programming—not prompting—language models
Python
made assertion syntax simpler
manishshettymfe442d6b2857
diff --git a/assert_hotpotqa.py b/assert_hotpotqa.pyindex d767e56f4..927bdc169 100644--- a/assert_hotpotqa.py+++ b/assert_hotpotqa.py@@ -101,26 +101,25 @@ class SimplifiedBaleen(dspy.Module):def forward(self, question):context = []- previous_queries = [question]+ prev_queries = [question]+for hop in range(self.max_hops):query = self.generate_query[hop](context=context, question=question).querydspy.Assert(- lambda x: len(x) <= 100,- query,+ len(query) <= 100,msg="Query should be short and less than 100 characters",)dspy.Assert(- validate_query_distinction_local,- previous_queries,- query,+ validate_query_distinction_local(prev_queries, query),msg="Query should not be the following: "+ "; ".join(- f"{idx+1}) {query}" for idx, query in enumerate(previous_queries)+ f"{idx+1}) {query}" for idx, query in enumerate(prev_queries)),)- previous_queries.append(query)++ prev_queries.append(query)passages = self.retrieve(query).passagescontext = deduplicate(context + passages)diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.pyindex 957066921..eea480120 100644--- a/dspy/primitives/assertions.py+++ b/dspy/primitives/assertions.py@@ -16,9 +16,8 @@ class DSPyAssertionError(AssertionError):class Assert:"""DSPy Assertion"""- def __init__(self, assert_fun: Callable, *args, **kwargs):- self.assert_fun = assert_fun- self.args = args+ def __init__(self, assert_fun_res: bool, **kwargs):+ self.assert_fun_res = assert_fun_resif "msg" in kwargs:self.msg = kwargs["msg"]@@ -30,9 +29,8 @@ class Assert:self.__call__()def __call__(self) -> bool:- result = self.assert_fun(*self.args, **self.kwargs)- if isinstance(result, bool):- if result:+ if isinstance(self.assert_fun_res, bool):+ if self.assert_fun_res:return Trueelse:raise DSPyAssertionError(msg=self.msg, state=dsp.settings.trace)