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

refactor: introduce FunctionCallbackResolver interface #1804

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tzolov
Copy link
Contributor

@tzolov tzolov commented Nov 23, 2024

  • Introduces a new FunctionCallbackResolver interface to define the strategy for resolving FunctionCallback instances from the application context.
  • Renames FunctionCallbackContext to DefaultFunctionCallbackResolver to better reflect its implementation role. Updates all related components to use the new interface.
  • Update the affected AI model implementations
    • Replaces FunctionCallbackContext parameter with FunctionCallbackResolver in all model constructors
    • Updates builder patterns to use functionCallbackResolver() method instead of withFunctionCallbackContext()
    • Deprecates old withFunctionCallbackContext() methods in builders to guide migration
  • Updates integration tests to use DefaultFunctionCallbackResolver
  • Improves documentation to clarify the resolver's role in function callbacks
  • Moves SchemaType enum from FunctionCallbackContext to FunctionCallback (Braking change)git add .

Resolves #758

@tzolov tzolov added this to the 1.0.0-M5 milestone Nov 23, 2024
@tzolov tzolov mentioned this pull request Nov 23, 2024
10 tasks
* @param functionCallbackContext the function callback context used to store the
* state of the function calls.
* @param functionCallbackResolver the function callback resolver used to resolve the
* function by bean name.s
Copy link
Member

Choose a reason for hiding this comment

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

typo

@@ -128,28 +128,29 @@ public MoonshotChatModel(MoonshotApi moonshotApi, MoonshotChatOptions options) {
* @param moonshotApi The Moonshot instance to be used for interacting with the
* Moonshot Chat API.
* @param options The MoonshotChatOptions to configure the chat client.
* @param functionCallbackContext The function callback context.
* @param functionCallbackResolver The function callback resolver to
Copy link
Member

Choose a reason for hiding this comment

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

javadoc sentence incomplete.

@@ -155,27 +155,27 @@ public OpenAiChatModel(OpenAiApi openAiApi, OpenAiChatOptions options) {
* @param openAiApi The OpenAiApi instance to be used for interacting with the OpenAI
* Chat API.
* @param options The OpenAiChatOptions to configure the chat model.
* @param functionCallbackContext The function callback context.
* @param functionCallbackResolver The function callback context.
Copy link
Member

Choose a reason for hiding this comment

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

javadoc still refers to callback context.

@@ -43,7 +43,7 @@ public class SpringAiCoreRuntimeHints implements RuntimeHintsRegistrar {
public void registerHints(@NonNull RuntimeHints hints, @Nullable ClassLoader classLoader) {

var chatTypes = Set.of(AbstractMessage.class, AssistantMessage.class, ToolResponseMessage.class, Message.class,
MessageType.class, UserMessage.class, SystemMessage.class, FunctionCallbackContext.class,
MessageType.class, UserMessage.class, SystemMessage.class, DefaultFunctionCallbackResolver.class,
Copy link
Member

Choose a reason for hiding this comment

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

How does this work when we have other implementations of "FunctionCallbackResolver"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there are other implementation they have to be added here.

null);
if (functionCallback != null) {
this.functionCallbackRegister.put(functionName, functionCallback);
}
else {
throw new IllegalStateException(
"No function callback [" + functionName + "] fund in tht FunctionCallbackContext");
"No function callback [" + functionName + "] fund in tht FunctionCallbackRegister");
Copy link
Member

Choose a reason for hiding this comment

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

typo in the exception message.

import org.springframework.lang.Nullable;

/**
* Strategy interface for resolving {@link FunctionCallback} instances as bean from the
Copy link
Member

Choose a reason for hiding this comment

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

I believe there could be an implementation for the FunctionCallbackResolver that doesn't depend on the application context, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can not think of any not app context case?

- Introduces a new FunctionCallbackResolver interface to define the strategy for
  resolving FunctionCallback instances from the application context.
- Renames FunctionCallbackContext to DefaultFunctionCallbackResolver to better reflect its
  implementation role. Updates all related components to use the new interface.
- Update the affected AI model implementations
  - Replaces FunctionCallbackContext parameter with FunctionCallbackResolver in all
    model constructors
  - Updates builder patterns to use functionCallbackResolver() method instead of
    withFunctionCallbackContext()
  - Deprecates old withFunctionCallbackContext() methods in builders to guide
    migration
- Updates integration tests to use DefaultFunctionCallbackResolver
- Improves documentation to clarify the resolver's role in function callbacks
- Moves SchemaType enum from FunctionCallbackContext to FunctionCallback (Braking change)git add .

Resolves spring-projects#758
@tzolov tzolov force-pushed the issue-758-function-callback-resolver branch from 33c25ee to 30af292 Compare November 25, 2024 15:33
* @param beanName the name of the bean to resolve
* @return the {@link FunctionCallback} instance
*/
FunctionCallback getFunctionCallback(@NonNull String beanName, @Nullable String defaultDescription);
Copy link
Member

Choose a reason for hiding this comment

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

what about

FunctionCallback resolve(@NonNull String name)

The description should be provided by the implementation.

* @param functionCallbackContext the function callback context used to store the
* state of the function calls.
* @param functionCallbackResolver the function callback resolver used to resolve the
* function by bean name
Copy link
Member

Choose a reason for hiding this comment

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

Javadoc doesn't need to refer "bean" and just "by name" is enough. will fix when merging.

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.

Refactor FunctionCallbackContext to FuctionCallBackResolver interface
3 participants