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 Agent
2from kern.team.team import Team
3from kern.tools import tool
4from kern.tools.hackernews import HackerNewsTools
5
6
7@tool()
8def answer_from_known_questions(question: str) -> str:
9 """Answer a question from a list of known questions
10
11 Args:
12 question: The question to answer
13
14 Returns:
15 The answer to the question
16 """
17
18 # FAQ knowledge base
19 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 }
28
29 # Check if question is in FAQ
30 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."
34
35
36# Create news agent for fallback
37news_agent = Agent(
38 name="News Agent",
39 role="Search HackerNews for information",
40 tools=[HackerNewsTools()],
41 markdown=True,
42)
43
44# Create team with custom tool and agent members
45team = Team(name="Q & A team", members=[news_agent], tools=[answer_from_known_questions])
46
47# Test the team
48team.print_response("What is the capital of France?", stream=True)
49
50# Check if team has session state and display information
51print("\nTeam Session Info:")
52session = team.get_session()
53print(f" Session ID: {session.session_id}")
54print(f" Session State: {session.session_data['session_state']}")
55
56# Show team capabilities
57print("\nTeam Tools Available:")
58for t in team.tools:
59 print(f" - {t.name}: {t.description}")
60
61print("\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.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai

Export 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