Skip to content

Commit

Permalink
Include docstring of field definitions in LM prompt if applicable.
Browse files Browse the repository at this point in the history
Usage:
```python
class Step(pg.Object):
  description: str
  output: pg.typing.Annotated[
    float,
    'Output for current step based on the steps of previous steps.'
  ]

class Solution(pg.Object):
  steps: list[Step]
  final_answer: int

lf.query(question, Solution)
```

PiperOrigin-RevId: 569547439
  • Loading branch information
daiyip authored and langfun authors committed Sep 29, 2023
1 parent 3f56a1d commit 52296ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion langfun/core/structured/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ def class_definition(cls, strict: bool = False) -> str:
'Variable-length keyword arguments is not supported in '
f'structured parsing or query. Encountered: {field}'
)
out.write(f' {field.key}: {annotation(field.value, strict=strict)}\n')
out.write(f' {field.key}: {annotation(field.value, strict=strict)}')
if field.description:
description = field.description.replace('\n', ' ')
out.write(f' # {description}')
out.write('\n')
else:
out.write(' pass\n')
return out.getvalue()
Expand Down
7 changes: 5 additions & 2 deletions langfun/core/structured/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class Itinerary(pg.Object):
day: pg.typing.Int[1, None]
type: pg.typing.Enum['daytime', 'nighttime']
activities: list[Activity]
hotel: pg.typing.Str['.*Hotel'] | None
hotel: pg.typing.Annotated[
pg.typing.Str['.*Hotel'] | None,
'Hotel to stay if applicable.'
]


class Node(pg.Object):
Expand Down Expand Up @@ -387,7 +390,7 @@ class Itinerary:
day: int(min=1)
type: Literal['daytime', 'nighttime']
activities: list[Activity]
hotel: str(regex='.*Hotel') | None
hotel: str(regex='.*Hotel') | None # Hotel to stay if applicable.
""") + '\n',
)

Expand Down

0 comments on commit 52296ea

Please sign in to comment.