tmc / langchaingo

LangChain for Go, the easiest way to write LLM-based programs in Go
https://tmc.github.io/langchaingo/
MIT License
4.38k stars 597 forks source link

outputparser: add `Defined` parser to extract a struct from LLM output #856

Closed erictse closed 3 months ago

erictse commented 4 months ago

This introduces an output parser named 'Defined' as an improvement to the existing output parser 'Structured'.

A developer can define a struct with the proper tags which can generate a description of the desired output format for an LLM. The parser then returns a struct of the given type, populated with the data it extracted. The developer experience parallels what's described in this Build an Extraction Chain tutorial.

For best results, use this with the tool calling feature of your LLM.

Fixes https://github.com/tmc/langchaingo/issues/171 and https://github.com/tmc/langchaingo/issues/197

PR Checklist

erictse commented 4 months ago

@tmc I've addressed the linter issues, but the tagalign issues remain for my test file. This feature may mean looking into disabling the tagalign linter may be more worthwhile. It wants to force reordering the json: struct tag key so it appears after describe:, reducing readability.

erictse commented 4 months ago

@FluffyKebab Hi, do you have any feedback on this PR for me? Haven't heard from anyone yet.

erictse commented 4 months ago

@eliben Sorry if I'm barking up the wrong tree. I'm not sure who should be reviewing/accepting PRs. Either way, do you have any feedback on this?

eliben commented 4 months ago

@erictse this isn't an area of langchaingo I was working on, so I think you'll have to wait for @tmc to review.

erictse commented 3 months ago

@tmc I've addressed your comments, including allowing untagged struct fields.

tmc commented 3 months ago

I like this broadly, but can’t help but think this represents a limited subset of what we might encode via https://github.com/invopop/jsonschema — which could be reused to handle tool reprs.

thoughts?

erictse commented 3 months ago

Using struct tags to generate JSON Schema looks promising! It should probably be on the roadmap.

However, here are reasons for not dropping this pull request:

tmc commented 3 months ago

That's all fair, let's do it!

Can you follow on with an example and some docs?