avatarFu Cheng

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3236

Abstract

ass="hljs-name">id</span>></span>generate-client-code<span class="hljs-tag"></<span class="hljs-name">id</span>></span> <span class="hljs-tag"><<span class="hljs-name">goals</span>></span> <span class="hljs-tag"><<span class="hljs-name">goal</span>></span>generate<span class="hljs-tag"></<span class="hljs-name">goal</span>></span> <span class="hljs-tag"></<span class="hljs-name">goals</span>></span> <span class="hljs-tag"><<span class="hljs-name">configuration</span>></span> <span class="hljs-tag"><<span class="hljs-name">generatorName</span>></span>Java<span class="hljs-tag"></<span class="hljs-name">generatorName</span>></span> <span class="hljs-tag"><<span class="hljs-name">library</span>></span>native<span class="hljs-tag"></<span class="hljs-name">library</span>></span> <span class="hljs-tag"><<span class="hljs-name">inputSpec</span>></span>${project.basedir}/universities.yaml<span class="hljs-tag"></<span class="hljs-name">inputSpec</span>></span> <span class="hljs-tag"></<span class="hljs-name">configuration</span>></span> <span class="hljs-tag"></<span class="hljs-name">execution</span>></span> <span class="hljs-tag"></<span class="hljs-name">executions</span>></span> <span class="hljs-tag"><<span class="hljs-name">dependencies</span>></span> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>io.github.alexcheng1982<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>openapi-function-spring-ai-generator<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-name">version</span>></span>1.0.0<span class="hljs-tag"></<span class="hljs-name">version</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependencies</span>></span> <span class="hljs-tag"></<span class="hljs-name">plugin</span>></span></pre></div><p id="ab94">After running the generation, a Spring <code>Configuration</code> will be generated for each API. See the UniversitiesApiSpringAiFunctionConfiguration below.</p><p id="7dda">For each operation, the generator generates:</p><ul><li>A record for request type.</li><li>A record for response type.</li><li>A bean of type Function to invoke the corresponding operation.</li></ul><div id="c4a6"><pre><span class="hljs-meta">@Configuration</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UniversitiesApiSpringAiFunctionConfiguration</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">record</span> <span class="hljs-title class_">Request_searchUniversities</span><span class="hljs-params">(String country, Integer limit)</span> {}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">reco

Options

rd</span> <span class="hljs-title class_">Response_searchUniversities</span><span class="hljs-params">(List<University> value)</span> {}

<span class="hljs-meta">@Bean</span> <span class="hljs-meta">@Description("Search universities")</span> <span class="hljs-keyword">public</span> Function<Request_searchUniversities, Response_searchUniversities> <span class="hljs-title function_">searchUniversities</span><span class="hljs-params">(UniversitiesApi apiInvoker)</span> { <span class="hljs-keyword">return</span> (request) -> { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Response_searchUniversities</span>(apiInvoker.searchUniversities(request.country(), request.limit())); } <span class="hljs-keyword">catch</span> (ApiException e) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e); } }; } }</pre></div><p id="7a00">The generated OpenAPI client should then be published for later usage.</p><p id="c3cf">To use the generated OpenAPI client in Spring AI:</p><p id="498e">1. Add the generated client into your project.</p><p id="eb77">2. Import the <code>Configuration</code>or use component scanning.</p><div id="47b7"><pre><span class="hljs-meta">@Import({ UniversitiesApiSpringAiFunctionConfiguration.class })</span></pre></div><p id="a8e9">3. Declare an API bean.</p><div id="a489"><pre><span class="hljs-meta">@Bean</span> <span class="hljs-keyword">public</span> UniversitiesApi <span class="hljs-title function_">universitiesApi</span><span class="hljs-params">()</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UniversitiesApi</span>(); }</pre></div><p id="32d4">4. Use the function in Spring AI.</p><p id="1fcd">See <a href="https://docs.spring.io/spring-ai/reference/api/clients/functions/openai-chat-functions.html">here</a> for a guide of how to use functions with OpenAI.</p><p id="05e9">searchUniversities is the name of an operation defined in the OpenAPI spec, which is also the name of function to call.</p><div id="8a91"><pre><span class="hljs-type">OpenAiChatClient</span> <span class="hljs-variable">chatClient</span> <span class="hljs-operator">=</span><span class="hljs-type">UserMessage</span> <span class="hljs-variable">userMessage</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UserMessage</span>( <span class="hljs-string">"Find 10 universities in United Kingdom, output the name and website in CSV format."</span>); <span class="hljs-type">ChatResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> chatClient.call(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Prompt</span>(List.of(userMessage), OpenAiChatOptions.builder().withFunction(<span class="hljs-string">"searchUniversities"</span>) .build())); <span class="hljs-comment">// (1) Enable the function</span></pre></div><p id="6b7c">More info on the GitHub.</p></article></body>

Connect Existing OpenAPIs to LLMs with Spring AI

Photo by Steve Johnson on Unsplash

Function calling allows LLMs to access external data to perform certain tasks. Spring AI already supports function calling. Function in Spring AI are simple Java Function s.

There are already many public or private APIs exposed using OpenAPI spec. When developing AI apps, we may want to connect these OpenAPIs to LLMs.

I created a small tool to generate OpenAPI clients with Spring AI configurations, which turns operations in OpenAPI into functions in Spring AI. This tool is based on OpenAPI Generator.

Let’s see an example how to use this tool. I use this universities search service and create an OpenAPI spec for this service.

Run OpenAPI generator with my custom generator. See the pom.xml in the example. Put the custom generator as a dependency of openapi-generator-maven-plugin .

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>${openapi-generator-version}</version>
  <executions>
    <execution>
      <id>generate-client-code</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <generatorName>Java</generatorName>
        <library>native</library>
        <inputSpec>${project.basedir}/universities.yaml</inputSpec>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>io.github.alexcheng1982</groupId>
      <artifactId>openapi-function-spring-ai-generator</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</plugin>

After running the generation, a Spring Configuration will be generated for each API. See the UniversitiesApiSpringAiFunctionConfiguration below.

For each operation, the generator generates:

  • A record for request type.
  • A record for response type.
  • A bean of type Function to invoke the corresponding operation.
@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);
      }
    };
  }
}

The generated OpenAPI client should then be published for later usage.

To use the generated OpenAPI client in Spring AI:

1. Add the generated client into your project.

2. Import the Configurationor use component scanning.

@Import({
 UniversitiesApiSpringAiFunctionConfiguration.class
})

3. Declare an API bean.

@Bean
public UniversitiesApi universitiesApi() {
 return new UniversitiesApi();
}

4. 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

More info on the GitHub.

Spring Ai
Llm
Spring
Open Api
Recommended from ReadMedium