stanfordnlp / dspy

DSPy: The framework for programming—not prompting—language models
https://dspy.ai
MIT License
18.83k stars 1.44k forks source link

Signature Optimizer doesn't not consider nested BaseModel signature #1499

Open jimixxperez opened 2 months ago

jimixxperez commented 2 months ago

I currently use a nested BaseModel as output of a signature for a project.

class Bar(BaseModel):
   val: str = Field(desc="value desc")

class Foo(BaseModel):
   bar: Bar = Field(desc="bar desc")

the current implementation seems to look solely at top level field declarations. https://github.com/stanfordnlp/dspy/blob/ef4e5ad109c7fc94c14319f3e687f7e4de7108e3/dspy/teleprompt/signature_opt_typed.py#L28

I m working on a fix but using the current custom signatureInfo design to represent a signature with variable depth feels a bit hacky. I prototyped a way to use the json schema of the model as signature and to let the LM generate a new json schema signature with unmodified fields except description and prefix. This has the additional advantage of including the types as context information further helping the LM to reason about the problem.

disavantages:

What do you guys think of this approach?

arnavsinghvi11 commented 1 month ago

Hi @jimixxperez ,

this approach makes sense but may need some deeper testing. feel free to open a PR with your current version and we can iterate on it!