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

[FEATURE]: Custom output parsing for AI services (fixes TODO) #1118

Closed
wants to merge 1 commit into from

Conversation

ashimoon
Copy link

This change gives users the ability to provide custom output parsers to AI services.

-- Context --
Previously, users were constrained to the set of data types that were supported by langchain4j (most primitives, string lists, string sets, enums, and JSON POJOs).

-- Change --
Users can now provide a custom ParserProvider when creating an AI service via the builder, or can provide individual OutputParsers on each service method with the @parser annotation.

This enables users to have full control of the output parsing process, while at the same time they continue to benefit from all the existing output parsers that come bundled with langchain4j.

-- Checklist --
Before submitting this PR, please check the following points:

  • I have added unit and integration tests for my change
  • All unit and integration tests in the module I have added/changed are green
  • All unit and integration tests in the core and
    main modules are green
  • I have added/updated the documentation
  • I have added an example in the examples repo (only for "big" features)

Issue

Fixes TODO for supporting custom parsing: https://github.com/langchain4j/langchain4j/blob/main/langchain4j/src/main/java/dev/langchain4j/service/DefaultAiServices.java#L100

General checklist

  • There are no breaking changes
  • I have added unit and integration tests for my change
  • I have manually run all the unit and integration tests in the module I have added/changed, and they are all green
  • I have manually run all the unit and integration tests in the core and main modules, and they are all green

Checklist for adding new model integration

  • I have added my new module in the BOM

Checklist for adding new embedding store integration

  • I have added a {NameOfIntegration}EmbeddingStoreIT that extends from either EmbeddingStoreIT or EmbeddingStoreWithFilteringIT
  • I have added my new module in the BOM

Checklist for changing existing embedding store integration

  • I have manually verified that the {NameOfIntegration}EmbeddingStore works correctly with the data persisted using the latest released version of LangChain4j

This change gives users the ability to provide custom output parsers to
AI services.

-- Context --
Previously, users were constrained to the set of data types that were
supported by langchain4j (most primitives, string lists, string sets,
enums, and JSON POJOs). Generic lists and sets were not supported.

-- Change --
Users can now provide a custom ParserProvider when creating an AI
service via the builder, or can provide individual OutputParsers or
ParserFactory's on each service method with the @parser annotation.

This enables users to have full control of the output parsing process,
while at the same time they continue to benefit from all the existing
output parsers that come bundled with langchain4j.

In addition to replacing the default parser provider, users can
override existing parser instances (e.g. for boolean or int), or add
parsers for their own custom types (e.g. parsing instances of Foo).

Finally, users can add their own ParserFactory instances which will
be called if no output parser instance is found, allowing the user
to construct a parser instance on the fly for the requried type.

This change also adds support for generic List<> and Set<> types, where
the element can be any supported parsable type (e.g. a List<Integer>
works by default). Furthermore, a List<Foo> works by parsing each line
using the registered parser for Foo (either a custom parser, or falling
back to the default JsonParser).

-- Checklist --
Before submitting this PR, please check the following points:
- [X] I have added unit and integration tests for my change
- [X] All unit and integration tests in the module I have added/changed
are green
- [X] All unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules are green
- [x] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
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.

None yet

1 participant