<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
In essence, when Spring scans the venture searching for a ChatClient, it’ll use the property to make one utilizing naming conventions within the openai
starter venture. Within the easy helloworld
instance we’re , that ChatClient is known as for by the controller:
package deal com.xkcd.ai.helloworld;
import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.net.bind.annotation.GetMapping;
import org.springframework.net.bind.annotation.RequestParam;
import org.springframework.net.bind.annotation.RestController;
import java.util.Map;
@RestController
public class SimpleAiController {
personal last ChatClient chatClient;
@Autowired
public SimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/easy")
public MapString, <String> technology(
@RequestParam(worth = "message", defaultValue = "Inform me a joke") String message) {
return Map.of("technology", chatClient.name(message));
}
}
This can be a typical Spring REST controller, the place the chatClient
member is methodology annotated as @Autowired
. That ChatClient is then used to deal with the requests at /ai/easy
. (Request parameter defaults are offered on the strategy, so a request with no parameters might be set as “Inform me a joke.”) The endpoint methodology returns a map with a “technology” key whose worth is the return worth of chatClient.name(message)
.
For all this to work, you want an API key for Azure. The secret’s set as an surroundings variable: