spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Using @Tool annotation for Function beans?

Open markpollack opened this issue 7 months ago • 1 comments
trafficstars

Discussed in https://github.com/spring-projects/spring-ai/discussions/2581

Originally posted by henningSaulCM March 27, 2025 We have a bunch of tools implemented as Functions and instantiated as Spring Beans (with a custom annotation), e.g.:

  @AcmeTool
  public BiFunction<SomeAcmeTool.Request, ToolContext, AcmeToolResponse<Record>> someAcmeTool(SomeService someService) {
    return new SomeAcmeTool(someService);
 }

To register the tools, we've implemented a custom ToolCallbackProvider that looks for this custom annotation:

public FunctionCallback[] getToolCallbacks() {
    List<FunctionCallback> tools = new ArrayList<>();
    // create ToolCallback for each AcmeTool
    beanFactory.getBeansWithAnnotation(AcmeTool.class).forEach((beanName, acmeTool) -> {
      try {
        ToolCallback toolCallback = springBeanToolCallbackResolver.resolve(beanName);
        tools.add(toolCallback);
      } catch (Exception e) {
        LOG.error("Failed to create ToolCallback for AcmeTool bean '{}'", beanName, e);
        throw e;
      }

    });
    return tools.toArray(new FunctionCallback[0]);
  }

We're wondering if there's a better way...

Can the Spring AI tool annotation somehow be used instead of our custom annotation for our Function beans?

markpollack avatar Apr 03 '25 17:04 markpollack