Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | """ComposioLangChain class definition""" |
| 2 | |
| 3 | import types |
| 4 | import typing as t |
| 5 | from inspect import Signature |
| 6 | |
| 7 | from llama_index.core.tools import FunctionTool |
| 8 | |
| 9 | from composio.core.provider import AgenticProvider, AgenticProviderExecuteFn |
| 10 | from composio.types import Tool |
| 11 | from composio.utils.shared import ( |
| 12 | get_signature_format_from_schema_params, |
| 13 | normalize_tool_arguments, |
| 14 | reinstate_reserved_python_keywords, |
| 15 | substitute_reserved_python_keywords, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | class LlamaIndexProvider( |
| 20 | AgenticProvider[FunctionTool, t.List[FunctionTool]], |
| 21 | name="llamaindex", |
| 22 | ): |
| 23 | """ |
| 24 | Composio toolset for LlamaIndex framework. |
| 25 | """ |
| 26 | |
| 27 | def wrap_tool( |
| 28 | self, |
| 29 | tool: Tool, |
| 30 | execute_tool: AgenticProviderExecuteFn, |
| 31 | ) -> FunctionTool: |
| 32 | """ |
| 33 | Wrap a tool into a LlamaIndex FunctionTool object. |
| 34 | """ |
| 35 | schema_params, keywords = substitute_reserved_python_keywords( |
| 36 | schema=tool.input_parameters |
| 37 | ) |
| 38 | |
| 39 | def function(**kwargs: t.Any) -> t.Dict: |
| 40 | """Wrapper function for composio action.""" |
| 41 | kwargs = reinstate_reserved_python_keywords( |
| 42 | request=kwargs, keywords=keywords |
| 43 | ) |
| 44 | # Normalize defensively so a stringified payload is coerced to a dict (issue #2406). |
| 45 | return execute_tool( |
| 46 | slug=tool.slug, arguments=normalize_tool_arguments(kwargs) |
| 47 | ) |
| 48 | |
| 49 | action_func = types.FunctionType( |
| 50 | function.__code__, |
| 51 | globals=globals(), |
| 52 | name=tool.slug, |
| 53 | closure=function.__closure__, |
| 54 | ) |
| 55 | action_func.__signature__ = Signature( # type: ignore |
| 56 | parameters=get_signature_format_from_schema_params( |
| 57 | schema_params=schema_params, |
| 58 | skip_default=self.skip_default, |
| 59 | ) |
| 60 | ) |
| 61 | action_func.__doc__ = tool.description |
| 62 | return FunctionTool.from_defaults( |
| 63 | action_func, |
| 64 | name=tool.slug, |
| 65 | description=tool.description, |
| 66 | ) |
| 67 | |
| 68 | def wrap_tools( |
| 69 | self, |
| 70 | tools: t.Sequence[Tool], |
| 71 | execute_tool: AgenticProviderExecuteFn, |
| 72 | ) -> t.List[FunctionTool]: |
| 73 | """ |
| 74 | Wrap tools into LlamaIndex FunctionTool objects. |
| 75 | """ |
| 76 | return [self.wrap_tool(tool, execute_tool) for tool in tools] |