Skip to content
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

Experimental JSON_SCHEMA #7

Closed
wants to merge 4 commits into from
Closed

Experimental JSON_SCHEMA #7

wants to merge 4 commits into from

Conversation

lmangani
Copy link
Collaborator

@lmangani lmangani commented Oct 26, 2024

JSON Structured Output (very experimental)

Define a json_schema to receive a structured response in JSON format... most of the time.

{
    summary: 'VARCHAR',
    favourite_animals:='VARCHAR[]',
    favourite_activity:='VARCHAR[]',
    star_rating:='INTEGER'},
    struct_descr:={star_rating: 'rating on a scale from 1 (bad) to 5 (very good)'
}

Prompt based. Output depends on model skills.

D SET VARIABLE openprompt_model_name = 'qwen2.5:1.5b';
D SET VARIABLE openprompt_json_schema = "struct:={summary: 'VARCHAR', favourite_animals:='VARCHAR[]', favourite_activity:='VARCHAR[]', star_rating:='INTEGER'}, struct_descr:={star_rating: 'visit rating on a scale from 1 (bad) to 5 (very good)'}";
D SELECT open_prompt('My zoo visit was fun and I loved the bears and tigers. i also had icecream') AS response;
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                        response                                                                         │
│                                                                         varchar                                                                         │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"summary": "A short summary of your recent zoo visit activity.", "favourite_animals": ["bears", "tigers"], "favourite_activity": ["icecream"], "sta …  │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

The results can be validated and parsed with the DuckDB JSON extension and its functions

D LOAD json;                                      ^
D WITH response AS (SELECT open_prompt('My zoo visit was fun and I loved the bears and tigers. i also had icecream') AS response) SELECT json_structure(response) FROM response;
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                          json_structure(response)                                          │
│                                                    json                                                    │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"summary":"VARCHAR","favourite_animals":"VARCHAR","favourite_activity":"VARCHAR","star_rating":"UBIGINT"} │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

@lmangani lmangani closed this Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant