llms: allow specifying JSON Schema
This commit adds llms.WithJSONSchema(schema *jsonschema.Definition) so as to enforce structured outputs:
https://ollama.com/blog/structured-outputs
This fixes an issue that an LLM produces JSON with an unexpected schema.
Supported LLMs: ollama, openai
PR Checklist
- [X] Read the Contributing documentation.
- [X] Read the Code of conduct documentation.
- [X] Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as
memory: add interfaces for X, Yorutil: add whizzbang helpers). - [X] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
- [X] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g.
Fixes #123). - [X] Describes the source of new concepts.
- (Not applicable) References existing implementations as appropriate.
- [X] Contains test coverage for new functions.
- [X] Passes all
golangci-lintchecks.
Any plan to merge this PR? I would be interested too.
Rebased to solve the merge conflict
@tmc Could you take a look?
@tmc May I ask if you have an interest in this, or is this not planned to be merged?
Quite interested and I want to see it in. I've been developing a supplemental project that has kept me away from maintenance tasks here but I will look this evening.
I have https://github.com/tmc/langchaingo/tree/prep-v0.1.15 pushed which has structured output changes that improve provider support but I like what I see here, need to get to bed but will consider refactoring this PR to fit on top of those commits (or you can do so yourself). Thanks for your contirbution!