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

feat(core): Add Supplier and Consumer function callback support #1746

Conversation

tzolov
Copy link
Contributor

@tzolov tzolov commented Nov 16, 2024

Add support for no-argument Supplier and single-argument Consumer function callbacks in the Spring AI core module. This enhancement allows:

  • Registration of Supplier callbacks with no input (Void) type
  • Registration of Consumer callbacks with no output (Void) type
  • Support for Kotlin Function0 (equivalent to Java Supplier)
  • Handle empty properties for Void input types in schema generation
  • Enhance FunctionCallback builder to support Supplier/Consumer patterns

Additional changes:

  • Add test coverage for both Supplier and Consumer callbacks in various scenarios
  • Enhance TypeResolverHelper to support Consumer input type resolution
  • Support lambda-style function declarations for improved ergonomics
  • Add test cases for void input/output handling in OpenAI chat model
  • Include examples of function calls without return values
  • Add support for parameterless functions through Supplier interface

Resolves #1718 , #1277 , #1118, #860

Copy link
Contributor Author

@tzolov tzolov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may have to add support for the Kotlin's equivalent of java.util.Consumer() in a follow up PR.

return FunctionCallback.builder()
.description(functionDescription)
.schemaType(this.schemaType)
.function(beanName, KotlinDelegate.wrapKotlinSupplier(bean))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we will need support for the Kotlin's equivalent of java.util.Consumer?
But this can be implemented in a follow up PR.

@SuppressWarnings("unchecked")
public static Supplier<?> wrapKotlinSupplier(Object function) {
return () -> ((Function0<Object>) function).invoke();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely we may have to add support for the Kotlin's equivalent of java.util.Consumer() in a follow up PR.

Add support for no-argument Supplier and single-argument Consumer function
callbacks in the Spring AI core module. This enhancement allows:
- Registration of Supplier<O> callbacks with no input (Void) type
- Registration of Consumer<I> callbacks with no output (Void) type
- Support for Kotlin Function0 (equivalent to Java Supplier)
- Handle empty properties for Void input types in schema generation
- Enhance FunctionCallback builder to support Supplier/Consumer patterns

Additional changes:
- Add test coverage for both Supplier and Consumer callbacks in various scenarios
- Enhance TypeResolverHelper to support Consumer input type resolution
- Support lambda-style function declarations for improved ergonomics
- Add test cases for void input/output handling in OpenAI chat model
- Include examples of function calls without return values
- Add support for parameterless functions through Supplier interface

Resolves spring-projects#1718 , spring-projects#1277 , spring-projects#1118, spring-projects#860
Add comprehensive documentation for the FunctionCallback API in Spring AI, including:
- Overview of the interface and its key methods
- Builder pattern usage with function and method invocation approaches
- Examples for different function types (Function, BiFunction, Supplier, Consumer)
- Best practices and common pitfalls
- Schema generation and customization options
@tzolov tzolov force-pushed the support-consumer-supplier-function-calling-v2 branch from e7cc220 to 8a32cff Compare November 17, 2024 16:35
@markpollack
Copy link
Member

Refactored some of the long methods so that it reads more cleanly.
Added 'final' to the kotlin delegate classes.

@markpollack
Copy link
Member

in the docs, the section "Customization Options" was empty, so I removed it.

@markpollack
Copy link
Member

merged in 432954d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for Consumer and Supplier function callbacks
2 participants