All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.agentexecutor.ToolInfo Maven / Gradle / Ivy

package dev.langchain4j.agentexecutor;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.service.tool.DefaultToolExecutor;
import dev.langchain4j.service.tool.ToolExecutor;
import lombok.NonNull;
import lombok.Value;
import lombok.experimental.Accessors;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import static dev.langchain4j.agent.tool.ToolSpecifications.toolSpecificationFrom;
import static java.util.Collections.unmodifiableList;

@Value
@Accessors( fluent = true)
public class ToolInfo {
    @NonNull ToolSpecification specification;
    @NonNull ToolExecutor executor;

    public static List of( Object ...objectsWithTools) {
        return fromArray( (Object[])objectsWithTools );
    }
    public static List fromArray( Object[] objectsWithTools ) {
        List toolSpecifications = new ArrayList<>();

        for (Object objectWithTools : objectsWithTools) {
            for (Method method : objectWithTools.getClass().getDeclaredMethods()) {
                if (method.isAnnotationPresent(Tool.class)) {
                    ToolSpecification toolSpecification = toolSpecificationFrom(method);
                    ToolExecutor executor = new DefaultToolExecutor(objectWithTools, method);
                    toolSpecifications.add( new ToolInfo( toolSpecification, executor));
                }
            }
        }
        return unmodifiableList(toolSpecifications);
    }
    public static List fromList(List objectsWithTools ) {
        return fromArray(objectsWithTools.toArray());
    }

}