Kotlinv2.4.0

内置工具

Koog 框架为 Kotlin 和 Java 提供了内置工具,用于处理智能体与用户交互的常见场景。

以下是可用的内置工具:

工具
名称
描述
SayToUser__say_to_user__允许智能体向用户发送消息。它将智能体消息打印到控制台,并带有 Agent says: 前缀。
AskUser__ask_user__允许智能体向用户请求输入。它将智能体消息打印到控制台并等待用户响应。
ExitTool__exit__允许智能体完成对话并终止会话。
ReadFileTool__read_file__读取文本文件,支持可选的行范围选择。返回带有元数据的格式化内容,使用从 0 开始的行索引。
EditFileTool__edit_file__对文件进行单个、有针对性的文本替换;也可以创建新文件或完全替换内容。
ListDirectoryTool__list_directory__以层次结构树的形式列出目录内容,支持可选的深度控制和 glob 筛选。
WriteFileTool__write_file__将文本内容写入文件(根据需要创建父目录)。

注册内置工具

与任何其他工具一样,内置工具必须添加到工具注册表才能供智能体使用。示例如下:

kotlin
// Create a tool registry with all built-in tools
val toolRegistry = ToolRegistry {
    tool(SayToUser)
    tool(AskUser)
    tool(ExitTool)
    tool(ReadFileTool(JVMFileSystemProvider.ReadOnly))
    tool(ListDirectoryTool(JVMFileSystemProvider.ReadOnly))
    tool(WriteFileTool(JVMFileSystemProvider.ReadWrite))
}

// Pass the registry when creating an agent
val agent = AIAgent(
    promptExecutor = simpleOpenAIExecutor(apiToken),
    systemPrompt = "You are a helpful assistant.",
    llmModel = OpenAIModels.Chat.GPT4o,
    toolRegistry = toolRegistry
)

通过在 Kotlin 和 Java 的同一个注册表中组合内置工具和自定义工具,您可以为您的智能体创建一套全面的功能。 要了解更多关于自定义工具的信息,请参阅基于注解的工具基于类的工具