forked from strands-agents/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_function_tools.py
More file actions
55 lines (41 loc) · 1.5 KB
/
test_function_tools.py
File metadata and controls
55 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
"""
Test script for function-based tools
"""
import logging
from typing import Optional
from strands import Agent, tool
logging.getLogger("strands").setLevel(logging.DEBUG)
logging.basicConfig(format="%(levelname)s | %(name)s | %(message)s", handlers=[logging.StreamHandler()])
@tool
def word_counter(text: str) -> str:
"""
Count words in text.
Args:
text: Text to analyze
"""
count = len(text.split())
return f"Word count: {count}"
@tool(name="count_chars", description="Count characters in text")
def count_chars(text: str, include_spaces: Optional[bool] = True) -> str:
"""
Count characters in text.
Args:
text: Text to analyze
include_spaces: Whether to include spaces in the count
"""
if not include_spaces:
text = text.replace(" ", "")
return f"Character count: {len(text)}"
# Initialize agent with function tools
agent = Agent(tools=[word_counter, count_chars])
print("\n===== Testing Direct Tool Access =====")
# Use the tools directly
word_result = agent.tool.word_counter(text="Hello world, this is a test")
print(f"\nWord counter result: {word_result}")
char_result = agent.tool.count_chars(text="Hello world!", include_spaces=False)
print(f"\nCharacter counter result: {char_result}")
print("\n===== Testing Natural Language Access =====")
# Use through natural language
nl_result = agent("Count the words in this sentence: 'The quick brown fox jumps over the lazy dog'")
print(f"\nNL Result: {nl_result}")