Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexcheng1982/llm-openapi-function-spring-ai-generator
Generate Spring AI functions from OpenAPI spec
https://github.com/alexcheng1982/llm-openapi-function-spring-ai-generator
ai llm openapi openapi-generator spring spring-ai
Last synced: about 2 months ago
JSON representation
Generate Spring AI functions from OpenAPI spec
- Host: GitHub
- URL: https://github.com/alexcheng1982/llm-openapi-function-spring-ai-generator
- Owner: alexcheng1982
- License: apache-2.0
- Created: 2024-03-29T04:02:08.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-29T07:06:49.000Z (10 months ago)
- Last Synced: 2024-04-17T02:01:12.264Z (10 months ago)
- Topics: ai, llm, openapi, openapi-generator, spring, spring-ai
- Language: Mustache
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OpenAPI Generator for Spring AI Functions
[![build](https://github.com/alexcheng1982/llm-openapi-function-spring-ai-generator/actions/workflows/build.yaml/badge.svg)](https://github.com/alexcheng1982/llm-openapi-function-spring-ai-generator/actions/workflows/build.yaml)
![Maven Central Version](https://img.shields.io/maven-central/v/io.github.alexcheng1982/openapi-function-spring-ai-generator)Generate Spring
AI [functions](https://docs.spring.io/spring-ai/reference/api/functions.html)
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](example).
## Code Generation
See [pom.xml](example/pom.xml) of the example. Add the custom generator as a
dependency of OpenAPI Generator Maven plugin.```xml
io.github.alexcheng1982
openapi-function-spring-ai-generator
1.0.0```
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.
```java
@Configuration
public class UniversitiesApiSpringAiFunctionConfiguration {public record Request_searchUniversities(String country, Integer limit) {
}
public record Response_searchUniversities(List value) {
}
@Bean
@Description("Search universities")
public Function searchUniversities(
UniversitiesApi apiInvoker) {
return (request) -> {
try {
return new Response_searchUniversities(
apiInvoker.searchUniversities(request.country(), request.limit()));
} catch (ApiException e) {
throw new RuntimeException(e);
}
};
}
}
```## Use Generated Client
To use the generated OpenAPI client in Spring AI:
1. Add the library into your project.
2. Import the `Configuration` or use component scanning.
```java
@Import({
UniversitiesApiSpringAiFunctionConfiguration.class
})
```3. Declare an API bean.
```java
@Bean
public UniversitiesApi universitiesApi() {
return new UniversitiesApi();
}
```4. Use the function in Spring AI.
See [here](https://docs.spring.io/spring-ai/reference/api/clients/functions/openai-chat-functions.html)
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.```java
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
```