Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreroman committed May 28, 2024
1 parent 12b83ca commit 9632305
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Broadcom, Inc. or its affiliates
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.weather;

class WeatherFunctions {
public static final String GET_WEATHER_BY_CITY = "getWeatherByCity";
public static final String GET_WEATHER_BY_CITIES = "getWeatherByCities";

private WeatherFunctions() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.function;
package com.broadcom.tanzu.demos.springai101.weather;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
Expand All @@ -35,7 +35,7 @@ String weather(@RequestParam("city") String city) {
// Rely on a function to get additional (live) data.
return chatClient.prompt()
.user(p -> p.text("What is the current temperature in {city}?").param("city", city))
.functions(Functions.GET_WEATHER_BY_CITY)
.functions(WeatherFunctions.GET_WEATHER_BY_CITY)
.call()
.content();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.function;
package com.broadcom.tanzu.demos.springai101.weather;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
Expand All @@ -36,7 +36,7 @@ TemperatureResponse weather(@RequestParam("city") String city) {
// This allows you to use AI generated responses without having to manually parse the content.
return chatClient.prompt()
.user(p -> p.text("What is the current temperature in {city}?").param("city", city))
.functions(Functions.GET_WEATHER_BY_CITY)
.functions(WeatherFunctions.GET_WEATHER_BY_CITY)
.call()
.entity(TemperatureResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.function;
package com.broadcom.tanzu.demos.springai101.weather;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
Expand All @@ -33,10 +33,10 @@ class WeatherV3Controller {
@GetMapping(value = "/weather/v3", produces = MediaType.TEXT_PLAIN_VALUE)
String weather(@RequestParam("q") String query) {
// Use this endpoint to query your LLM with any requests.
// Whenever you ask for weather information for one or more cities, the functions will be automatically called.
// Whenever you ask for weather information from one or more cities, functions are automatically called as needed.
return chatClient.prompt()
.user(query)
.functions(Functions.GET_WEATHER_BY_CITY, Functions.GET_WEATHER_BY_CITIES)
.functions(WeatherFunctions.GET_WEATHER_BY_CITY, WeatherFunctions.GET_WEATHER_BY_CITIES)
.call()
.content();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.function;
package com.broadcom.tanzu.demos.springai101.weather;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -45,7 +45,7 @@ TemperatureResponse weather(@RequestParam("u") URL url) {
Then, get the current weather for this city.
""")
.media(MimeTypeUtils.IMAGE_JPEG, url))
.functions(Functions.GET_WEATHER_BY_CITY)
.functions(WeatherFunctions.GET_WEATHER_BY_CITY)
.call()
.entity(TemperatureResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.weather;
package com.broadcom.tanzu.demos.springai101.weather.impl;

/**
* Hold weather information for a city.
*
* @param city city name
* @param temperature temperature in Celsius
*/
public record Weather(
record Weather(
String city,
float temperature
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.weather;
package com.broadcom.tanzu.demos.springai101.weather.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.weather;
package com.broadcom.tanzu.demos.springai101.weather.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.function;
package com.broadcom.tanzu.demos.springai101.weather.impl;

import com.broadcom.tanzu.demos.springai101.weather.Weather;
import com.broadcom.tanzu.demos.springai101.weather.WeatherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
Expand All @@ -32,21 +30,18 @@
import java.util.stream.Collectors;

@Configuration(proxyBeanMethods = false)
class Functions {
public static final String GET_WEATHER_BY_CITY = "getWeatherByCity";
public static final String GET_WEATHER_BY_CITIES = "getWeatherByCities";

private final Logger logger = LoggerFactory.getLogger(Functions.class);
class WeatherFunctionsConfig {
private final Logger logger = LoggerFactory.getLogger(WeatherFunctionsConfig.class);

@Bean
@Description("""
Get the current weather in a given city, including temperature (in Celsius).
Call this function if you need to get the weather for a single city.
Call this function if you need to get the weather in a single city.
""")
Function<ByCityRequest, Weather> getWeatherByCity(WeatherService weatherService) {
// Map a Spring AI function (including description which will be used by the LLM) to your business function.
return req -> {
logger.info("Loading weather in {} using OpenWeatherMap", req.city());
logger.info("Loading weather from {} using OpenWeatherMap", req.city());
return weatherService.getWeatherByCity(req.city());
};
}
Expand All @@ -55,7 +50,7 @@ Function<ByCityRequest, Weather> getWeatherByCity(WeatherService weatherService)
@Description("""
Get the current weather in different cities, all at once.
The result is a map of weather details (including temperature in Celsius) by city.
Call this function to optimize calls when you need to get the weather for different cities.
Call this function to optimize calls when you need to get the weather in different cities.
""")
Function<ByCitiesRequest, Map<String, Weather>> getWeatherByCities(WeatherService weatherService, AsyncTaskExecutor taskExecutor) {
return req -> {
Expand All @@ -67,7 +62,7 @@ Function<ByCitiesRequest, Map<String, Weather>> getWeatherByCities(WeatherServic
final var tasks = new ArrayList<CompletableFuture<Weather>>(req.cities().length);
for (final var city : req.cities()) {
final var task = taskExecutor.submitCompletable(() -> {
logger.debug("Asynchronously loading weather in {} using OpenWeatherMap", city);
logger.debug("Asynchronously loading weather from {} using OpenWeatherMap", city);
return weatherService.getWeatherByCity(city);
});
tasks.add(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

package com.broadcom.tanzu.demos.springai101.weather;
package com.broadcom.tanzu.demos.springai101.weather.impl;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.stereotype.Service;

@Service
public class WeatherService {
class WeatherService {
private final WeatherApi api;
private final ObservationRegistry observationRegistry;

Expand Down

0 comments on commit 9632305

Please sign in to comment.