聊聊langchain4j的Tools(Function Calling)

深入探究langchain4j的Tools功能

本文主要研究一下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 中,函数调用(Function Calling)与 vLLM 的集成提供了一种高效、灵活的方式来扩展语言模型的能力。vLLM 是一个高性能的 LLM 推理和服务系统,支持快速解码和批处理请求,适用于大规模部署场景。LangChain4j 通过其 Runnable 协议[^2] 和工具集成机制,使得与 vLLM 的整合成为可能。 ### 函数调用的基本流程 1. **定义可调用函数**:开发者可以定义 Java 对象作为工具,这些对象的方法可以通过 JSON Schema 描述并注册到 LangChain4j 中。 2. **模型提示生成函数调用**:大语言模型根据输入提示决定是否需要调用外部函数,并生成相应的函数名和参数。 3. **执行函数调用**:LangChain4j 调用对应的 Java 方法,获取结果。 4. **将结果反馈给模型**:执行结果被注入回模型上下文,用于生成最终响应。 ### 与 vLLM 的集成方式 由于 vLLM 提供了高效的推理能力,LangChain4j 可以将其作为底层执行引擎。具体集成步骤包括: - 配置 vLLM 模型服务端点,确保其可通过 HTTP 或 gRPC 访问。 - 在 LangChain4j 中配置远程模型适配器,指向 vLLM 提供的服务地址。 - 使用 `Runnable` 接口封装模型推理逻辑,使其兼容 LangChain4j 的链式调用结构。 ### 示例代码:使用 vLLM 的 LangChain4j 函数调用 以下是一个简化版的示例,展示如何在 LangChain4j 中配置与 vLLM 的集成,并实现函数调用: ```java import dev.langchain4j.chain.function.FunctionCallingChain; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.model.vllm.VllmModel; import dev.langchain4j.service.AiServices; public class VllmFunctionCallingExample { public interface MyTools { String searchProduct(String query); } public static void main(String[] args) { // 配置 vLLM 模型服务 VllmModel model = VllmModel.builder() .baseUrl("http://localhost:8080") // vLLM 服务地址 .modelName("Llama-3-8B") // 使用的模型名称 .build(); // 创建工具代理 MyTools tools = AiServices.create(model, MyTools.class); // 构建函数调用链 FunctionCallingChain<MyTools> chain = FunctionCallingChain.builder(model, tools) .promptTemplate("用户查询: {input}\n请判断是否需要调用工具进行搜索") .build(); // 执行链式调用 AiMessage response = chain.invoke("我想买一台笔记本电脑"); System.out.println(response.content().text()); } } ``` 该示例展示了如何通过 LangChain4j 将用户查询转换为对 `searchProduct` 工具的调用,并利用 vLLM 提供的高性能推理服务来完成任务。 ### 注意事项 - **性能优化**:为了充分发挥 vLLM 的优势,建议启用批量推理和并发请求处理功能。 - **错误处理**:在生产环境中,需加入重试机制、超时控制以及日志记录,以增强系统的健壮性[^4]。 - **安全性**:确保 vLLM 服务的安全访问控制,防止未授权调用和数据泄露。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值