stanfordnlp / dspy

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

`LangchainPredict` class not compatible with `COPRO` optimizer #1387

Closed aishwaryap closed 2 months ago

aishwaryap commented 2 months ago

Although this is included in this more general issue outlining issues with integration with Langchain, it felt beneficial to create a new issue for this particular error. With the latest dspy-ai==2.4.13, the Langchain integration example runs but if the optimizer in the notebook is changed to COPRO, it fails with the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[12], line 4
      2 teleprompter = COPRO(prompt_model=llm, metric=metric)
      3 kwargs = dict(num_threads=8, display_progress=True, display_table=0)
----> 4 compiled_prompt_opt = teleprompter.compile(zeroshot_chain, trainset=trainset, eval_kwargs=kwargs)
      5 eval_score = evaluate(compiled_prompt_opt, devset=testset, **kwargs)

File [~/Documents/venvs/dspy_v3_env/lib/python3.10/site-packages/dspy/teleprompt/copro_optimizer.py:160](http://localhost:8888/lab/tree/~/Documents/venvs/dspy_v3_env/lib/python3.10/site-packages/dspy/teleprompt/copro_optimizer.py#line=159), in COPRO.compile(self, student, trainset, eval_kwargs)
    158 basic_instruction = None
    159 basic_prefix = None
--> 160 *_, last_key = self._get_signature(predictor).fields.keys()
    161 basic_instruction = self._get_signature(predictor).instructions
    162 basic_prefix = self._get_signature(predictor).fields[last_key].json_schema_extra["prefix"]

AttributeError: 'list' object has no attribute 'keys'
arnavsinghvi11 commented 2 months ago

Hi @aishwaryap , most of the community-integrated modules like Langchain, Llamaindex, etc. were not built in concert with some of the new optimizers and will need some implementation details refined/updated. Particularly, this sets up the signature using the Langchain template, while DSPy module signatures use DSPy templates, which is why the error is triggered. Will keep this issue open for further investigation and/or if members using Langchain have suggestions for better integration of the Langchain modules to ensure this compatibility.

Additionally, I recommend using MIPROv2 over COPRO going forward!

okhat commented 2 months ago

Consider Zenbase @CyrusOfEden for LangChain<>DSPy integrations, @aishwaryap !