Class WithExampleConverter

  • All Implemented Interfaces:
    org.springframework.ai.converter.FormatProvider , org.springframework.ai.converter.StructuredOutputConverter , org.springframework.core.convert.converter.Converter

    
    public final class WithExampleConverter<T extends Object>
     implements StructuredOutputConverter<T>
                        

    Decorator for Spring's StructuredOutputConverter that adds few-shot examples to the output format description.

    <p> Few-shot examples are often used in prompt engineering to help AI models understand the expected output format by providing concrete example outputs. This converter generates such examples automatically using dummy data and injects them into the format description returned by [getFormat()].
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      T convert(String source) Delegates conversion to the underlying delegate.
      String getFormat() Returns a format description string, augmented with few-shot examples.
      • Methods inherited from class org.springframework.core.convert.converter.Converter

        andThen
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WithExampleConverter

        WithExampleConverter(StructuredOutputConverter<T> delegate, Class<T> outputClass, Boolean ifPossible, Boolean generateExamples)
        Parameters:
        delegate - the underlying output converter to which conversion is delegated
        outputClass - the class type for which dummy example instances will be generated
        ifPossible - determines whether to include both success and failure examples (true) or just a simple example (false)
        generateExamples - whether to generate examples or not.
    • Method Detail

      • convert

         T convert(String source)

        Delegates conversion to the underlying delegate.

        Parameters:
        source - the raw output string to convert
        Returns:

        the converted output, or null if conversion fails

      • getFormat

         String getFormat()

        Returns a format description string, augmented with few-shot examples.

        The example(s) are generated using DummyInstanceCreator, which creates a mock instance of outputClass.

        If ifPossible is true, the example is wrapped in a structure (presumably MaybeReturn) that shows both 'success' and 'failure' cases. This is helpful when the output type can be a success or an error.

        If ifPossible is false, only a single example output is shown (not wrapped).

        The underlying converter's format is always appended after the examples.

        Returns:

        a string describing the output format, including examples