聊聊langchain4j的Tools(Function Calling)

本文主要研究一下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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值