stanfordnlp / dspy

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

Field descriptions for individual items in a Pydantic model #1314

Open manankalra opened 1 month ago

manankalra commented 1 month ago
from pydantic import BaseModel, Field
from dspy.functional import TypedPredictor

class TravelInformation(BaseModel):
    origin: str = Field(pattern=r"^[A-Z]{3}$")
    destination: str = Field(pattern=r"^[A-Z]{3}$")
    date: datetime.date
    confidence: float = Field(gt=0, lt=1)

class TravelSignature(Signature):
    """ Extract all travel information in the given email """
    email: str = InputField()
    flight_information: list[TravelInformation] = OutputField()

predictor = TypedPredictor(TravelSignature)
predictor(email='...')

The README contains this example for using Pydantic types within a Signature. Is there a way to provide descriptions for individual properties in the Pydantic model that will be used by the prompt?

d3banjan commented 1 month ago

https://github.com/stanfordnlp/dspy/blob/main/dspy%2Fsignatures%2Ffield.py#L22

The description kwarg for pydantic should suffice. You can subclass Field to include the title, description and examples into description.