-8.1 C
New York
Monday, December 23, 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 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 known as for by the controller:


bundle 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 {

	non-public 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 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:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles