序
本文主要研究一下langchain4j的Tools(Function Calling)
示例
tool
@Slf4j
public class WeatherTools {
@Tool("Returns the weather forecast for tomorrow for a given city")
String getWeather(@P("The city for which the weather forecast should be returned") String city) {
log.info("getWeather called");
return "The weather tomorrow in " + city + " is 25°C";
}
@Tool("Returns the date for tomorrow")
LocalDate getTomorrow() {
log.info("getTomorrow called");
return LocalDate.now().plusDays(1);
}
@Tool("Transforms Celsius degrees into Fahrenheit")
double celsiusToFahrenheit(@P("The celsius degree to be transformed into fahrenheit") double celsius) {
log.info("celsiusToFahrenheit called");
return (celsius * 1.8) + 32;
}
String iAmNotATool() {
log.info("iAmNotATool called");
return "I am not a method annotated with @Tool";
}
}
这里用@Tool注解来描述这个方法的用途,用@P注解来描述参数
Low-level
public static void main(String[] args) {
// STEP 1: User specify tools and query
// Tools
WeatherTools weatherTools = new WeatherTools();
List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(weatherTools);
// User query
List<ChatMessage> chatMessages = new ArrayList<>();
UserMessage userMessage = userMessage("What will the weather be like in London tomorrow?");
chatMessages.add(userMessage);
// Chat request
ChatRequest chatRequest = ChatRequest.builder()
.messages(chatMessages)
.parameters(ChatRequestParam
深入探究langchain4j的Tools功能

最低0.47元/天 解锁文章
984

被折叠的 条评论
为什么被折叠?



