使用LangChain4J实现Agent与Tool调用

一些LLM除了生成文本,还可触发操作。

有一个被称为“工具(tools)”或“函数调用(function calling)”的概念。它允许LLM在必要时调用一或多个由开发者定义的工具。工具可以是任何东西:网页搜索、外部API调用、或执行一段特定代码等。LLM本身无法实际调用这些工具;它们会在响应中表达出调用某个工具的意图(而不是直接生成文本)。我们开发者,则需要根据提供的参数来执行这些工具并报告工具执行结果。

如我们知道LLM本身并不擅长数学运算。若你的应用场景涉及偶尔的数学计算,你可能希望为LLM提供一个“math tool”。通过在请求中声明一个或多个工具,LLM可以在认为适合时调用其中一个。如遇到数学问题并拥有一组数学工具时,LLM可能会决定首先调用其中的一个来正确回答问题。

1 有无工具时的效果

1.1 没有工具的消息示例

Request:
- messages:
    - UserMessage:
        - text: What is the square root of 475695037565?

Response:
- AiMessage:
    - text: The square root of 475695037565 is approximately 689710.

接近正确,但不完全对。

1.2 使用以下工具的消息示例

@Tool("Sums 2 given numbers")
public double sum(double a, double b) {
    return a + b;
}

@Tool("Returns a square root of a given number")
public double squareRoot(double x) {
    return Math.sqrt(x);
}
Request 1:
- messages:
    - UserMessage:
        - text: What is the square root of 475695037565?
- tools:
    - sum(double a, double b): Sums 2 given numbers
    - squareRoot(double x): Returns a square root of a given number

Response 1:
- AiMessage:
    - toolExecutionRequests:
        - squareRoot(475695037565)


... here we are executing the squareRoot method with the "475695037565" argument and getting "689706.486532" as a result ...


Request 2:
- messages:
    - UserMessage:
        - text: What is the square root of 475695037565?
    - AiMessage:
        - toolExecutionRequests:
            - squareRoot(475695037565)
    - ToolExecutionResultMessage:
        - text: 689706.486532

Response 2:
- AiMessage:
    - text: The square root of 475695037565 is 689706.486532.

如你所见,当LLM拥有工具时,它可在适当时决定调用其中的一个。

这是一个非常强大的功能。这简单例子,我们给LLM提供原始的数学工具,但可想象如提供如googleSearchsendEmail工具,然后提供一个查询“我的朋友想知道AI领域的最新消息。请将简短的总结发送到friend@email.com”,那它可用googleSearch工具找到最新消息,然后总结并通过sendEmail工具发送总结。

经验法则

为了增加LLM调用正确工具和参数的几率,我们应该提供清晰且明确的:

  • 工具名称
  • 工具的功能描述以及何时使用
  • 每个工具参数的描述

一个好的经验法则是:如果人类能理解工具的用途和如何使用,那么LLM也能理解。

LLM被专门微调,以检测何时调用工具以及如何调用它们。某些模型甚至可以一次调用多个工具,如OpenAI

注意,工具/函数调用与JSON模式不同。

2 两个抽象层次

LangChain4j 提供两个使用工具的抽象层:

  • 底层,使用 ChatLanguageModel API
  • 高级,使用AI服务@Tool注解的Java方法

3 底层工具API

3.1 generate

可用ChatLanguageModel#generate(List<ChatMessage>, List<ToolSpecification>)

/**
  * 根据消息列表和工具规范列表从模型生成响应。响应可以是文本消息,也可以是执行指定工具之一的请求。通常,该列表包含按以下顺序排列的消息:System (optional) - User - AI - User - AI - User ...
  * messages – 消息列表
  * toolSpecifications – 允许模型执行的工具列表。该模型自主决定是否使用这些工具中的任何一个
  * return:模型生成的响应
  * AiMessage 可以包含文本响应或执行其中一个工具的请求。
  */
default Response<AiMessage> generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications) {
    throw new IllegalArgumentException("Tools are currently not supported by this model");
}

类似方法也存于StreamingChatLanguageModel

3.2 ToolSpecification

package dev.langchain4j.agent.tool;

// 包含工具所有信息
public class ToolSpecification {
    // 工具的`名称`
    private final String name;
    // 工具的`描述`
    private final String description;
    // 工具的`参数`及其描述
    private final ToolParameters parameters;

推荐尽可能提供关于工具的所有信息:清晰的名称、详尽的描述和每个参数的描述等。

3.2.1 创建ToolSpecification
① 手动
ToolSpecification toolSpecification = ToolSpecification.builder()
    .name("getWeather")
    .description("返回指定城市的天气预报")
    .addParameter("city", type("string"), description("应返回天气预报的城市"))
    .addParameter("temperatureUnit", enums(TemperatureUnit.class)) // 枚举 TemperatureUnit { 摄氏, 华氏 }
    .build();
② 使用辅助方法
  • ToolSpecifications.toolSpecificationsFrom(Class)
  • ToolSpecifications.toolSpecificationsFrom(Object)
  • ToolSpecifications.toolSpecificationFrom(Method)
class WeatherTools { 
  
    @Tool("Returns the weather forecast for a given city")
    String getWeather(
            @P("The city for which the weather forecast should be returned") String city,
            TemperatureUnit temperatureUnit
    ) {
        ...
    }
}

List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(WeatherTools.class);

一旦你拥有List<ToolSpecification>,可调用模型:

UserMessage userMessage = UserMessage.from("伦敦明天的天气如何?");
Response<AiMessage> response = model.generate(List.of(userMessage), toolSpecifications);
AiMessage aiMessage = response.content();

若LLM决定调用工具,返回的AiMessage将包含toolExecutionRequests字段中的数据。此时,AiMessage.hasToolExecutionRequests()将返回true。根据LLM不同,它可包含一或多个ToolExecutionRequest对象(某些LLM支持并行调用多个工具)。

每个ToolExecutionRequest应包含:

public class ToolExecutionRequest {
  	// 工具调用的`id`(某些LLM不提供)
    private final String id;
  	// 要调用的工具名称,例如:`getWeather`
    private final String name;
  	// 工具的`参数`,例如:`{ "city": "London", "temperatureUnit": "CELSIUS" }`
    private final String arguments;

你要用ToolExecutionRequest中的信息手动执行工具。

如希望将工具执行的结果发回LLM,你要为每个ToolExecutionRequest创建一个ToolExecutionResultMessage并与之前的所有消息一起发送:

String result = "预计明天伦敦会下雨。";
ToolExecutionResultMessage toolExecutionResultMessage = ToolExecutionResultMessage.from(toolExecutionRequest, result);
List<ChatMessage> messages = List.of(userMessage, aiMessage, toolExecutionResultMessage);
Response<AiMessage> response2 = model.generate(messages, toolSpecifications);

4 高级工具API

高层,你可为任何Java方法添加@Tool注解,并将其与AI服务一起使用。

AI服务会自动将这些方法转换为ToolSpecification,并在每次与LLM的交互中包含它们。当LLM决定调用工具时,AI服务将自动执行相应的方法,并将方法的返回值(如果有)发送回LLM。实现细节可以在DefaultToolExecutor中找到。

@Tool("Searches Google for relevant URLs, given the query")
public List<String> searchGoogle(@P("search query") String query) {
    return googleSearchService.search(query);
}

@Tool("Returns the content of a web page, given the URL")
public String getWebPageContent(@P("URL of the page") String url) {
    Document jsoupDocument = Jsoup.connect(url).get();
    return jsoupDocument.body().text();
}

4.1 @Tool

任何用@Tool注解并在构建AI服务时明确指定的Java方法,都可以被LLM执行

interface MathGenius {
    
    String ask(String question);
}

class Calculator {
    
    @Tool
    public double add(int a, int b) {
        return a + b;
    }

    @Tool
    public double squareRoot(double x) {
        return Math.sqrt(x);
    }
}

MathGenius mathGenius = AiServices.builder(MathGenius.class)
    .chatLanguageModel(model)
    .tools(new Calculator())
    .build();

String answer = mathGenius.ask("What is the square root of 475695037565?");

System.out.println(answer); // The square root of 475695037565 is 689706.486532.

调用ask方法时,会发生两次与LLM的交互,如前文所述。交互期间,会自动调用squareRoot方法。

@Tool注解有两个可选字段:

  • name: 工具的名称。如果未提供,方法名将作为工具名称。
  • value: 工具的描述。

根据具体工具,即使不提供描述,LLM也可能理解其用途(例如,add(a, b)很明显),但通常最好提供清晰且有意义的名称和描述。这样,LLM在决定是否调用工具以及如何调用时会有更多信息。

4.2 @P

方法的参数可以使用@P注解。

@P注解有两个字段:

  • value: 参数的描述,此字段是必填的。
  • required: 参数是否是必需的,默认值为true,此字段为可选。

4.3 @ToolMemoryId

如果AI服务方法的某个参数使用了@MemoryId注解,则可以在@Tool方法的参数上使用@ToolMemoryId进行注解。这样,提供给AI服务方法的值将自动传递给@Tool方法。这对于多个用户和/或每个用户有多个聊天或记忆的场景非常有用,可以在@Tool方法中区分它们。

4.4 访问已执行的工具

如果你希望访问AI服务调用过程中执行的工具,可以通过将返回类型封装在Result类中轻松实现:

interface Assistant {

