spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/index.html
Apache License 2.0
2.99k stars 743 forks source link

Feature Request: Support for Invoking HTTP-based APIs via OpenAPI Spec #806

Open LiveNathan opened 4 months ago

LiveNathan commented 4 months ago

I would like to propose an enhancement to Spring AI that could significantly broaden its application: the ability to generate and invoke HTTP-based API calls dynamically based on OpenAPI specifications.

Current Functionality:

Spring AI currently allows the registration of custom Java functions with the OpenAiChatModel, which the model can then call by generating a JSON object with the required arguments. This setup works excellently for predefined functions where the function signature is known beforehand.

Proposed Enhancement:

Extend the functionality to allow the AI to interpret OpenAPI specifications and generate HTTP requests dynamically. This feature would enable developers to automate interactions with various APIs without needing to manually define each possible function or endpoint.

Benefits:

Enhanced Flexibility: Automating the generation of API calls based on OpenAPI specs would make Spring AI more adaptable, allowing it to interact with a broader range of external APIs.

Reduced Development Time: Developers wouldn't need to manually define and maintain function signatures for each API endpoint they wish to call. Instead, they could simply provide the OpenAPI spec and let the AI handle the rest.

Increased Productivity: This feature could automate complex workflows involving multiple API calls, making it easier to build sophisticated applications quickly.

Implementation Suggestion:

Function Bean: Introduce a new type of function bean that can interpret OpenAPI specs and generate the corresponding HTTP requests. This bean would parse the OpenAPI document, extract endpoint details, and formulate the necessary request parameters dynamically.

AI Integration: Enhance the OpenAiChatModel to understand when to call these new function beans based on the provided API documentation and user requests.

Example Use Case:

Imagine a staffing application that interacts with an API to manage contacts. Currently, a developer might need to:

GET a contact's UUID. Use that UUID in a subsequent POST request to update their email address. With this proposed enhancement, the developer could provide the OpenAPI spec for the staffing API, and the AI could dynamically generate and execute these calls based on the user's request.

Conclusion:

This feature would greatly enhance the capabilities of Spring AI, bringing it closer to the functionality offered by frameworks like LangChain, but with the added robustness and flexibility of the Spring ecosystem. It would empower developers to automate complex workflows and integrate with a vast array of external APIs more efficiently.

LiveNathan commented 4 months ago

For more context, I think what I'm asking for is a way to build an agent, eg. Build an Agent

markpollack commented 2 months ago

I've labelled the issue with 'agent' and 'function calling' so that we can review in these contexts. Agents, if we do anything at all, would be post 1.0 GA. Improvements to function calling are being working on for 1.0.

coderphonui commented 2 weeks ago

I would suggest a concept of "AI function call support API spec" to make the scope simpler and easier to implement instead of supporting the full OpenAPI spec.

"AI function call support API" are endpoints that satisfied conditions

In the real world there's a lot of complex scenarios and we do not want to adapt to specific case. That would make the core logic of spring-ai really complex to adopt to these cases. The suitable approach is building "connector" API - which is the API following the above conditions to wrap the real / complex API execution. For any endpoint which is not satisfied, a wrapper endpoint is a MUST.

With that thought, I come with a super simple stupid approach to implement this as below:

1/ Create the APIFunctionCallback implements the FunctionCallback interface with below simple code:

@Getter
@Builder
@Slf4j
public class ApiFunctionCallback implements FunctionCallback {
    private String name;

    private String description;

    private String inputTypeSchema;

    private String apiEndpoint;

    private String apiKey;

    private ApiClient apiClient;

    @Override
    public String call(String functionInput) {
        return apiClient.execute(apiEndpoint, functionInput, apiKey);
    }
}

2/ The inputTypeSchema: we accept the JSON schema string - which can be generated easily from the input of the OpenAPI spec or setup in the configuration.

In my case, I have a need to setup a JSON configuration where I can load all of these API specs and can use them as function call for my AI Agent. I come with this approach to adopt it first while waiting the official solution from spring-ai implementation.

Let me know if you have other ideas / comments.

Thanks