Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Add assert decor to main, add dspyAssertException

Shangyint741238ea3de1
diff --git a/dspy/assertions/__init__.py b/dspy/assertions/__init__.py
index ca1facabb..6da1aa37e 100644
--- a/dspy/assertions/__init__.py
+++ b/dspy/assertions/__init__.py
@@ -1 +1 @@
-from .assertions import *
\ No newline at end of file
+from .assertions import *
diff --git a/dspy/assertions/assertions.py b/dspy/assertions/assertions.py
index 749479dbd..ef2f04143 100644
--- a/dspy/assertions/assertions.py
+++ b/dspy/assertions/assertions.py
@@ -4,8 +4,10 @@
from typing import Any
import dspy
+
# compile time assertion
class Assert:
+
def __init__(self, assert_fun, *args, **kwargs):
self.assert_fun = assert_fun
self.args = args
@@ -18,18 +20,20 @@ class Assert:
if result:
return True
else:
- raise AssertionError(f"Assertion {self.assert_fun} failed")
+ raise DSPyAssertionError(f"Assertion {self.assert_fun} failed")
else:
raise ValueError("Assertion function should always return [bool]")
-
+
+
# we could possibly to have runtime assertions as well
# class RuntimeAssert(Assert): ...
class Assertions(dspy.Module):
+
def __init__(self) -> None:
self.assertions = []
-
+
def add(self, assertion: Assert):
self.assertions.append(assertion)
@@ -40,6 +44,14 @@ class Assertions(dspy.Module):
else:
return assertion()
+
+class DSPyAssertionError(AssertionError):
+
+ def __init__(self, msg: str, state: Any = None) -> None:
+ super().__init__(msg)
+ self.state = state
+
+
def assert_transform(backtrack=2):
def wrapper(func):
def inner(*args, **kwargs):
@@ -49,6 +61,7 @@ def assert_transform(backtrack=2):
except AssertionError as e:
# Add metadata to state
pass
- return inner
+
+ return inner
+
return wrapper
-
diff --git a/dspy/primitives/program.py b/dspy/primitives/program.py
index 73dbee643..d9278be44 100644
--- a/dspy/primitives/program.py
+++ b/dspy/primitives/program.py
@@ -1,44 +1,51 @@
import copy
from dspy.primitives.module import BaseModule
+from assertions import assert_transform
class ProgramMeta(type):
pass
# def __call__(cls, *args, **kwargs):
# obj = super(ProgramMeta, cls).__call__(*args, **kwargs)
-
+
# if issubclass(cls, Program) and not getattr(obj, "_program_init_called", False):
# obj._base_init()
# obj._program_init_called = True
# return obj
+
class Module(BaseModule, metaclass=ProgramMeta):
+
def _base_init(self):
self._compiled = False
def __init__(self):
self._compiled = False
+ if getattr(self, "forward", False) and not getattr(
+ self.forward, "_decorated", False
+ ):
+ return setattr(self, "forward", assert_transform()(self.forward))
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
-
+
def named_predictors(self):
from dspy.predict.predict import Predict
-
+
named_parameters = self.named_parameters()
return [(name, param) for name, param in named_parameters if isinstance(param, Predict)]
def predictors(self):
return [param for _, param in self.named_predictors()]
-
+
def __repr__(self):
s = []
for name, param in self.named_predictors():
s.append(f"{name} = {param}")
-
+
return '\n'.join(s)
# def __deepcopy__(self, memo):
@@ -46,7 +53,7 @@ class Module(BaseModule, metaclass=ProgramMeta):
# # Check if the object is already copied
# if id(self) in memo:
# return memo[id(self)]
-
+
# print(f"Deep copying {self.__class__.__name__}...")
# new_copy = copy.copy(self)
@@ -60,4 +67,4 @@ class Module(BaseModule, metaclass=ProgramMeta):
# return new_copy
-Program = Module
\ No newline at end of file
+Program = Module
diff --git a/tot_assertion.ipynb b/tot_assertion.ipynb
index 6785cd1df..157b4f9f8 100644
--- a/tot_assertion.ipynb
+++ b/tot_assertion.ipynb
@@ -497,7 +497,10 @@
"\n",
" pred = self.generate_answer(context=context, question=question)\n",
" return dspy.Prediction(context=context, answer=pred.answer)\n",
- " \n",
+ "\n",
+ "def query_predicate(query):\n",
+ " pass # a function returns True or False, may calls some LM to get the answer\n",
+ "\n",
"# How to backtrack?\n",
"class SimplifiedBaleenWithAssertion(dspy.Module):\n",
" def __init__(self, passages_per_hop=3, max_hops=2):\n",
@@ -509,17 +512,12 @@
" self.generate_answer = dspy.ChainOfThought(GenerateAnswer)\n",
" self.max_hops = max_hops\n",
" \n",
- " @assert_transform(backtrack=2)\n",
" def forward(self, question):\n",
" context = []\n",
" \n",
- " query_so_far = [] \n",
" for hop in range(self.max_hops):\n",
" query = self.generate_query[hop](context=context, question=question).query\n",
- " # During compile time, this assertion might have some return value\n",
- " # If it's false, what shall we do? We do not have the python control flow.\n",
- " # can we do tracing to get control flow?\n",
- " self.assertion(Assert(lambda q: q not in query_so_far, query))\n",
+ " self.assertion(Assert(query_predicate, query))\n",
" passages = self.retrieve(query).passages\n",
" context = deduplicate(context + passages)\n",
"\n",