Team with Custom Tools
This example demonstrates how to create a team with custom tools, using custom tools alongside agent tools to answer questions from a knowledge base and fall back to web search when needed.
Code
1from kern.agent import Agent2from kern.team.team import Team3from kern.tools import tool4from kern.tools.hackernews import HackerNewsTools567@tool()8def answer_from_known_questions(question: str) -> str:9 """Answer a question from a list of known questions1011 Args:12 question: The question to answer1314 Returns:15 The answer to the question16 """1718 # FAQ knowledge base19 faq = {20 "What is the capital of France?": "Paris",21 "What is the capital of Germany?": "Berlin",22 "What is the capital of Italy?": "Rome",23 "What is the capital of Spain?": "Madrid",24 "What is the capital of Portugal?": "Lisbon",25 "What is the capital of Greece?": "Athens",26 "What is the capital of Turkey?": "Ankara",27 }2829 # Check if question is in FAQ30 if question in faq:31 return f"From my knowledge base: {faq[question]}"32 else:33 return "I don't have that information in my knowledge base. Try asking the news agent."343536# Create news agent for fallback37news_agent = Agent(38 name="News Agent",39 role="Search HackerNews for information",40 tools=[HackerNewsTools()],41 markdown=True,42)4344# Create team with custom tool and agent members45team = Team(name="Q & A team", members=[news_agent], tools=[answer_from_known_questions])4647# Test the team48team.print_response("What is the capital of France?", stream=True)4950# Check if team has session state and display information51print("\nTeam Session Info:")52session = team.get_session()53print(f" Session ID: {session.session_id}")54print(f" Session State: {session.session_data['session_state']}")5556# Show team capabilities57print("\nTeam Tools Available:")58for t in team.tools:59 print(f" - {t.name}: {t.description}")6061print("\nTeam Members:")62for member in team.members:63 print(f" - {member.name}: {member.role}")Usage
Create a Python file
Create team_with_custom_tools.py with the code above.
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openaiExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run Team
1python team_with_custom_tools.py