• Mon. Nov 25th, 2024

Spring AI: An AI framework for Java developers

Byadmin

Oct 2, 2024



org.springframework.ai
spring-ai-azure-openai-spring-boot-starter

In essence, when Spring scans the project looking for a ChatClient, it’ll use the property to make one using naming conventions in the openai starter project. In the simple helloworld example we are looking at, that ChatClient is called for by the controller:

package com.xkcd.ai.helloworld;

import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class SimpleAiController {

private final ChatClient chatClient;

@Autowired
public SimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}

@GetMapping(“/ai/simple”)
public MapString, generation(
@RequestParam(value = “message”, defaultValue = “Tell me a joke”) String message) {
return Map.of(“generation”, chatClient.call(message));
}

}

This is a typical Spring REST controller, where the chatClient member is method annotated as @Autowired. That ChatClient is then used to handle the requests at /ai/simple. (Request parameter defaults are provided on the method, so a request with no parameters will be set as “Tell me a joke.”) The endpoint method returns a map with a “generation” key whose value is the return value of chatClient.call(message).

For all this to work, you need an API key for Azure. The key is set as an environment variable:



Source link