Generate Spring AI functions from OpenAPI spec to be used with LLMs. Connect existing APIs with LLMs.
Each OpenAPI operation will be converted into a Function
and register to
Spring context.
native
library is used to minimize external dependencies.
See example.
See pom.xml of the example. Add the custom generator as a dependency of OpenAPI Generator Maven plugin.
<dependency>
<groupId>io.github.alexcheng1982</groupId>
<artifactId>openapi-function-spring-ai-generator</artifactId>
<version>1.0.0</version>
</dependency>
For each API defined in OpenAPI spec, a Spring Configuration
will be
generated. For each operation, the generator will generate:
- A record for request type.
- A record for response type.
- A bean of
Function
to invoke the corresponding operation.
See the sample configuration below.
@Configuration
public class UniversitiesApiSpringAiFunctionConfiguration {
public record Request_searchUniversities(String country, Integer limit) {
}
public record Response_searchUniversities(List<University> value) {
}
@Bean
@Description("Search universities")
public Function<Request_searchUniversities, Response_searchUniversities> searchUniversities(
UniversitiesApi apiInvoker) {
return (request) -> {
try {
return new Response_searchUniversities(
apiInvoker.searchUniversities(request.country(), request.limit()));
} catch (ApiException e) {
throw new RuntimeException(e);
}
};
}
}
To use the generated OpenAPI client in Spring AI:
-
Add the library into your project.
-
Import the
Configuration
or use component scanning.
@Import({
UniversitiesApiSpringAiFunctionConfiguration.class
})
- Declare an API bean.
@Bean
public UniversitiesApi universitiesApi() {
return new UniversitiesApi();
}
- Use the function in Spring AI.
See here for a guide of how to use functions with OpenAI.
searchUniversities
is the name of an operation defined in the OpenAPI spec,
which is also the name of function to call.
OpenAiChatClient chatClient = ...
UserMessage userMessage = new UserMessage(
"Find 10 universities in United Kingdom, output the name and website in CSV format.");
ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
OpenAiChatOptions.builder().withFunction("searchUniversities")
.build())); // (1) Enable the function