组件兼容LangChain组件规范
更新时间:2025-04-08
概述
LangChain 是一个开源框架,旨在帮助开发者构建由大型语言模型(LLM)驱动的应用程序。它提供丰富的工具和接口,简化了聊天机器人、问答系统等应用的开发过程。用户在自己的业务中使用了LangChain框架,同时又希望使用AppBuilder的组件能力。AppBuilder提供了将组件转化为LangChain组件的方法,以便用户更好的融合自己的业务进行开发。
AppBuilder组件转为LangChain组件的具体方法
通过 create_langchain_tool( )方法,将 AppBuilder 组件转化为 langchain 的 StructuredTool 。
转化后,可以直接与 langchain 提供的工具链和环境集成。
Python
1import os
2import unittest
3import appbuilder
4from appbuilder.core.message import Message
5from appbuilder.core.component import Component
6
7class HelloWorldComponent(Component): // 定义 HelloWorldComponent 类,继承自 AppBuilder 的 Component
8 manifests = [ // 通过 manifests 定义组件的元数据,包括名称、描述和输入参数格式
9 {
10 "name": "hello_world",
11 "description": "向使用这个工具的人打招呼",
12 "parameters": {
13 "type": "object",
14 "properties": {
15 "name": {
16 "type": "string",
17 "description": "使用者的名字"
18 }
19 }
20 }
21 }
22 ]
23
24 def run(self, name: str, **kwargs): // 定义方法 run( ),接受一个 name 参数,并返回一个包含 name 的问候消息
25 return "hello world from {}".format(name)
26
27
28@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "")
29class TestLandmarkRecognition(unittest.TestCase):
30 def setUp(self):
31 self.component = HelloWorldComponent()
32
33 def test_to_langchain_tool(self):
34 tool = self.component.create_langchain_tool()
35 from langchain.tools import StructuredTool
36 self.assertIsInstance(tool, StructuredTool)