Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add list_tools method to Agent with logging & test cases & upda… #167

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,29 @@ robot_agent = pirate_agent.clone(
instructions="Write like a robot",
)
```

## Additional Methods

### `get_system_prompt`

The `get_system_prompt` method retrieves the system prompt for the agent. This can be a static string or dynamically generated instructions.

```python
system_prompt = await agent.get_system_prompt(context)
print("System Prompt:", system_prompt)
```

- **Parameters**:
- `run_context`: The context in which the agent is running.
- **Returns**: A string representing the system prompt.

### `list_tools`

The `list_tools` method provides a list of all tools the agent has access to. This is useful for debugging and monitoring the agent's capabilities.

```python
tool_names = agent.list_tools()
print("Tools:", tool_names)
```

- **Returns**: A list of tool names or descriptions. Logs a warning if no tools are available.
12 changes: 12 additions & 0 deletions src/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,15 @@ async def get_system_prompt(self, run_context: RunContextWrapper[TContext]) -> s
logger.error(f"Instructions must be a string or a function, got {self.instructions}")

return None

def list_tools(self) -> list[str]:
"""Get a list of all tools the agent has access to.

Returns:
A list of tool names. Logs a warning if no tools are available.
"""
if not self.tools:
logger.warning(f"Agent '{self.name}' has no tools available.")
return []

return [tool.name for tool in self.tools]
25 changes: 24 additions & 1 deletion tests/test_agent_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from pydantic import BaseModel

from agents import Agent, Handoff, RunContextWrapper, Runner, handoff
from agents import Agent, Handoff, RunContextWrapper, Runner, handoff, FunctionTool


@pytest.mark.asyncio
Expand All @@ -26,6 +26,29 @@ async def async_instructions(agent: Agent[None], context: RunContextWrapper[None
agent = agent.clone(instructions=async_instructions)
assert await agent.get_system_prompt(context) == "async_123"

def test_list_tools():
# Create some mock tools using FunctionTool
tool1 = FunctionTool(
name="Tool1",
description="A mock tool for testing",
params_json_schema={},
on_invoke_tool=lambda ctx, input: "output"
)
tool2 = FunctionTool(
name="Tool2",
description="Another mock tool for testing",
params_json_schema={},
on_invoke_tool=lambda ctx, input: "output"
)

# Initialize the agent with these tools
agent = Agent(name="TestAgent", tools=[tool1, tool2])

# Get the list of tool names
tool_names = agent.list_tools()

# Assert that the tool names are as expected
assert tool_names == ["Tool1", "Tool2"]

@pytest.mark.asyncio
async def test_handoff_with_agents():
Expand Down