(no commit message)

This commit is contained in:
2025-11-04 13:29:58 -05:00
parent 87433012c0
commit 22d47004cd
6 changed files with 143 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ import dspy
from enum import Enum
from typing import Any, Dict, List, Optional, Literal, Union, get_origin
from pydantic import BaseModel, Field, ValidationError
from time import time
class FieldType(str, Enum):
STRING = "str"
@@ -167,16 +168,18 @@ class SignatureGeneration(dspy.Signature):
signature_fields: list[GeneratedField] = dspy.OutputField(
desc="""List of input and output fields for the signature.
CRITICAL RULES:
1. If the prompt describes a structured output with multiple nested fields (e.g., medical records, user profiles, complex forms), you MUST use type='pydantic' with a full PydanticModelSchema
2. NEVER use type='str' with a description like 'JSON string containing...' for complex outputs
3. For Pydantic models: define ALL nested fields properly in the schema with correct types
4. Simple outputs (single values) can use basic types like str, int, bool
5. Use Literal types for enumerated values (e.g., severity levels, status codes)
CRITICAL RULES:
1. If the prompt describes a structured output with multiple nested fields (e.g., medical records, user profiles, complex forms), you MUST use type='pydantic' with a full PydanticModelSchema
2. NEVER use type='str' with a description like 'JSON string containing...' for complex outputs
3. For Pydantic models: define ALL nested fields properly in the schema with correct types
4. Simple outputs (single values) can use basic types like str, int, bool
5. Use Literal types for enumerated values (e.g., severity levels, status codes)
Examples:
- BAD: structured_output: str = "A JSON containing patient data..."
- GOOD: medical_record: MedicalRecord (with full PydanticModelSchema defining all nested fields)"""
Examples:
- BAD: structured_output: str = "A JSON containing patient data..."
- GOOD: medical_record: MedicalRecord (with full PydanticModelSchema defining all nested fields)
"""
)
signature_name: str = dspy.OutputField(
desc="Suggested class name for the signature (PascalCase)"
@@ -186,11 +189,14 @@ Examples:
class SignatureGenerator(dspy.Module):
def __init__(self):
super().__init__()
self.generator = dspy.ChainOfThought(SignatureGeneration)
self.generator = dspy.Predict(SignatureGeneration)
def forward(self, prompt: str):
"""Generate DSPy signature and return raw prediction attributes"""
start_time = time()
result = self.generator(prompt=prompt)
end_time = time()
print(f"Signature generation took {end_time - start_time:.2f} seconds in inference.")
return dspy.Prediction(
signature_name=result.signature_name,