File size: 1,008 Bytes
09321b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from copy import deepcopy

from .tool import Tool


class LangchainTool(Tool):

    def __init__(self, langchain_tool):
        from langchain.tools import BaseTool

        if not isinstance(langchain_tool, BaseTool):
            raise ValueError('langchain_tool should be type of langchain tool')
        self.langchain_tool = langchain_tool
        self.parse_langchain_schema()
        super().__init__()

    def parse_langchain_schema(self):
        # convert langchain tool schema to modelscope_agent tool schema
        self.description = self.langchain_tool.description
        self.name = self.langchain_tool.name
        self.parameters = []
        for name, arg in self.langchain_tool.args.items():
            tool_arg = deepcopy(arg)
            tool_arg['name'] = name
            tool_arg['required'] = True
            tool_arg.pop('title')
            self.parameters.append(tool_arg)

    def _local_call(self, *args, **kwargs):
        return {'result': self.langchain_tool.run(kwargs)}