From b0d545992af3780530f7a24131b8c7299b69ab97 Mon Sep 17 00:00:00 2001 From: Brandon Hancock Date: Tue, 31 Dec 2024 11:19:48 -0500 Subject: [PATCH] Suppressed userWarnings from litellm pydantic issues --- pyproject.toml | 2 +- src/crewai/crew.py | 30 +++++++++------ src/crewai/llm.py | 8 ++-- src/crewai/utilities/internal_instructor.py | 8 ++-- .../utilities/token_counter_callback.py | 20 ++++++---- uv.lock | 38 ++++++++++++------- 6 files changed, 66 insertions(+), 40 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bcc00a0d9..10d7ea62d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ dependencies = [ # Core Dependencies "pydantic>=2.4.2", "openai>=1.13.3", - "litellm>=1.44.22", + "litellm>=1.56.4", "instructor>=1.3.3", # Text Processing diff --git a/src/crewai/crew.py b/src/crewai/crew.py index d488783ea..c01a89280 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -726,11 +726,7 @@ class Crew(BaseModel): # Determine which tools to use - task tools take precedence over agent tools tools_for_task = task.tools or agent_to_use.tools or [] - tools_for_task = self._prepare_tools( - agent_to_use, - task, - tools_for_task - ) + tools_for_task = self._prepare_tools(agent_to_use, task, tools_for_task) self._log_task_start(task, agent_to_use.role) @@ -797,14 +793,18 @@ class Crew(BaseModel): return skipped_task_output return None - def _prepare_tools(self, agent: BaseAgent, task: Task, tools: List[Tool]) -> List[Tool]: + def _prepare_tools( + self, agent: BaseAgent, task: Task, tools: List[Tool] + ) -> List[Tool]: # Add delegation tools if agent allows delegation if agent.allow_delegation: if self.process == Process.hierarchical: if self.manager_agent: tools = self._update_manager_tools(task, tools) else: - raise ValueError("Manager agent is required for hierarchical process.") + raise ValueError( + "Manager agent is required for hierarchical process." + ) elif agent and agent.allow_delegation: tools = self._add_delegation_tools(task, tools) @@ -823,7 +823,9 @@ class Crew(BaseModel): return self.manager_agent return task.agent - def _merge_tools(self, existing_tools: List[Tool], new_tools: List[Tool]) -> List[Tool]: + def _merge_tools( + self, existing_tools: List[Tool], new_tools: List[Tool] + ) -> List[Tool]: """Merge new tools into existing tools list, avoiding duplicates by tool name.""" if not new_tools: return existing_tools @@ -839,7 +841,9 @@ class Crew(BaseModel): return tools - def _inject_delegation_tools(self, tools: List[Tool], task_agent: BaseAgent, agents: List[BaseAgent]): + def _inject_delegation_tools( + self, tools: List[Tool], task_agent: BaseAgent, agents: List[BaseAgent] + ): delegation_tools = task_agent.get_delegation_tools(agents) return self._merge_tools(tools, delegation_tools) @@ -856,7 +860,9 @@ class Crew(BaseModel): if len(self.agents) > 1 and len(agents_for_delegation) > 0 and task.agent: if not tools: tools = [] - tools = self._inject_delegation_tools(tools, task.agent, agents_for_delegation) + tools = self._inject_delegation_tools( + tools, task.agent, agents_for_delegation + ) return tools def _log_task_start(self, task: Task, role: str = "None"): @@ -870,7 +876,9 @@ class Crew(BaseModel): if task.agent: tools = self._inject_delegation_tools(tools, task.agent, [task.agent]) else: - tools = self._inject_delegation_tools(tools, self.manager_agent, self.agents) + tools = self._inject_delegation_tools( + tools, self.manager_agent, self.agents + ) return tools def _get_context(self, task: Task, task_outputs: List[TaskOutput]): diff --git a/src/crewai/llm.py b/src/crewai/llm.py index 5d6a0ccf5..bdac7080a 100644 --- a/src/crewai/llm.py +++ b/src/crewai/llm.py @@ -6,8 +6,10 @@ import warnings from contextlib import contextmanager from typing import Any, Dict, List, Optional, Union -import litellm -from litellm import get_supported_openai_params +with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + import litellm + from litellm import get_supported_openai_params from crewai.utilities.exceptions.context_window_exceeding_exception import ( LLMContextLengthExceededException, @@ -138,7 +140,7 @@ class LLM: self.kwargs = kwargs litellm.drop_params = True - litellm.set_verbose = False + self.set_callbacks(callbacks) self.set_env_callbacks() diff --git a/src/crewai/utilities/internal_instructor.py b/src/crewai/utilities/internal_instructor.py index 13fe5a19f..a3206ba15 100644 --- a/src/crewai/utilities/internal_instructor.py +++ b/src/crewai/utilities/internal_instructor.py @@ -1,3 +1,4 @@ +import warnings from typing import Any, Optional, Type @@ -25,9 +26,10 @@ class InternalInstructor: if self.agent and not self.llm: self.llm = self.agent.function_calling_llm or self.agent.llm - # Lazy import - import instructor - from litellm import completion + with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + import instructor + from litellm import completion self._client = instructor.from_litellm( completion, diff --git a/src/crewai/utilities/token_counter_callback.py b/src/crewai/utilities/token_counter_callback.py index 06ad15022..46c7c68f9 100644 --- a/src/crewai/utilities/token_counter_callback.py +++ b/src/crewai/utilities/token_counter_callback.py @@ -1,3 +1,5 @@ +import warnings + from litellm.integrations.custom_logger import CustomLogger from litellm.types.utils import Usage @@ -12,11 +14,13 @@ class TokenCalcHandler(CustomLogger): if self.token_cost_process is None: return - usage: Usage = response_obj["usage"] - self.token_cost_process.sum_successful_requests(1) - self.token_cost_process.sum_prompt_tokens(usage.prompt_tokens) - self.token_cost_process.sum_completion_tokens(usage.completion_tokens) - if usage.prompt_tokens_details: - self.token_cost_process.sum_cached_prompt_tokens( - usage.prompt_tokens_details.cached_tokens - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + usage: Usage = response_obj["usage"] + self.token_cost_process.sum_successful_requests(1) + self.token_cost_process.sum_prompt_tokens(usage.prompt_tokens) + self.token_cost_process.sum_completion_tokens(usage.completion_tokens) + if usage.prompt_tokens_details: + self.token_cost_process.sum_cached_prompt_tokens( + usage.prompt_tokens_details.cached_tokens + ) diff --git a/uv.lock b/uv.lock index c37a1fa4e..dad7b150e 100644 --- a/uv.lock +++ b/uv.lock @@ -1,10 +1,18 @@ version = 1 requires-python = ">=3.10, <3.13" resolution-markers = [ - "python_full_version < '3.11'", - "python_full_version == '3.11.*'", - "python_full_version >= '3.12' and python_full_version < '3.12.4'", - "python_full_version >= '3.12.4'", + "python_full_version < '3.11' and platform_system == 'Darwin'", + "python_full_version < '3.11' and platform_machine == 'aarch64' and platform_system == 'Linux'", + "(python_full_version < '3.11' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version < '3.11' and platform_system != 'Darwin' and platform_system != 'Linux')", + "python_full_version == '3.11.*' and platform_system == 'Darwin'", + "python_full_version == '3.11.*' and platform_machine == 'aarch64' and platform_system == 'Linux'", + "(python_full_version == '3.11.*' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version == '3.11.*' and platform_system != 'Darwin' and platform_system != 'Linux')", + "python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_system == 'Darwin'", + "python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_machine == 'aarch64' and platform_system == 'Linux'", + "(python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_system != 'Darwin' and platform_system != 'Linux')", + "python_full_version >= '3.12.4' and platform_system == 'Darwin'", + "python_full_version >= '3.12.4' and platform_machine == 'aarch64' and platform_system == 'Linux'", + "(python_full_version >= '3.12.4' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version >= '3.12.4' and platform_system != 'Darwin' and platform_system != 'Linux')", ] [[package]] @@ -620,6 +628,9 @@ agentops = [ docling = [ { name = "docling" }, ] +embeddings = [ + { name = "tiktoken" }, +] fastembed = [ { name = "fastembed" }, ] @@ -642,7 +653,6 @@ tools = [ [package.dev-dependencies] dev = [ { name = "cairosvg" }, - { name = "crewai-tools" }, { name = "mkdocs" }, { name = "mkdocs-material" }, { name = "mkdocs-material-extensions" }, @@ -673,7 +683,7 @@ requires-dist = [ { name = "instructor", specifier = ">=1.3.3" }, { name = "json-repair", specifier = ">=0.25.2" }, { name = "jsonref", specifier = ">=1.1.0" }, - { name = "litellm", specifier = ">=1.44.22" }, + { name = "litellm", specifier = ">=1.56.4" }, { name = "mem0ai", marker = "extra == 'mem0'", specifier = ">=0.1.29" }, { name = "openai", specifier = ">=1.13.3" }, { name = "openpyxl", specifier = ">=3.1.5" }, @@ -688,6 +698,7 @@ requires-dist = [ { name = "python-dotenv", specifier = ">=1.0.0" }, { name = "pyvis", specifier = ">=0.3.2" }, { name = "regex", specifier = ">=2024.9.11" }, + { name = "tiktoken", marker = "extra == 'embeddings'", specifier = "~=0.7.0" }, { name = "tomli", specifier = ">=2.0.2" }, { name = "tomli-w", specifier = ">=1.1.0" }, { name = "uv", specifier = ">=0.4.25" }, @@ -696,7 +707,6 @@ requires-dist = [ [package.metadata.requires-dev] dev = [ { name = "cairosvg", specifier = ">=2.7.1" }, - { name = "crewai-tools", specifier = ">=0.17.0" }, { name = "mkdocs", specifier = ">=1.4.3" }, { name = "mkdocs-material", specifier = ">=9.5.7" }, { name = "mkdocs-material-extensions", specifier = ">=1.3.1" }, @@ -2219,24 +2229,24 @@ wheels = [ [[package]] name = "litellm" -version = "1.50.2" +version = "1.56.4" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "aiohttp" }, { name = "click" }, + { name = "httpx" }, { name = "importlib-metadata" }, { name = "jinja2" }, { name = "jsonschema" }, { name = "openai" }, { name = "pydantic" }, { name = "python-dotenv" }, - { name = "requests" }, { name = "tiktoken" }, { name = "tokenizers" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/a7/45/4d54617b267a96f1f7c17c0010ea1aba20e30a3672b873fe92a6001e5952/litellm-1.50.2.tar.gz", hash = "sha256:b244c9a0e069cc626b85fb9f5cc252114aaff1225500da30ce0940f841aef8ea", size = 6096949 } +sdist = { url = "https://files.pythonhosted.org/packages/83/ea/2c51d16c244a64dd3f0bdb1757aef798cf943b92e5695da04e3e42ba09e0/litellm-1.56.4.tar.gz", hash = "sha256:2808ca21878d200f7676a3d11e5bf2b5e3349ae504628f279cd7297c7dbd2038", size = 6284983 } wheels = [ - { url = "https://files.pythonhosted.org/packages/22/f3/89a4d65d1b9286eb5ac6a6e92dd93523d92f3142a832e60c00d5cad64176/litellm-1.50.2-py3-none-any.whl", hash = "sha256:99cac60c78037946ab809b7cfbbadad53507bb2db8ae39391b4be215a0869fdd", size = 6318265 }, + { url = "https://files.pythonhosted.org/packages/8f/25/2fd7b28a270b2963e8fa0ecf6aab4db47c54d932cc5aac8bc87e7ebc3755/litellm-1.56.4-py3-none-any.whl", hash = "sha256:699a8db46f7de045069a77c435e13244b5fdaf5df1c8cb5e6ad675ef7e104ccd", size = 6564370 }, ] [[package]] @@ -3030,7 +3040,7 @@ wheels = [ [[package]] name = "openai" -version = "1.52.1" +version = "1.58.1" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, @@ -3042,9 +3052,9 @@ dependencies = [ { name = "tqdm" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/80/ac/54c76352d493866637756b7c0ecec44f0b5bafb8fe753d98472cf6cfe4ce/openai-1.52.1.tar.gz", hash = "sha256:383b96c7e937cbec23cad5bf5718085381e4313ca33c5c5896b54f8e1b19d144", size = 310069 } +sdist = { url = "https://files.pythonhosted.org/packages/27/3c/b1ecce430ed56fa3ac1b0676966d3250aab9c70a408232b71e419ea62148/openai-1.58.1.tar.gz", hash = "sha256:f5a035fd01e141fc743f4b0e02c41ca49be8fab0866d3b67f5f29b4f4d3c0973", size = 343411 } wheels = [ - { url = "https://files.pythonhosted.org/packages/ad/31/28a83e124e9f9dd04c83b5aeb6f8b1770f45addde4dd3d34d9a9091590ad/openai-1.52.1-py3-none-any.whl", hash = "sha256:f23e83df5ba04ee0e82c8562571e8cb596cd88f9a84ab783e6c6259e5ffbfb4a", size = 386945 }, + { url = "https://files.pythonhosted.org/packages/8e/5a/d22cd07f1a99b9e8b3c92ee0c1959188db4318828a3d88c9daac120bdd69/openai-1.58.1-py3-none-any.whl", hash = "sha256:e2910b1170a6b7f88ef491ac3a42c387f08bd3db533411f7ee391d166571d63c", size = 454279 }, ] [[package]]