LangChain.js - AI 代理工具

@tiptap-pro/ai-toolkit-langchain 包提供了可以添加到你使用 LangChain.js 构建的 AI 代理中的工具定义。

模型生成的工具调用随后可以在客户端通过 executeTool 方法 执行。

示例用法

安装该包。

npm install @tiptap-pro/ai-toolkit-langchain

将工具定义提供给你的 LangChain.js 模型。

import { ChatOpenAI } from '@langchain/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-langchain'

const llm = new ChatOpenAI({ model: 'gpt-5.4-mini' })
const llmWithTools = llm.bindTools(toolDefinitions())

将工具定义与你的自定义工具结合使用。

import { ChatOpenAI } from '@langchain/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-langchain'
import { DynamicStructuredTool } from '@langchain/core/tools'
import { z } from 'zod'

const llm = new ChatOpenAI({ model: 'gpt-5.4-mini' })

const customTools = [
  new DynamicStructuredTool({
    name: 'weather',
    description: '获取某地的天气',
    schema: z.object({
      location: z.string(),
    }),
    func: async ({ location }) => {
      return `该地区 ${location} 的天气晴朗,气温72°F`
    },
  }),
]

const llmWithTools = llm.bindTools([...toolDefinitions(), ...customTools])

API 参考

toolDefinitions

为 Tiptap AI 工具包创建与 LangChain.js 兼容的工具定义。

参数 (ToolDefinitionsOptions)

  • tools?: EnabledTools - 通过将值设置为 true(启用)或 false(禁用)来启用或禁用特定工具。
    • tiptapRead?: boolean - 启用/禁用 tiptapRead 工具(默认:true
    • tiptapEdit?: boolean - 启用/禁用 tiptapEdit 工具(默认:true
    • tiptapReadSelection?: boolean - 启用/禁用 tiptapReadSelection 工具(默认:true
    • getThreads?: boolean - 启用/禁用 getThreads 工具(默认:false
    • editThreads?: boolean - 启用/禁用 editThreads 工具(默认:false

返回值

一个包含已启用工具定义的数组,可用于 LangChain.js 的工具调用系统。完整工具列表详见可用工具页面。