15.9 C
London
Wednesday, October 2, 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 challenge in search of a ChatClient, it’ll use the property to make one utilizing naming conventions within the openai starter challenge. Within the easy helloworld instance we’re taking a look at, 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.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 {

	personal closing 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 shall 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 secret is set as an atmosphere variable:

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here