11 C
New York
Thursday, November 21, 2024

Spring AI: An AI Framework for Java Builders





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


In essence, when Spring scans the mission for a ChatClient, it should use the property to create one utilizing naming conventions within the openai preliminary mission. within the easy helloworld Within the instance we’re taking a look at, the controller requests ChatClient:


bundle 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 last 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 technique annotated as @Autowired. That ChatClient is then used to deal with the requests in /ai/easy. (The default values ​​of the request parameters are supplied within the technique, so a request with out parameters shall be set to “Inform me a joke.”) The endpoint technique returns a map with a “generate” key whose worth is the return worth of chatClient.name(message).

For all of this to work, you want an API key for Azure. The secret’s set as an surroundings variable:

Related Articles

Latest Articles