Cognify supports unmodified DSPy programs. All you need to do is to register the entry function for Cognify to execute the workflow during optimization. The following is the DSPy code for the Math Problem Solver example:
In DSPy, the dspy.Predict class is the primary abstraction for obtaining a response from a language model. When intializing the optimizer, Cognify will automatically translate each predictor in your globally instantiated dspy.Module (as defined by the module's __init__ function) into a cognify.StructuredModel.
Tip
DSPy also contains other, more detailed modules that don't follow the behavior of dspy.Predict (e.g., dspy.ChainOfThought). In Cognify, we view Chain-of-Thought prompting (and other similar techniques) as possible optimizations to apply to an LLM call on the fly instead of as pre-defined templates.
By default, Cognify will translate all predictors into valid optimization targets. For more fine-grained control over which predictors should be targeted for optimization, you can manually wrap your predictor with our cognify.PredictModel class like so:
import cognify
import dspy
class MathSolverWorkflow(dspy.Module):
def __init__(self):
super().__init__()
self.interpreter_agent = dspy.Predict("problem -> math_model")
# -- manually wrap the predictor with PredictModel --
self.solver_agent = cognify.PredictModel(
"solver_agent",
dspy.Predict("problem, math_model -> answer")
)
def forward(self, problem):
math_model = self.interpreter_agent(problem=problem).math_model
answer = self.solver_agent(problem=problem, math_model=math_model).answer # unchanged
return answer
...If you prefer to define your modules using our cognify.Model interface but still want to utilize them with your existing DSPy infrastructure, you can wrap your cognify.Model with an as_predict() call. This will convert your cognify.Model into a cognify.PredictModel and follows the dspy.Predict protocol.
import cognify
import dspy
cognify_solver_agent = cognify.Model("solver_agent", ...)
class MathSolverWorkflow(dspy.Module):
def __init__(self):
super().__init__()
self.interpreter_agent = dspy.Predict("problem -> math_model")
self.solver_agent = cognify.as_predict(solver_agent) # wrap cognify model here
def forward(self, problem):
math_model = self.interpreter_agent(problem=problem).math_model
answer = self.solver_agent(problem=problem, math_model=math_model).answer # unchanged
return answer
...Finally to register the workflow to Cognify, you can annotate the entry point as follows:
import cognify
...
math_agent = MathSolverWorkflow()
@cognify.register_workflow
def math_solver_workflow(workflow_input):
answer = my_workflow(problem=workflow_input)
return {"workflow_output": answer}