Accuracy with Teams

Example showing how to evaluate the accuracy of an Kern Team.

Create a Python file

1from typing import Optional
2
3from kern.agent import Agent
4from kern.eval.accuracy import AccuracyEval, AccuracyResult
5from kern.models.openai import OpenAIResponses
6from kern.team import Team
7
8# Setup a team with two members
9english_agent = Agent(
10 name="English Agent",
11 role="You only answer in English",
12 model=OpenAIResponses(id="gpt-5.2"),
13)
14spanish_agent = Agent(
15 name="Spanish Agent",
16 role="You can only answer in Spanish",
17 model=OpenAIResponses(id="gpt-5.2"),
18)
19
20multi_language_team = Team(
21 name="Multi Language Team",
22 model=OpenAIResponses(id="gpt-5.2"),
23 members=[english_agent, spanish_agent],
24 respond_directly=True,
25 markdown=True,
26 instructions=[
27 "You are a language router that directs questions to the appropriate language agent.",
28 "If the user asks in a language whose agent is not a team member, respond in English with:",
29 "'I can only answer in the following languages: English and Spanish.",
30 "Always check the language of the user's input before routing to an agent.",
31 ],
32)
33
34# Evaluate the accuracy of the Team's responses
35evaluation = AccuracyEval(
36 name="Multi Language Team",
37 model=OpenAIResponses(id="gpt-5.2"),
38 team=multi_language_team,
39 input="Comment allez-vous?",
40 expected_output="I can only answer in the following languages: English and Spanish.",
41 num_iterations=1,
42)
43
44result: Optional[AccuracyResult] = evaluation.run(print_results=True)
45assert result is not None and result.avg_score >= 8

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 openai kern-ai

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 accuracy_with_teams.py