Java based function callback demo
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.example.java_ai_function_callback;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringAiJavaFunctionCallbackApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringAiJavaFunctionCallbackApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner init(ChatClient.Builder chatClientBuilder) {
|
||||
return args -> {
|
||||
try {
|
||||
ChatClient chatClient = chatClientBuilder.build();
|
||||
ChatResponse response = chatClient
|
||||
.prompt("What are the weather conditions in San Francisco, Tokyo, and Paris? Find the temperature in Celsius for each of the three locations.")
|
||||
.functions("WeatherInfo")
|
||||
.call().chatResponse();
|
||||
|
||||
System.out.println("Response: " + response);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("Error during weather check: " + e.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public enum Unit {
|
||||
C("metric"),
|
||||
F("imperial");
|
||||
|
||||
private final String unitName;
|
||||
|
||||
Unit(String unitName) {
|
||||
this.unitName = unitName;
|
||||
}
|
||||
|
||||
public String getUnitName() {
|
||||
return unitName;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public FunctionCallback weatherFunctionInfo(Function<WeatherRequest, WeatherResponse> currentWeather) {
|
||||
return FunctionCallback.builder()
|
||||
.description(
|
||||
"Find the weather conditions, forecasts, and temperatures for a location, like a city or state."
|
||||
)
|
||||
.function("WeatherInfo", currentWeather)
|
||||
.inputType(WeatherRequest.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<WeatherRequest, WeatherResponse> currentWeather() {
|
||||
return request -> new MockJavaWeatherService().apply(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class MockJavaWeatherService implements Function<WeatherRequest, WeatherResponse> {
|
||||
|
||||
@Override
|
||||
public WeatherResponse apply(WeatherRequest weatherRequest) {
|
||||
double temperature = 10.0;
|
||||
if (weatherRequest.getLocation().contains("Paris")) {
|
||||
temperature = 15.0;
|
||||
}
|
||||
else if (weatherRequest.getLocation().contains("Tokyo")) {
|
||||
temperature = 10.0;
|
||||
}
|
||||
else if (weatherRequest.getLocation().contains("San Francisco")) {
|
||||
temperature = 30.0;
|
||||
}
|
||||
|
||||
return new WeatherResponse(temperature, 15.0, 20.0, 2.0, 53, 45, Unit.C);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.example.java_ai_function_callback;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonClassDescription;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonClassDescription("Weather API request")
|
||||
public class WeatherRequest {
|
||||
|
||||
public WeatherRequest() {
|
||||
}
|
||||
|
||||
@JsonProperty(required = true, value = "location")
|
||||
@JsonPropertyDescription("The city and state e.g. San Francisco, CA")
|
||||
private String location = "";
|
||||
|
||||
@JsonProperty(required = true, value = "lat")
|
||||
@JsonPropertyDescription("The city latitude")
|
||||
private double lat = 0.0;
|
||||
|
||||
@JsonProperty(required = true, value = "lon")
|
||||
@JsonPropertyDescription("The city longitude")
|
||||
private double lon = 0.0;
|
||||
|
||||
@JsonProperty(required = true, value = "unit")
|
||||
@JsonPropertyDescription("Temperature unit")
|
||||
private SpringAiJavaFunctionCallbackApplication.Unit unit = SpringAiJavaFunctionCallbackApplication.Unit.C;
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public double getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public void setLat(double lat) {
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
public double getLon() {
|
||||
return lon;
|
||||
}
|
||||
|
||||
public void setLon(double lon) {
|
||||
this.lon = lon;
|
||||
}
|
||||
|
||||
public SpringAiJavaFunctionCallbackApplication.Unit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(SpringAiJavaFunctionCallbackApplication.Unit unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.example.java_ai_function_callback;
|
||||
|
||||
public class WeatherResponse {
|
||||
|
||||
private final double temp;
|
||||
|
||||
private final double feels_like;
|
||||
|
||||
private final double temp_min;
|
||||
|
||||
private final double temp_max;
|
||||
|
||||
private final int pressure;
|
||||
|
||||
private final int humidity;
|
||||
|
||||
private final SpringAiJavaFunctionCallbackApplication.Unit unit;
|
||||
|
||||
public WeatherResponse(double temp, double feels_like, double temp_min,
|
||||
double temp_max, int pressure, int humidity, SpringAiJavaFunctionCallbackApplication.Unit unit) {
|
||||
this.temp = temp;
|
||||
this.feels_like = feels_like;
|
||||
this.temp_min = temp_min;
|
||||
this.temp_max = temp_max;
|
||||
this.pressure = pressure;
|
||||
this.humidity = humidity;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public double getTemp() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
public double getFeels_like() {
|
||||
return feels_like;
|
||||
}
|
||||
|
||||
public double getTemp_min() {
|
||||
return temp_min;
|
||||
}
|
||||
|
||||
public double getTemp_max() {
|
||||
return temp_max;
|
||||
}
|
||||
|
||||
public int getPressure() {
|
||||
return pressure;
|
||||
}
|
||||
|
||||
public int getHumidity() {
|
||||
return humidity;
|
||||
}
|
||||
|
||||
public SpringAiJavaFunctionCallbackApplication.Unit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.application.name=java-ai-function-callback
|
||||
|
||||
spring.ai.openai.api-key=<your-open-ai-key>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.java_ai_function_callback;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringAiJavaFunctionCallbackApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user