Skip to content

Building AI agents, atomically

Some amendments

Koichi KAWAMURAb38987ffd5e8
diff --git a/atomic-agents/atomic_agents/agents/base_agent.py b/atomic-agents/atomic_agents/agents/base_agent.py
index 5b1a7f0..9947450 100644
--- a/atomic-agents/atomic_agents/agents/base_agent.py
+++ b/atomic-agents/atomic_agents/agents/base_agent.py
@@ -1,7 +1,6 @@
import instructor
from pydantic import BaseModel, Field
from typing import Optional, Type, Generator, AsyncGenerator, get_args
-import warnings
from atomic_agents.lib.components.agent_memory import AgentMemory
from atomic_agents.lib.components.system_prompt_generator import (
SystemPromptContextProviderBase,
@@ -123,18 +122,18 @@ class BaseAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
TI, _ = get_args(self.__orig_class__)
else:
TI = BaseAgentInputSchema
-
+
return TI
-
+
@property
def output_schema(self) -> Type[BaseIOSchema]:
if hasattr(self, "__orig_class__"):
_, TO = get_args(self.__orig_class__)
else:
TO = BaseAgentOutputSchema
-
- return TO
-
+
+ return TO
+
def _prepare_messages(self):
if self.system_role is None:
self.messages = []
@@ -239,10 +238,7 @@ class BaseAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
self._prepare_messages()
response = await self.client.chat.completions.create(
- model=self.model,
- messages=self.messages,
- response_model=self.output_schema,
- **self.model_api_parameters
+ model=self.model, messages=self.messages, response_model=self.output_schema, **self.model_api_parameters
)
self.memory.add_message("assistant", response)
diff --git a/atomic-agents/atomic_agents/lib/base/base_tool.py b/atomic-agents/atomic_agents/lib/base/base_tool.py
index b4ec922..271a53f 100644
--- a/atomic-agents/atomic_agents/lib/base/base_tool.py
+++ b/atomic-agents/atomic_agents/lib/base/base_tool.py
@@ -59,9 +59,9 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
TI, _ = get_args(self.__orig_class__)
else:
TI = BaseIOSchema
-
+
return TI
-
+
@property
def output_schema(self) -> Type[OutputSchema]:
"""
@@ -74,9 +74,9 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
_, TO = get_args(self.__orig_class__)
else:
TO = BaseIOSchema
-
+
return TO
-
+
@property
def tool_name(self) -> str:
"""
@@ -86,7 +86,7 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
str: The name of the tool.
"""
return self.config.title or self.input_schema.model_json_schema()["title"]
-
+
@property
def tool_description(self) -> str:
"""
diff --git a/atomic-agents/tests/lib/base/test_base_tool.py b/atomic-agents/tests/lib/base/test_base_tool.py
index 6ef7dfb..b0f619c 100644
--- a/atomic-agents/tests/lib/base/test_base_tool.py
+++ b/atomic-agents/tests/lib/base/test_base_tool.py
@@ -1,4 +1,3 @@
-import pytest
from pydantic import BaseModel
from atomic_agents.lib.base.base_tool import BaseToolConfig, BaseTool
from atomic_agents.agents.base_agent import BaseIOSchema
diff --git a/atomic-examples/deep-research/README.md b/atomic-examples/deep-research/README.md
index 9ce8a17..39a5d0d 100644
--- a/atomic-examples/deep-research/README.md
+++ b/atomic-examples/deep-research/README.md
@@ -23,6 +23,7 @@ This directory contains the Deep Research Agent example for the Atomic Agents pr
Create a `.env` file in the `deep-research` directory with:
```env
OPENAI_API_KEY=your_openai_api_key
+ SEARXNG_BASE_URL=http://localhost:8080
```
5. **Set up SearxNG:**
diff --git a/atomic-examples/deep-research/deep_research/agents/query_agent.py b/atomic-examples/deep-research/deep_research/agents/query_agent.py
index 40c6afa..3362f94 100644
--- a/atomic-examples/deep-research/deep_research/agents/query_agent.py
+++ b/atomic-examples/deep-research/deep_research/agents/query_agent.py
@@ -5,7 +5,7 @@ from pydantic import Field
from atomic_agents.agents.base_agent import BaseIOSchema, BaseAgent, BaseAgentConfig
from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator
-from deep_research.tools.searxng_search import SearxNGSearchTool
+from deep_research.tools.searxng_search import SearxNGSearchTool, SearxNGSearchToolInputSchema
class QueryAgentInputSchema(BaseIOSchema):
@@ -15,7 +15,7 @@ class QueryAgentInputSchema(BaseIOSchema):
num_queries: int = Field(..., description="The number of search queries to generate.")
-query_agent = BaseAgent[QueryAgentInputSchema, SearxNGSearchTool.input_schema.__class__](
+query_agent = BaseAgent[QueryAgentInputSchema, SearxNGSearchToolInputSchema](
BaseAgentConfig(
client=instructor.from_openai(openai.OpenAI(api_key=ChatConfig.api_key)),
model=ChatConfig.model,
diff --git a/atomic-examples/deep-research/deep_research/tools/webpage_scraper.py b/atomic-examples/deep-research/deep_research/tools/webpage_scraper.py
index 3a74e1f..6eb1357 100644
--- a/atomic-examples/deep-research/deep_research/tools/webpage_scraper.py
+++ b/atomic-examples/deep-research/deep_research/tools/webpage_scraper.py
@@ -1,15 +1,11 @@
-from time import sleep
-from typing import Optional, Dict, List, Any
-import asyncio
-from concurrent.futures import ThreadPoolExecutor
-import logging
-
+from typing import Optional, Dict
+import re
import requests
+from urllib.parse import urlparse
from bs4 import BeautifulSoup
from markdownify import markdownify
from pydantic import Field, HttpUrl
-import trafilatura
-
+from readability import Document
from atomic_agents.agents.base_agent import BaseIOSchema
from atomic_agents.lib.base.base_tool import BaseTool, BaseToolConfig
diff --git a/atomic-examples/quickstart/quickstart/6_asynchronous_processing_streaming.py b/atomic-examples/quickstart/quickstart/6_asynchronous_processing_streaming.py
index 2f013fa..f9597ba 100644
--- a/atomic-examples/quickstart/quickstart/6_asynchronous_processing_streaming.py
+++ b/atomic-examples/quickstart/quickstart/6_asynchronous_processing_streaming.py
@@ -55,11 +55,7 @@ sem = asyncio.Semaphore(2)
# Agent setup with specified configuration
agent = BaseAgent[BaseAgentInputSchema, PersonSchema](
- config=BaseAgentConfig(
- client=client,
- model="gpt-4o-mini",
- sysyem_prompt_generator=sysyem_prompt_generator
- )
+ config=BaseAgentConfig(client=client, model="gpt-4o-mini", sysyem_prompt_generator=sysyem_prompt_generator)
)
@@ -87,7 +83,7 @@ async def process(dataset: list[str]):
# Prompt the user for input with a styled prompt
return responses
+
if __name__ == "__main__":
responses = asyncio.run(process(dataset))
console.print(f"\n[bold green]Responses: {responses}[/bold green]")
-
diff --git a/atomic-examples/quickstart/quickstart/7_asynchronous_processing.py b/atomic-examples/quickstart/quickstart/7_asynchronous_processing.py
index 5f541aa..217df00 100644
--- a/atomic-examples/quickstart/quickstart/7_asynchronous_processing.py
+++ b/atomic-examples/quickstart/quickstart/7_asynchronous_processing.py
@@ -53,11 +53,7 @@ sem = asyncio.Semaphore(2)
# Agent setup with specified configuration
agent = BaseAgent[BaseAgentInputSchema, PersonSchema](
- config=BaseAgentConfig(
- client=client,
- model="gpt-4o-mini",
- sysyem_prompt_generator=sysyem_prompt_generator
- )
+ config=BaseAgentConfig(client=client, model="gpt-4o-mini", sysyem_prompt_generator=sysyem_prompt_generator)
)
diff --git a/atomic-examples/web-search-agent/web_search_agent/agents/query_agent.py b/atomic-examples/web-search-agent/web_search_agent/agents/query_agent.py
index 383d5ec..242c10d 100644
--- a/atomic-examples/web-search-agent/web_search_agent/agents/query_agent.py
+++ b/atomic-examples/web-search-agent/web_search_agent/agents/query_agent.py
@@ -33,13 +33,13 @@ query_agent = BaseAgent[QueryAgentInputSchema, QueryAgentOutputSchema](
steps=[
"Analyze the user's question to understand the core information need.",
"Generate multiple search queries that capture the question's essence from different angles.",
- "Ensure each query is optimized for search engines (compact, focused, and unambiguous)."
+ "Ensure each query is optimized for search engines (compact, focused, and unambiguous).",
],
output_instructions=[
"Generate 3-5 different search queries.",
"Do not include special search operators or syntax.",
- "Each query should be concise and focused on retrieving relevant information."
- ]
+ "Each query should be concise and focused on retrieving relevant information.",
+ ],
),
)
)
diff --git a/atomic-examples/web-search-agent/web_search_agent/main.py b/atomic-examples/web-search-agent/web_search_agent/main.py
index f484c68..b507f23 100644
--- a/atomic-examples/web-search-agent/web_search_agent/main.py
+++ b/atomic-examples/web-search-agent/web_search_agent/main.py
@@ -30,15 +30,20 @@ memory = AgentMemory()
# Initialize the SearxNGSearchTool
search_tool = SearxNGSearchTool(config=SearxNGSearchToolConfig(base_url=os.getenv("SEARXNG_BASE_URL"), max_results=5))
+
# Define input/output schemas for the main agent
class MainAgentInputSchema(BaseIOSchema):
"""Input schema for the main agent."""
+
chat_message: str = Field(..., description="Chat message from the user.")
+
class MainAgentOutputSchema(BaseIOSchema):
"""Output schema for the main agent."""
+
chat_message: str = Field(..., description="Response to the user's message.")
+
# Initialize the BaseAgent
agent = BaseAgent[MainAgentInputSchema, MainAgentOutputSchema](
config=BaseAgentConfig(
diff --git a/atomic-forge/tools/tavily_search/tool/tavily_search.py b/atomic-forge/tools/tavily_search/tool/tavily_search.py
index 2261266..bc92748 100644
--- a/atomic-forge/tools/tavily_search/tool/tavily_search.py
+++ b/atomic-forge/tools/tavily_search/tool/tavily_search.py
@@ -2,10 +2,8 @@ import os
from typing import List, Literal, Optional
import asyncio
from concurrent.futures import ThreadPoolExecutor
-import datetime
import aiohttp
-from tavily import TavilyClient
from pydantic import Field
from atomic_agents.agents.base_agent import BaseIOSchema
@@ -108,7 +106,6 @@ class TavilySearchTool(BaseTool[TavilySearchToolInputSchema, TavilySearchToolOut
)
data = await response.json()
results = data.get("results", [])
- answer = data.get("answer", "")
# Add query information to each result
for result in results: