org.springframework.ai
spring-ai-azure-openai-spring-boot-starter
In essence, when Spring scans the venture in search of 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 taking a look at, 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 facility.annotation.Autowired;
import org.springframework.internet.bind.annotation.GetMapping;
import org.springframework.internet.bind.annotation.RequestParam;
import org.springframework.internet.bind.annotation.RestController;
import java.util.Map;
@RestController
public class SimpleAiController {
non-public closing ChatClient chatClient;
@Autowired
public SimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/easy")
public MapString, 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 tactic, so a request with no parameters can 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: