05、Tools

对于一些LLM来说除了可以生成文本外,还可以触发操作。

有一个概念叫“工具/函数调用”。它允许LLM在必要的时候调用一个或多个可用的工具,这些工具一般是针对业务由开发人员来定义的。

这里说的工具可以是任何东西。如:网络搜索、外部API的调用或特定代码的执行......LLM不能自己调用这些工具。然而它们在响应中表达了调用特定工具的意图,作为开发人员应该使用提供的参数来执行这个工具,并报告出这个执行的结果。

两个抽象层次

对于工具的使用,LangChain4j提供了两个层次的抽象

  • 低级别的,使用ChatLanguageModel和ToolSpecification的api
  • 高级别的,使用AiServece和@Tool注解的java方法

低级别的

对于它可以使用ChatLanguageModel的generate(List<ChatMessage),List<ToolSpecification>)方法。StreamingChatLangugageModel也有类似的方法。

ToolSpecification对象中包含了关于工具的所有信息,这些信息包含:

  • name:工具的名称
  • description:工具的描述
  • parameters:工具的参数

注意:要尽可能详细地提供工具的信息,清晰的名字、全面的描述及每个参数的描述等

我们定义一个工具的类

public class WeatherTools {

    @Tool("根据给出的城市,获取天气信息")
    String getWeather(
            // 这里的@P()注解,用在工具的参数上用来说明参数的
            @P("用来指定要获取天气的城市") String city) {
        return "[" +  city + "],天气信息: 晴,12~19℃,北风2级 ";
    }
}

这里的方法使用@Tool注解标注,其中提供的信息是对这个工具的说明

在Controller类中我们在进行具体聊天时去执行工具获取对应的信息

@Slf4j
@RestController
@RequestMapping("/tools")
public class ToolsController {


    @Resource
    private StreamingChatLanguageModel streamingChatLanguageModel;
    @Resource
    private ChatLanguageModel chatLanguageModel;


    @GetMapping(path = "/chat", produces = "text/plain;charset=UTF-8")
    public Flux<String> weather(@RequestParam("message") String message) {
        // 创建工具
        /*ToolSpecification weatherTool = ToolSpecification.builder()
                .name("getWeather")
                .description("获取天气信息")
                .parameters(JsonObjectSchema.builder()
                        .addStringProperty("city", "城市")
                        .required("city")
                        .build())
                .build();*/
        List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(WeatherTools.class);
        Response<AiMessage> generate = chatLanguageModel.generate(List.of(UserMessage.from(message)), toolSpecifications);

        WeatherTools wt = new WeatherTools();
        List<ChatMessage> messages = new ArrayList<>();
        generate.content().toolExecutionRequests().forEach(toolExecutionRequest -> {
            System.out.println(toolExecutionRequest.name());
            System.out.println(toolExecutionRequest.arguments());
            // 手动调用工具
            String toolExecutorResponse = new DefaultToolExecutor(wt, toolExecutionRequest).execute(toolExecutionRequest, null);
            ToolExecutionResultMessage toolExecutionResultMessage = ToolExecutionResultMessage.from(toolExecutionRequest, toolExecutorResponse);
            
            messages.add(UserMessage.from(message)); // UserMessage
            messages.add(generate.content()); // AIMessage
            messages.add(toolExecutionResultMessage); // ToolExecutionResultMessage
        });


        return Flux.create(slink -> {
            streamingChatLanguageModel.generate(messages, toolSpecifications, new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值