Thursday, November 21, 2024

Spring AI: An AI framework for Java builders


<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>

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.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 remaining ChatClient chatClient;

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

	@GetMapping("/ai/easy")
	public MapString, <String> era(
			@RequestParam(worth = "message", defaultValue = "Inform me a joke") String message) {
		return Map.of("era", chatClient.name(message));
	}

}

This can be a typical Spring REST controller, the place the chatClient member is technique 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 technique returns a map with a “era” key whose worth is the return worth of chatClient.name(message).

For all this to work, you want an API key for Azure. The hot button is set as an atmosphere variable:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles