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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from crewai import Agent, Task, Crew, Process, LLM
|
| 3 |
+
from crewai.tools import tool
|
| 4 |
+
|
| 5 |
+
from probability_engine import elo_probability, monte_carlo_group
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
os.environ["GROQ_API_KEY"] = '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'
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
| 10 |
+
os.environ["OPENAI_BASE_URL"] = "https://openrouter.ai/api/v1"
|
| 11 |
+
|
| 12 |
+
@tool
|
| 13 |
+
def match_probability(teamA: str, teamB: str):
|
| 14 |
+
"""
|
| 15 |
+
Simulate match probability
|
| 16 |
+
Returns probability of a team winning.
|
| 17 |
+
"""
|
| 18 |
+
return elo_probability(teamA, teamB)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@tool
|
| 22 |
+
def simulate_group_stage(group: str):
|
| 23 |
+
"""
|
| 24 |
+
Simulate a tournament group using Monte Carlo simulations.
|
| 25 |
+
Returns probability of teams qualifying.
|
| 26 |
+
"""
|
| 27 |
+
return monte_carlo_group(group)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
llm = LLM(
|
| 31 |
+
# model="openrouter/qwen/qwen-2.5-14b-instruct",
|
| 32 |
+
model = "openrouter/mistralai/mistral-7b-instruct",
|
| 33 |
+
temperature=0.2
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
statistician = Agent(
|
| 38 |
+
role="Cricket Statistician",
|
| 39 |
+
goal="Analyze match probabilities using Elo ratings",
|
| 40 |
+
backstory="A veteran cricket data scientist who specializes in statistical modelling and match prediction.",
|
| 41 |
+
tools=[match_probability, simulate_group_stage],
|
| 42 |
+
llm=llm,
|
| 43 |
+
verbose=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
strategist = Agent(
|
| 47 |
+
role="Cricket Strategist",
|
| 48 |
+
goal="Evaluate player strengths and match conditions",
|
| 49 |
+
backstory="A former international cricket coach who studies tactical matchups and team combinations.",
|
| 50 |
+
llm=llm,
|
| 51 |
+
verbose=True
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
chaos_agent = Agent(
|
| 55 |
+
role="Chaos Analyst",
|
| 56 |
+
goal="Identify upset scenarios",
|
| 57 |
+
backstory="A pundit who believes cricket is unpredictable and focuses on variance and surprise outcomes.",
|
| 58 |
+
llm=llm,
|
| 59 |
+
verbose=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
compere = Agent(
|
| 63 |
+
role="Debate Host",
|
| 64 |
+
goal="Summarize debate and provide the verdict",
|
| 65 |
+
backstory="A neutral studio host moderating a panel of cricket experts discussing match predictions.",
|
| 66 |
+
llm=llm,
|
| 67 |
+
verbose=True
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def run_agents(query_type, teamA=None, teamB=None, venue=None, group=None):
|
| 72 |
+
|
| 73 |
+
if query_type == "match":
|
| 74 |
+
|
| 75 |
+
stats_description = f"""
|
| 76 |
+
You are analyzing the match between {teamA} and {teamB}.
|
| 77 |
+
|
| 78 |
+
IMPORTANT:
|
| 79 |
+
You MUST call the tool `match_probability`
|
| 80 |
+
with teamA="{teamA}" and teamB="{teamB}"
|
| 81 |
+
|
| 82 |
+
Do NOT estimate probabilities yourself.
|
| 83 |
+
Do NOT invent Elo ratings.
|
| 84 |
+
|
| 85 |
+
Use the tool output to produce your answer.
|
| 86 |
+
|
| 87 |
+
Venue: {venue if venue else "Neutral conditions assumed"}
|
| 88 |
+
|
| 89 |
+
Output:
|
| 90 |
+
- Team A win probability
|
| 91 |
+
- Team B win probability
|
| 92 |
+
- Short explanation
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
strategist_description = f"""
|
| 96 |
+
Consider the statistical prediction for {teamA} vs {teamB}.
|
| 97 |
+
|
| 98 |
+
Now evaluate tactical factors:
|
| 99 |
+
- batting strengths
|
| 100 |
+
- bowling strengths
|
| 101 |
+
- venue conditions
|
| 102 |
+
|
| 103 |
+
Explain how these factors may influence the match.
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
chaos_description = f"""
|
| 107 |
+
Challenge the prediction for {teamA} vs {teamB}.
|
| 108 |
+
|
| 109 |
+
Discuss possible upset scenarios.
|
| 110 |
+
|
| 111 |
+
Consider:
|
| 112 |
+
- volatility of T20 cricket
|
| 113 |
+
- early wickets
|
| 114 |
+
- momentum shifts
|
| 115 |
+
|
| 116 |
+
Explain when the weaker team might win.
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
compere_description = f"""
|
| 120 |
+
Summarize the panel discussion for the match {teamA} vs {teamB}.
|
| 121 |
+
|
| 122 |
+
Combine insights from:
|
| 123 |
+
- statistics
|
| 124 |
+
- strategy
|
| 125 |
+
- chaos analysis
|
| 126 |
+
|
| 127 |
+
Provide final verdict:
|
| 128 |
+
- predicted winner
|
| 129 |
+
- probability
|
| 130 |
+
- key factors
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
elif query_type == "group":
|
| 134 |
+
|
| 135 |
+
stats_description = f"""
|
| 136 |
+
Simulate Group {group} using the simulate_group_stage tool.
|
| 137 |
+
|
| 138 |
+
Provide:
|
| 139 |
+
- probability of each team qualifying
|
| 140 |
+
- explanation of results
|
| 141 |
+
"""
|
| 142 |
+
|
| 143 |
+
strategist_description = f"""
|
| 144 |
+
Interpret the Group {group} simulation results.
|
| 145 |
+
|
| 146 |
+
Discuss:
|
| 147 |
+
- strongest teams
|
| 148 |
+
- potential challengers
|
| 149 |
+
- possible group surprises
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
chaos_description = f"""
|
| 153 |
+
Highlight possible upset scenarios in Group {group}.
|
| 154 |
+
|
| 155 |
+
Explain how weaker teams could still qualify.
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
compere_description = f"""
|
| 159 |
+
Summarize the analysis for Group {group}.
|
| 160 |
+
|
| 161 |
+
Provide:
|
| 162 |
+
- top teams expected to qualify
|
| 163 |
+
- overall tournament outlook
|
| 164 |
+
"""
|
| 165 |
+
else:
|
| 166 |
+
raise ValueError('query_type must be "match" or "group"')
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
stats_task = Task(
|
| 170 |
+
description=stats_description,
|
| 171 |
+
expected_output="""
|
| 172 |
+
Output format:
|
| 173 |
+
|
| 174 |
+
Team A:
|
| 175 |
+
Win Probability:
|
| 176 |
+
|
| 177 |
+
Team B:
|
| 178 |
+
Win Probability:
|
| 179 |
+
|
| 180 |
+
Explanation:
|
| 181 |
+
""",
|
| 182 |
+
agent=statistician
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
strategist_task = Task(
|
| 186 |
+
description=strategist_description,
|
| 187 |
+
expected_output="Strategic match analysis",
|
| 188 |
+
agent=strategist
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
chaos_task = Task(
|
| 192 |
+
description=chaos_description,
|
| 193 |
+
expected_output="Upset scenarios and uncertainty discussion",
|
| 194 |
+
agent=chaos_agent
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
compere_task = Task(
|
| 198 |
+
description=compere_description,
|
| 199 |
+
expected_output="Final verdict combining all insights",
|
| 200 |
+
agent=compere
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
crew = Crew(
|
| 204 |
+
agents=[statistician, strategist, chaos_agent, compere],
|
| 205 |
+
tasks=[stats_task, strategist_task, chaos_task, compere_task],
|
| 206 |
+
process=Process.sequential,
|
| 207 |
+
verbose=True
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
result = crew.kickoff()
|
| 211 |
+
# EXTRACT OUTPUTS
|
| 212 |
+
stats_output = stats_task.output.raw
|
| 213 |
+
strategist_output = strategist_task.output.raw
|
| 214 |
+
chaos_output = chaos_task.output.raw
|
| 215 |
+
compere_output = compere_task.output.raw
|
| 216 |
+
return {
|
| 217 |
+
"stats": stats_output,
|
| 218 |
+
"strategist": strategist_output,
|
| 219 |
+
"chaos": chaos_output,
|
| 220 |
+
"compere": compere_output,
|
| 221 |
+
"result": result
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
# return result
|