Skip to content

Commit

Permalink
adding default Basemodel
Browse files Browse the repository at this point in the history
  • Loading branch information
Occupying-Mars committed Nov 27, 2024
1 parent 47a359e commit 019105f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
32 changes: 32 additions & 0 deletions docs/swarms/tools/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ def summarize_expenses(data: SummarizeExpenses) -> dict:
return {"total_expenses": total_expenses}
```

#### Using Default BaseModel

When initializing an Agent with tools but without specifying `list_base_models`, the system automatically uses a DefaultBaseModel:

```python
from pydantic import BaseModel, Field
from typing import Any

class DefaultBaseModel(BaseModel):
"""A default base model that accepts any data structure"""
data: Any = Field(default=None, description="Any data type")

class Config:
arbitrary_types_allowed = True
extra = "allow"

# Agent will use DefaultBaseModel automatically if no list_base_models provided
agent = Agent(
agent_name="Simple Agent",
tools=[your_tool_function], # DefaultBaseModel will be used
llm=your_llm_instance
)
```

⚠️ **Warning**: While the DefaultBaseModel provides flexibility, it's recommended to define your own specific Pydantic models for:
- Type safety and validation
- Clear documentation
- Predictable behavior
- Schema generation

For production use, define custom models like the examples above (CalculateTax, GenerateInvoice).

#### Using Functions Directly

Tools can also be defined directly as functions without using Pydantic models. This approach is suitable for simpler tasks where complex validation is not required.
Expand Down
28 changes: 27 additions & 1 deletion swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
execute_on_gpu,
execute_with_cpu_cores,
)
from pydantic import BaseModel
from pydantic import BaseModel, Field
from swarm_models.tiktoken_wrapper import TikTokenizer
from termcolor import colored

Expand Down Expand Up @@ -90,6 +90,14 @@ def exists(val):


# [FEAT][AGENT]
class DefaultBaseModel(BaseModel):
"""A default base model that accepts any data structure"""
data: Any = Field(default=None, description="Any data type")

class Config:
arbitrary_types_allowed = True
extra = "allow"

class Agent:
"""
Agent is the backbone to connect LLMs with tools and long term memory. Agent also provides the ability to
Expand Down Expand Up @@ -477,6 +485,24 @@ def __init__(
or exists(tool_schema)
):

# If no base models provided, use the default one
if exists(tools) or exists(tool_schema):
if list_base_models is None:
list_base_models = [DefaultBaseModel]
logger.warning(
colored(
"\nUsing default base model for tool schema validation. "
"This can lead to potential format mismatches or validation issues. "
"Consider defining your own Pydantic BaseModel(s) with proper field definitions "
"and validation rules for your specific use case.\n"
"Example:\n"
"class YourModel(BaseModel):\n"
" field1: str = Field(..., description='Description of field1')\n"
" field2: int = Field(..., description='Description of field2')\n",
"yellow"
)
)

self.tool_struct = BaseTool(
tools=tools,
base_models=list_base_models,
Expand Down

0 comments on commit 019105f

Please sign in to comment.