stanfordnlp / dspy

DSPy: The framework for programming—not prompting—foundation models
https://dspy-docs.vercel.app/
MIT License
16.74k stars 1.3k forks source link

dspyComponent module only allows for a dspy.Predict module as input #1300

Open tituslhy opened 1 month ago

tituslhy commented 1 month ago

I was following the llamaindex cookbook and reached an error for the code

dspy_component = DSPyComponent(dspy.ChainOfThought(GenerateAnswer))

The error trace showed that there was a pydantic validation error for "predict_module". That's because the DSPyComponent object only accepts a dspy.Predict(signature) input instead of a ChainOfThought object as listed in the cookbook.

I didn't have any errors running this cell before until I upgraded my dspy-ai version. Perhaps loosen the allowable classes for QueryComponent (the parent of DSPyComponent)? Or is the team's recommendation to use dspy.Predict instead?

Validation error trace

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[38], line 1
----> 1 DSPyComponent(dspy.ChainOfThought(GenerateAnswer))

File /opt/anaconda3/envs/llm/lib/python3.12/site-packages/dspy/predict/llamaindex.py:185, in DSPyComponent.__init__(self, predict_module)
    180 def __init__(
    181     self,
    182     predict_module: dspy.Predict,
    183 ) -> None:
    184     """Initialize."""
--> 185     return super().__init__(
    186         predict_module=predict_module,
    187         predict_template=signature_to_template(predict_module.signature),
    188     )

File /opt/anaconda3/envs/llm/lib/python3.12/site-packages/pydantic/v1/main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
    339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    340 if validation_error:
--> 341     raise validation_error
    342 try:
    343     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for DSPyComponent
predict_module
  instance of Predict expected (type=type_error.arbitrary_type; expected_arbitrary_type=Predict)
okhat commented 1 month ago

Ah good catch. It looks like this some recent updates (switching CoT to become a module) broke compatibility with the llamaindex integraton.

Maybe we should pin the notebook to an old DSPy version?

tituslhy commented 1 month ago

Sure! I was on dspy-ai 2.4.11 before this I think - the update just before!