    Result<String> chat(String userMessage);
}

Result<String> result = assistant.chat("取消我的预订 123-456");

String answer = result.content();
List<ToolExecution> toolExecutions = result.toolExecutions();

4.5 以编程方式指定工具

在使用AI服务时,也可以通过编程方式指定工具。这种方法非常灵活,因为工具可以从外部资源(如数据库和配置文件)加载。

工具名称、描述、参数名称和描述都可以使用ToolSpecification进行配置:

ToolSpecification toolSpecification = ToolSpecification.builder()
    .name("get_booking_details")
    .description("返回预订详情")
    .addParameter("bookingNumber", type("string"), description("B-12345格式的预订编号"))
    .build();

对于每个ToolSpecification,需要提供一个ToolExecutor实现来处理LLM生成的工具执行请求:

ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
    Map<String, Object> arguments = fromJson(toolExecutionRequest.arguments());
    String bookingNumber = arguments.get("bookingNumber").toString();
    Booking booking = getBooking(bookingNumber);
    return booking.toString();
};

旦我们拥有一个或多个(ToolSpecificationToolExecutor)对,我们可以在创建AI服务时指定它们:

Assistant assistant = AiServices.builder(Assistant.class)
    .chatLanguageModel(chatLanguageModel)
    .tools(singletonMap(toolSpecification, toolExecutor))
    .build();

4.6 动态指定工具

在使用AI服务时,每次调用时也可以动态指定工具。可以配置一个ToolProvider,该提供者将在每次调用AI服务时被调用,并提供应包含在当前请求中的工具。ToolProvider接受一个包含UserMessage和聊天记忆ID的ToolProviderRequest,并返回包含工具的ToolProviderResult,其形式为ToolSpecificationToolExecutor的映射。

下面是一个示例,展示如何仅在用户消息中包含“预订”一词时添加get_booking_details工具:

ToolProvider toolProvider = (toolProviderRequest) -> {
    if (toolProviderRequest.userMessage().singleText().contains("booking")) {
        ToolSpecification toolSpecification = ToolSpecification.builder()
            .name("get_booking_details")
            .description("返回预订详情")
            .addParameter("bookingNumber", type("string"))
            .build();
        return ToolProviderResult.builder()
            .add(toolSpecification, toolExecutor)
            .build();
    } else {
        return null;
    }
};

Assistant assistant = AiServices.builder(Assistant.class)
    .chatLanguageModel(model)
    .toolProvider(toolProvider)
    .build();

程序员为什么要学大模型?

大模型时代,火爆出圈的LLM大模型让程序员们开始重新评估自己的本领。 “AI会取代那些行业?”“谁的饭碗又将不保了?”等问题热议不断。

事实上,抢你饭碗的不是AI,而是会利用AI的人。

科大讯飞、阿里、华为等巨头公司发布AI产品后,很多中小企业也陆续进场!超高年薪,挖掘AI大模型人才! 如今大厂老板们,也更倾向于会AI的人,普通程序员,还有应对的机会吗?

与其焦虑……

不如成为「掌握AI工具的技术人」,毕竟AI时代,谁先尝试,谁就能占得先机!

但是LLM相关的内容很多,现在网上的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学,学习成本和门槛很高。

针对所有自学遇到困难的同学们,我帮大家系统梳理大模型学习脉络,将这份 LLM大模型资料 分享出来:包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

一、LLM大模型经典书籍

AI大模型已经成为了当今科技领域的一大热点,那以下这些大模型书籍就是非常不错的学习资源。

在这里插入图片描述

二、640套LLM大模型报告合集

这套包含640份报告的合集,涵盖了大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。(几乎涵盖所有行业)

在这里插入图片描述

三、LLM大模型系列视频教程

在这里插入图片描述

四、LLM大模型开源教程(LLaLA/Meta/chatglm/chatgpt)

在这里插入图片描述

LLM大模型学习路线

阶段1:AI大模型时代的基础理解

  • 目标:了解AI大模型的基本概念、发展历程和核心原理。

  • 内容

    • L1.1 人工智能简述与大模型起源
    • L1.2 大模型与通用人工智能
    • L1.3 GPT模型的发展历程
    • L1.4 模型工程
    • L1.4.1 知识大模型
    • L1.4.2 生产大模型
    • L1.4.3 模型工程方法论
    • L1.4.4 模型工程实践
    • L1.5 GPT应用案例

阶段2:AI大模型API应用开发工程

  • 目标:掌握AI大模型API的使用和开发,以及相关的编程技能。

  • 内容

    • L2.1 API接口
    • L2.1.1 OpenAI API接口
    • L2.1.2 Python接口接入
    • L2.1.3 BOT工具类框架
    • L2.1.4 代码示例
    • L2.2 Prompt框架
    • L2.3 流水线工程
    • L2.4 总结与展望

阶段3:AI大模型应用架构实践

  • 目标:深入理解AI大模型的应用架构,并能够进行私有化部署。

  • 内容

    • L3.1 Agent模型框架
    • L3.2 MetaGPT
    • L3.3 ChatGLM
    • L3.4 LLAMA
    • L3.5 其他大模型介绍

阶段4:AI大模型私有化部署

  • 目标:掌握多种AI大模型的私有化部署,包括多模态和特定领域模型。

  • 内容

    • L4.1 模型私有化部署概述
    • L4.2 模型私有化部署的关键技术
    • L4.3 模型私有化部署的实施步骤
    • L4.4 模型私有化部署的应用场景

这份 LLM大模型资料 包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值