Building AI agents, atomically
style: apply black formatting
KennyVaneetvelde97704457661e
diff --git a/atomic-agents/atomic_agents/agents/atomic_agent.py b/atomic-agents/atomic_agents/agents/atomic_agent.pyindex f32acb2..b1bdb1b 100644--- a/atomic-agents/atomic_agents/agents/atomic_agent.py+++ b/atomic-agents/atomic_agents/agents/atomic_agent.py@@ -261,7 +261,7 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:Optional[List[Dict[str, Any]]]: Tools definition for TOOLS mode, or None for JSON modes."""from instructor.processing.schema import generate_openai_schema-+# Only return tools for TOOLS-based modestools_modes = {Mode.TOOLS, Mode.TOOLS_STRICT, Mode.PARALLEL_TOOLS}if self.mode in tools_modes:@@ -283,16 +283,18 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:str: JSON schema string formatted as Instructor does."""from textwrap import dedent-+schema = self.output_schema.model_json_schema()- return dedent(f"""+ return dedent(+ f"""As a genius expert, your task is to understand the content and providethe parsed objects in json that match the following json_schema:{json.dumps(schema, indent=2, ensure_ascii=False)}Make sure to return an instance of the JSON, not the schema itself- """).strip()+ """+ ).strip()def _serialize_history_for_token_count(self) -> List[Dict[str, Any]]:"""@@ -330,9 +332,7 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:f"Failed to serialize {media_type} for token counting: {e}. "f"Using placeholder for estimation.")- serialized_content.append(- {"type": "text", "text": f"[{media_type.lower()} content]"}- )+ serialized_content.append({"type": "text", "text": f"[{media_type.lower()} content]"})else:# Unknown type - convert to stringserialized_content.append({"type": "text", "text": str(item)})diff --git a/atomic-agents/atomic_agents/utils/token_counter.py b/atomic-agents/atomic_agents/utils/token_counter.pyindex 764c135..1427f82 100644--- a/atomic-agents/atomic_agents/utils/token_counter.py+++ b/atomic-agents/atomic_agents/utils/token_counter.py@@ -108,10 +108,7 @@ class TokenCounter:return token_counter(model=model, messages=messages, tools=tools)return token_counter(model=model, messages=messages)except ImportError as e:- raise ImportError(- "litellm is required for token counting. "- "Install it with: pip install litellm"- ) from e+ raise ImportError("litellm is required for token counting. " "Install it with: pip install litellm") from eexcept Exception as e:raise TokenCountError(f"Failed to count tokens for model '{model}': {e}") from e@@ -152,10 +149,7 @@ class TokenCounter:try:from litellm import get_max_tokensexcept ImportError as e:- raise ImportError(- "litellm is required for token counting. "- "Install it with: pip install litellm"- ) from e+ raise ImportError("litellm is required for token counting. " "Install it with: pip install litellm") from etry:return get_max_tokens(model)@@ -193,12 +187,8 @@ class TokenCounter:if tools:# To count just the tools overhead, we count empty messages with tools# and subtract the base overhead- empty_with_tools = self.count_messages(- model, [{"role": "user", "content": ""}], tools=tools- )- empty_without_tools = self.count_messages(- model, [{"role": "user", "content": ""}]- )+ empty_with_tools = self.count_messages(model, [{"role": "user", "content": ""}], tools=tools)+ empty_without_tools = self.count_messages(model, [{"role": "user", "content": ""}])tools_tokens = empty_with_tools - empty_without_toolstotal_tokens = system_tokens + history_tokens + tools_tokensdiff --git a/atomic-agents/tests/agents/test_atomic_agent.py b/atomic-agents/tests/agents/test_atomic_agent.pyindex 14688e8..e5ceb4f 100644--- a/atomic-agents/tests/agents/test_atomic_agent.py+++ b/atomic-agents/tests/agents/test_atomic_agent.py@@ -803,7 +803,9 @@ def test_get_context_token_count_basic(mock_get_token_counter, mock_instructor,@patch("atomic_agents.agents.atomic_agent.get_token_counter")-def test_get_context_token_count_includes_schema_overhead(mock_get_token_counter, mock_instructor, mock_history, mock_system_prompt_generator):+def test_get_context_token_count_includes_schema_overhead(+ mock_get_token_counter, mock_instructor, mock_history, mock_system_prompt_generator+):"""Test that token counting includes the output schema overhead for JSON mode."""from instructor import Mode@@ -868,7 +870,9 @@ def test_get_context_token_count_no_system_prompt(mock_get_token_counter, mock_i@patch("atomic_agents.agents.atomic_agent.get_token_counter")-def test_get_context_token_count_uses_configured_model(mock_get_token_counter, mock_instructor, mock_history, mock_system_prompt_generator):+def test_get_context_token_count_uses_configured_model(+ mock_get_token_counter, mock_instructor, mock_history, mock_system_prompt_generator+):"""Test that token counting uses the agent's configured model."""mock_history.get_history.return_value = []diff --git a/atomic-agents/tests/utils/test_token_counter.py b/atomic-agents/tests/utils/test_token_counter.pyindex 63c668d..da2f706 100644--- a/atomic-agents/tests/utils/test_token_counter.py+++ b/atomic-agents/tests/utils/test_token_counter.py@@ -112,9 +112,7 @@ class TestTokenCounter:assert result == 5# Should wrap text in a message- mock_token_counter.assert_called_once_with(- model="gpt-4", messages=[{"role": "user", "content": "Hello world"}]- )+ mock_token_counter.assert_called_once_with(model="gpt-4", messages=[{"role": "user", "content": "Hello world"}])@patch("litellm.get_max_tokens")def test_get_max_tokens(self, mock_get_max_tokens):@@ -145,9 +143,7 @@ class TestTokenCounter:result = counter.count_messages("gpt-4", messages, tools=tools)assert result == 150- mock_token_counter.assert_called_once_with(- model="gpt-4", messages=messages, tools=tools- )+ mock_token_counter.assert_called_once_with(model="gpt-4", messages=messages, tools=tools)@patch("litellm.token_counter")def test_count_messages_raises_token_count_error(self, mock_token_counter):@@ -267,12 +263,9 @@ class TestTokenCounter:# Verify all models were calledassert mock_token_counter.call_count == len(models)-@patch("litellm.get_max_tokens")@patch("litellm.token_counter")- def test_count_context_division_by_zero_prevention(- self, mock_token_counter, mock_get_max_tokens- ):+ def test_count_context_division_by_zero_prevention(self, mock_token_counter, mock_get_max_tokens):"""Test that division by zero is prevented when max_tokens is 0."""mock_token_counter.side_effect = [20, 30]mock_get_max_tokens.return_value = 0 # Edge case: max_tokens = 0