Now we'll give Fred the ability to search Wikipedia! The SignalWire SDK's skills system makes this incredibly easy.
Skills are modular capabilities that extend your agent's functionality. Think of them as plugins that add specific features.
A skill is:
The SDK includes several pre-built skills:
wikipedia_search
- Search Wikipedia articlesdatetime
- Get current date/timemath
- Perform calculationsweb_search
- Search Google (requires API key)weather_api
- Get weather informationLet's add Wikipedia search to Fred with just one line of code!
Add this to Fred's __init__
method, after the global data:
# Add the Wikipedia search skill
self.add_skill("wikipedia_search")
That's it! Fred can now search Wikipedia. But let's customize it for better results.
Replace the basic addition with this enhanced version:
# Add the Wikipedia search skill with custom configuration
self.add_skill("wikipedia_search", {
"num_results": 2, # Get up to 2 articles for broader coverage
"no_results_message": "Oh, I couldn't find anything about '{query}' on Wikipedia. Maybe try different keywords or let me know if you meant something else!",
"swaig_fields": {
"fillers": {
"en-US": [
"Let me look that up on Wikipedia for you...",
"Searching Wikipedia for that information...",
"One moment, checking Wikipedia...",
"Let me find that in the encyclopedia..."
]
}
}
})
Let's understand each configuration option:
num_results
(default: 1)
- Number of Wikipedia articles to return
- More results = broader coverage but longer responses
- Fred uses 2 for balance
no_results_message
- Custom message when no articles are found
- Use {query}
as a placeholder for the search term
- Should match your agent's personality
swaig_fields
- Additional SWAIG function configuration
- fillers
: Phrases spoken while searching
- Enhances conversation flow
# Minimal configuration
self.add_skill("wikipedia_search")
# Academic assistant
self.add_skill("wikipedia_search", {
"num_results": 3,
"no_results_message": "No Wikipedia entries found for '{query}'. Please verify the spelling or try related terms."
})
# Casual helper
self.add_skill("wikipedia_search", {
"num_results": 1,
"no_results_message": "Hmm, Wikipedia doesn't have info on '{query}'. Got another topic?",
"swaig_fields": {
"fillers": {
"en-US": ["Let me check...", "Looking that up..."]
}
}
})
Understanding the magic behind add_skill()
:
Skill Loading
python
# The SDK dynamically imports the skill module
from signalwire_agents.skills.wikipedia_search.skill import WikipediaSearchSkill
Skill Initialization
python
# Creates skill instance with your configuration
skill = WikipediaSearchSkill(agent=self, params=config)
Tool Registration
python
# The skill registers its functions with your agent
self.define_tool(
name="search_wiki",
description="Search Wikipedia for information",
parameters={"query": {"type": "string", "description": "Search term"}},
handler=skill._search_wiki_handler
)
After adding the skill, Fred gains the search_wiki
function:
Function Signature:
search_wiki(query: string) -> string
What it does:
Example calls (by the AI):
search_wiki("Albert Einstein")
search_wiki("quantum physics")
search_wiki("Great Wall of China")
Let's add some test code to verify the skill works:
def main():
"""Run Fred the Wiki Bot"""
print("=" * 60)
print("π€ Fred - The Wikipedia Knowledge Bot")
print("=" * 60)
print()
print("Fred is a friendly assistant who loves searching Wikipedia!")
print("He can help you learn about almost any topic.")
print()
print("Example questions you can ask Fred:")
print(" β’ 'Tell me about Albert Einstein'")
print(" β’ 'What is quantum physics?'")
print(" β’ 'Who was Marie Curie?'")
print(" β’ 'Search for information about the solar system'")
print()
# Create Fred
fred = FredTheWikiBot()
# Show loaded skills
loaded_skills = fred.list_skills()
print(f"Fred's capabilities: {', '.join(loaded_skills)}")
print()
# The skill automatically adds tools to the agent
# You can verify this by checking registered functions
print("Wikipedia search is ready!")
print("Fred can now search Wikipedia for any topic.")
if __name__ == "__main__":
main()
Run it:
python fred.py
Expected output:
============================================================
π€ Fred - The Wikipedia Knowledge Bot
============================================================
Fred is a friendly assistant who loves searching Wikipedia!
He can help you learn about almost any topic.
Example questions you can ask Fred:
β’ 'Tell me about Albert Einstein'
β’ 'What is quantum physics?'
β’ 'Who was Marie Curie?'
β’ 'Search for information about the solar system'
Fred's capabilities: wikipedia_search
Wikipedia search is ready!
Fred can now search Wikipedia for any topic.
When a user asks Fred about a topic:
User says: "Tell me about Albert Einstein"
Fred's AI recognizes the request needs Wikipedia
Fred calls: search_wiki("Albert Einstein")
Wikipedia returns article summary
Fred responds with the information conversationally
User: "Hi there!"
Fred: "Hello! I'm Fred, your friendly Wikipedia assistant! I love helping people learn new things. What would you like to know about today?"
User: "Tell me about black holes"
Fred: [filler] "Let me look that up on Wikipedia for you..."
Fred: "Fascinating topic! According to Wikipedia, a black hole is a region of spacetime where gravity is so strong that nothingβno particles or even electromagnetic radiation such as lightβcan escape from it..."
User: "That's interesting! Who discovered them?"
Fred: [filler] "Searching Wikipedia for that information..."
Fred: "Great question! The concept of black holes has a rich history..."
With Skills:
# One line!
self.add_skill("wikipedia_search")
Without Skills:
# You'd need to:
# 1. Import Wikipedia API library
# 2. Handle API calls
# 3. Parse responses
# 4. Format results
# 5. Handle errors
# 6. Register SWAIG function
# 7. Implement handler
# ... 50+ lines of code
Fred can now search Wikipedia! But let's make him more fun by adding a custom function for sharing Wikipedia facts.
β‘οΈ Continue to Lesson 5: Creating Custom Functions
Skill Checklist:
What Fred Can Do Now:
β Previous: Basic Agent | Back to Overview | Next: Custom Functions β