Files
spring-ai/observability/src/main/java/com/ankurm/observability/AiController.java
T

48 lines
1.7 KiB
Java

package com.ankurm.observability;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
/**
* Four endpoints, each tagging its ChatClient call with an {@code endpoint} value in the request
* context. {@link EndpointTagConvention} turns that into a metric tag, which is what lets the
* dashboard show cost per endpoint.
*/
@RestController
public class AiController {
private final ChatClient chat;
private final WeatherTools tools;
public AiController(ChatClient.Builder builder, WeatherTools tools) {
this.chat = builder.build();
this.tools = tools;
}
@GetMapping("/ask")
public String ask(@RequestParam String q) {
return chat.prompt().user(q).advisors(a -> a.param(EndpointTagConvention.KEY, "ask")).call().content();
}
@GetMapping("/summarize")
public String summarize(@RequestParam String text) {
return chat.prompt().system("Summarize the user's text in one sentence.").user(text)
.advisors(a -> a.param(EndpointTagConvention.KEY, "summarize")).call().content();
}
@GetMapping("/weather")
public String weather(@RequestParam String city) {
return chat.prompt().user("What is the weather in " + city + "?").tools(tools)
.advisors(a -> a.param(EndpointTagConvention.KEY, "weather")).call().content();
}
@GetMapping("/stream")
public Flux<String> stream(@RequestParam String q) {
return chat.prompt().user(q).advisors(a -> a.param(EndpointTagConvention.KEY, "stream")).stream().content();
}
}