Skip to content

Function

FunctionTool #

Bases: AsyncBaseTool

函数工具。

一个接受函数作为输入的工具。

Source code in llama_index/core/tools/function_tool.py
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class FunctionTool(AsyncBaseTool):
    """函数工具。

    一个接受函数作为输入的工具。"""

    def __init__(
        self,
        fn: Callable[..., Any],
        metadata: ToolMetadata,
        async_fn: Optional[AsyncCallable] = None,
    ) -> None:
        self._fn = fn
        if async_fn is not None:
            self._async_fn = async_fn
        else:
            self._async_fn = sync_to_async(self._fn)
        self._metadata = metadata

    @classmethod
    def from_defaults(
        cls,
        fn: Callable[..., Any],
        name: Optional[str] = None,
        description: Optional[str] = None,
        return_direct: bool = False,
        fn_schema: Optional[Type[BaseModel]] = None,
        async_fn: Optional[AsyncCallable] = None,
        tool_metadata: Optional[ToolMetadata] = None,
    ) -> "FunctionTool":
        if tool_metadata is None:
            name = name or fn.__name__
            docstring = fn.__doc__
            description = description or f"{name}{signature(fn)}\n{docstring}"
            if fn_schema is None:
                fn_schema = create_schema_from_function(
                    f"{name}", fn, additional_fields=None
                )
            tool_metadata = ToolMetadata(
                name=name,
                description=description,
                fn_schema=fn_schema,
                return_direct=return_direct,
            )
        return cls(fn=fn, metadata=tool_metadata, async_fn=async_fn)

    @property
    def metadata(self) -> ToolMetadata:
        """元数据。"""
        return self._metadata

    @property
    def fn(self) -> Callable[..., Any]:
        """功能。"""
        return self._fn

    @property
    def async_fn(self) -> AsyncCallable:
        """异步函数。"""
        return self._async_fn

    def call(self, *args: Any, **kwargs: Any) -> ToolOutput:
        """调用。"""
        tool_output = self._fn(*args, **kwargs)
        return ToolOutput(
            content=str(tool_output),
            tool_name=self.metadata.name,
            raw_input={"args": args, "kwargs": kwargs},
            raw_output=tool_output,
        )

    async def acall(self, *args: Any, **kwargs: Any) -> ToolOutput:
        """调用。"""
        tool_output = await self._async_fn(*args, **kwargs)
        return ToolOutput(
            content=str(tool_output),
            tool_name=self.metadata.name,
            raw_input={"args": args, "kwargs": kwargs},
            raw_output=tool_output,
        )

    def to_langchain_tool(
        self,
        **langchain_tool_kwargs: Any,
    ) -> "Tool":
        """到langchain工具。"""
        from llama_index.core.bridge.langchain import Tool

        langchain_tool_kwargs = self._process_langchain_tool_kwargs(
            langchain_tool_kwargs
        )
        return Tool.from_function(
            func=self.fn,
            coroutine=self.async_fn,
            **langchain_tool_kwargs,
        )

    def to_langchain_structured_tool(
        self,
        **langchain_tool_kwargs: Any,
    ) -> "StructuredTool":
        """将结构化工具转换为语言链。"""
        from llama_index.core.bridge.langchain import StructuredTool

        langchain_tool_kwargs = self._process_langchain_tool_kwargs(
            langchain_tool_kwargs
        )
        return StructuredTool.from_function(
            func=self.fn,
            coroutine=self.async_fn,
            **langchain_tool_kwargs,
        )

metadata property #

metadata: ToolMetadata

元数据。

fn property #

fn: Callable[..., Any]

功能。

async_fn property #

async_fn: AsyncCallable

异步函数。

call #

call(*args: Any, **kwargs: Any) -> ToolOutput

调用。

Source code in llama_index/core/tools/function_tool.py
84
85
86
87
88
89
90
91
92
def call(self, *args: Any, **kwargs: Any) -> ToolOutput:
    """调用。"""
    tool_output = self._fn(*args, **kwargs)
    return ToolOutput(
        content=str(tool_output),
        tool_name=self.metadata.name,
        raw_input={"args": args, "kwargs": kwargs},
        raw_output=tool_output,
    )

acall async #

acall(*args: Any, **kwargs: Any) -> ToolOutput

调用。

Source code in llama_index/core/tools/function_tool.py
 94
 95
 96
 97
 98
 99
100
101
102
async def acall(self, *args: Any, **kwargs: Any) -> ToolOutput:
    """调用。"""
    tool_output = await self._async_fn(*args, **kwargs)
    return ToolOutput(
        content=str(tool_output),
        tool_name=self.metadata.name,
        raw_input={"args": args, "kwargs": kwargs},
        raw_output=tool_output,
    )

to_langchain_tool #

to_langchain_tool(**langchain_tool_kwargs: Any) -> Tool

到langchain工具。

Source code in llama_index/core/tools/function_tool.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def to_langchain_tool(
    self,
    **langchain_tool_kwargs: Any,
) -> "Tool":
    """到langchain工具。"""
    from llama_index.core.bridge.langchain import Tool

    langchain_tool_kwargs = self._process_langchain_tool_kwargs(
        langchain_tool_kwargs
    )
    return Tool.from_function(
        func=self.fn,
        coroutine=self.async_fn,
        **langchain_tool_kwargs,
    )

to_langchain_structured_tool #

to_langchain_structured_tool(
    **langchain_tool_kwargs: Any,
) -> StructuredTool

将结构化工具转换为语言链。

Source code in llama_index/core/tools/function_tool.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def to_langchain_structured_tool(
    self,
    **langchain_tool_kwargs: Any,
) -> "StructuredTool":
    """将结构化工具转换为语言链。"""
    from llama_index.core.bridge.langchain import StructuredTool

    langchain_tool_kwargs = self._process_langchain_tool_kwargs(
        langchain_tool_kwargs
    )
    return StructuredTool.from_function(
        func=self.fn,
        coroutine=self.async_fn,
        **langchain_tool_kwargs,
    )