From 1261ce513ff2b707c506c6ff65e326fc6e9627c7 Mon Sep 17 00:00:00 2001 From: lgesuellip <102637283+lgesuellip@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:34:07 -0300 Subject: [PATCH 1/4] Add doc structured tool (#1713) * Add doc structured tool * Fix example --------- Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- docs/concepts/tools.mdx | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/concepts/tools.mdx b/docs/concepts/tools.mdx index 9b9c6a32e..8abe0f4e6 100644 --- a/docs/concepts/tools.mdx +++ b/docs/concepts/tools.mdx @@ -172,6 +172,48 @@ def my_tool(question: str) -> str: return "Result from your custom tool" ``` +### Structured Tools + +The `StructuredTool` class wraps functions as tools, providing flexibility and validation while reducing boilerplate. It supports custom schemas and dynamic logic for seamless integration of complex functionalities. + +#### Example: +Using `StructuredTool.from_function`, you can wrap a function that interacts with an external API or system, providing a structured interface. This enables robust validation and consistent execution, making it easier to integrate complex functionalities into your applications as demonstrated in the following example: + +```python +from crewai.tools.structured_tool import CrewStructuredTool +from pydantic import BaseModel + +# Define the schema for the tool's input using Pydantic +class APICallInput(BaseModel): + endpoint: str + parameters: dict + +# Wrapper function to execute the API call +def tool_wrapper(*args, **kwargs): + # Here, you would typically call the API using the parameters + # For demonstration, we'll return a placeholder string + return f"Call the API at {kwargs['endpoint']} with parameters {kwargs['parameters']}" + +# Create and return the structured tool +def create_structured_tool(): + return CrewStructuredTool.from_function( + name='Wrapper API', + description="A tool to wrap API calls with structured input.", + args_schema=APICallInput, + func=tool_wrapper, + ) + +# Example usage +structured_tool = create_structured_tool() + +# Execute the tool with structured input +result = structured_tool._run(**{ + "endpoint": "https://example.com/api", + "parameters": {"key1": "value1", "key2": "value2"} +}) +print(result) # Output: Call the API at https://example.com/api with parameters {'key1': 'value1', 'key2': 'value2'} +``` + ### Custom Caching Mechanism From 8c90db04b5439bed4682b8575fe28db37deb7898 Mon Sep 17 00:00:00 2001 From: fuckqqcom <9391575+fuckqqcom@users.noreply.github.com> Date: Tue, 10 Dec 2024 00:37:54 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=5Fexecute=5Ftool=5Fand=5Fcheck=5Ffinality?= =?UTF-8?q?=20=E7=BB=93=E6=9E=9C=E7=BB=99=E5=9B=9E=E8=B0=83=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E8=BF=99=E6=A0=B7=E5=B0=B1=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=8F=90=E5=89=8D=E6=8B=BF=E5=88=B0=E7=BB=93=E6=9E=9C=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E5=8E=BB=E5=81=9A=E6=95=B0=E6=8D=AE=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=88=A4=E6=96=AD=E5=81=9A=E9=A2=84=E5=88=A4=20(#1716?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xiaohan Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- src/crewai/agents/crew_agent_executor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/crewai/agents/crew_agent_executor.py b/src/crewai/agents/crew_agent_executor.py index 649360e30..83ca06e41 100644 --- a/src/crewai/agents/crew_agent_executor.py +++ b/src/crewai/agents/crew_agent_executor.py @@ -143,6 +143,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin): tool_result = self._execute_tool_and_check_finality( formatted_answer ) + if self.step_callback: + self.step_callback(tool_result) + formatted_answer.text += f"\nObservation: {tool_result.result}" formatted_answer.result = tool_result.result if tool_result.result_as_answer: From 236e42d0bc4e1e79013e110c5f4502a54b338799 Mon Sep 17 00:00:00 2001 From: Tony Kipkemboi Date: Mon, 9 Dec 2024 11:40:01 -0500 Subject: [PATCH 3/4] format bullet points (#1734) Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- docs/concepts/knowledge.mdx | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/concepts/knowledge.mdx b/docs/concepts/knowledge.mdx index 16ac586d6..8af9b70a2 100644 --- a/docs/concepts/knowledge.mdx +++ b/docs/concepts/knowledge.mdx @@ -12,9 +12,11 @@ Knowledge in CrewAI is a powerful system that allows AI agents to access and uti Think of it as giving your agents a reference library they can consult while working. - Key benefits of using Knowledge: - Enhance agents with domain-specific - information - Support decisions with real-world data - Maintain context across - conversations - Ground responses in factual information + Key benefits of using Knowledge: + - Enhance agents with domain-specific information + - Support decisions with real-world data + - Maintain context across conversations + - Ground responses in factual information ## Supported Knowledge Sources @@ -23,10 +25,14 @@ CrewAI supports various types of knowledge sources out of the box: - - Raw strings - Text files (.txt) - PDF documents + - Raw strings + - Text files (.txt) + - PDF documents - - CSV files - Excel spreadsheets - JSON documents + - CSV files + - Excel spreadsheets + - JSON documents @@ -300,14 +306,14 @@ recent_news = SpaceNewsKnowledgeSource( - - Keep chunk sizes appropriate for your content type - Consider content - overlap for context preservation - Organize related information into - separate knowledge sources + - Keep chunk sizes appropriate for your content type + - Consider content overlap for context preservation + - Organize related information into separate knowledge sources - - Adjust chunk sizes based on content complexity - Configure appropriate - embedding models - Consider using local embedding providers for faster - processing + - Adjust chunk sizes based on content complexity + - Configure appropriate embedding models + - Consider using local embedding providers for faster processing From c4f7eaf2599c5709260d660d48e810aae7a8c124 Mon Sep 17 00:00:00 2001 From: Piotr Mardziel Date: Mon, 9 Dec 2024 08:51:12 -0800 Subject: [PATCH 4/4] Add missing @functools.wraps when wrapping functions and preserve wrapped class name in @CrewBase. (#1560) * Update annotations.py * Update utils.py * Update crew_base.py * Update utils.py * Update crew_base.py --------- Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- src/crewai/project/annotations.py | 2 ++ src/crewai/project/crew_base.py | 4 ++++ src/crewai/project/utils.py | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/crewai/project/annotations.py b/src/crewai/project/annotations.py index be4710b2b..bf0051c4d 100644 --- a/src/crewai/project/annotations.py +++ b/src/crewai/project/annotations.py @@ -66,6 +66,8 @@ def cache_handler(func): def crew(func) -> Callable[..., Crew]: + + @wraps(func) def wrapper(self, *args, **kwargs) -> Crew: instantiated_tasks = [] instantiated_agents = [] diff --git a/src/crewai/project/crew_base.py b/src/crewai/project/crew_base.py index e93452a6e..0b43882f2 100644 --- a/src/crewai/project/crew_base.py +++ b/src/crewai/project/crew_base.py @@ -213,4 +213,8 @@ def CrewBase(cls: T) -> T: callback_functions[callback]() for callback in callbacks ] + # Include base class (qual)name in the wrapper class (qual)name. + WrappedClass.__name__ = CrewBase.__name__ + "(" + cls.__name__ + ")" + WrappedClass.__qualname__ = CrewBase.__qualname__ + "(" + cls.__name__ + ")" + return cast(T, WrappedClass) diff --git a/src/crewai/project/utils.py b/src/crewai/project/utils.py index be3f757d9..2e95c755d 100644 --- a/src/crewai/project/utils.py +++ b/src/crewai/project/utils.py @@ -1,11 +1,13 @@ +from functools import wraps + def memoize(func): cache = {} + @wraps(func) def memoized_func(*args, **kwargs): key = (args, tuple(kwargs.items())) if key not in cache: cache[key] = func(*args, **kwargs) return cache[key] - memoized_func.__dict__.update(func.__dict__) return memoized_func