Skip to content

DEV: Use structured responses for summaries #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

romanrizzi
Copy link
Member

@romanrizzi romanrizzi commented Apr 8, 2025

System personas:

Screenshot 2025-04-14 at 2 38 28 PM

Regular personas:

Screenshot 2025-04-14 at 2 38 46 PM

Schema editor:

Screenshot 2025-04-14 at 2 40 36 PM

@romanrizzi romanrizzi force-pushed the summary_structured_prompt branch 4 times, most recently from 87f39d7 to 78198b6 Compare April 14, 2025 18:42
Copy link
Member

@keegangeorge keegangeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor suggestions, otherwise LGTM

@romanrizzi romanrizzi force-pushed the summary_structured_prompt branch 3 times, most recently from 4bc599a to c364743 Compare April 14, 2025 21:06
@romanrizzi romanrizzi force-pushed the summary_structured_prompt branch from c364743 to 0c23be8 Compare April 14, 2025 21:17
response_format: schema,
) { |partial, cancel| result << partial }

expect(result).to eq("\"key\":\"Hello!\"}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit surprising imo... it should be: {key: "Hello!"} or perhaps a special object that inherits off hash eg: StructuredOutput , then nobody needs to worry about parsing and we can enforce structure at the boundary.

Copy link
Member

@xfalcox xfalcox Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about we use JSON.parse(result, object_class: OpenStruct) so we always have a proper object to work with?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that will work. During streaming, we only get chunks of the JSON so we cannot use JSON#parse.

I believe Sam's proposal is to have a class that handles the buffering and parsing while decoding the response. This class reads the chunk, keeps track of which key/value are we at, and updates the state:

Chunk 1: "{\"message\": \"Line 1\\"
Partial: {"message"=>"Line 1"}

Chunk 2: "nLine 2\", \"status\": \"o"
Partial: {"message"=>"Line 1\nLine 2", "status"=>"o"}

Chunk 3: "k\"}"
Partial: {"message"=>"Line 1\nLine 2", "status"=>"ok"}

By taking care of it internally, the burden of doing this doesn't fall on the modules using structured outputs, like in this case, FoldContent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants