Spaces:
Sleeping
Sleeping
File updates
Browse files- src/agents.py +57 -79
src/agents.py
CHANGED
|
@@ -1,10 +1,21 @@
|
|
| 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["GROQ_API_KEY"] = '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'
|
| 10 |
os.environ["OPENAI_API_KEY"] = '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'
|
|
@@ -39,93 +50,35 @@ llm = LLM(
|
|
| 39 |
def detect_intent(user_query):
|
| 40 |
|
| 41 |
prompt = f"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
- If group not mentioned β return null
|
| 60 |
-
- If teams don't participate in the world cup β return null
|
| 61 |
-
---
|
| 62 |
-
|
| 63 |
-
Examples:
|
| 64 |
-
User: What will happen when the Kiwis taken on the Aussies
|
| 65 |
-
Output:
|
| 66 |
-
{{
|
| 67 |
-
"intent": "match_prediction",
|
| 68 |
-
"teamA": "NZ",
|
| 69 |
-
"teamB": "AUS",
|
| 70 |
-
"group": null,
|
| 71 |
-
"venue": null,
|
| 72 |
-
"tournament_team": null,
|
| 73 |
-
"context": null
|
| 74 |
-
}}
|
| 75 |
-
|
| 76 |
-
User: Who qualifies from Group D?
|
| 77 |
-
Output:
|
| 78 |
-
{{
|
| 79 |
-
"intent": "group_progression",
|
| 80 |
-
"teamA": null,
|
| 81 |
-
"teamB": null,
|
| 82 |
-
"group": "D",
|
| 83 |
-
"venue": null,
|
| 84 |
-
"tournament_team": null,
|
| 85 |
-
"context": null
|
| 86 |
-
}}
|
| 87 |
-
|
| 88 |
-
User: What are India's chances?
|
| 89 |
-
Output:
|
| 90 |
-
{{
|
| 91 |
-
"intent": "tournament_prediction",
|
| 92 |
-
"teamA": null,
|
| 93 |
-
"teamB": null,
|
| 94 |
-
"group": null,
|
| 95 |
-
"venue": null,
|
| 96 |
-
"tournament_team": "IND",
|
| 97 |
-
"context": null
|
| 98 |
-
}}
|
| 99 |
-
|
| 100 |
-
User: Bumrah is injured
|
| 101 |
-
Output:
|
| 102 |
-
{{
|
| 103 |
-
"intent": "context_update",
|
| 104 |
-
"teamA": null,
|
| 105 |
-
"teamB": null,
|
| 106 |
-
"group": null,
|
| 107 |
-
"venue": null,
|
| 108 |
-
"tournament_team": null,
|
| 109 |
-
"context": "Bumrah is injured"
|
| 110 |
-
}}
|
| 111 |
-
---
|
| 112 |
-
Now process this:
|
| 113 |
-
User: {user_query}
|
| 114 |
-
Return ONLY JSON.
|
| 115 |
-
"""
|
| 116 |
|
| 117 |
response = llm.call(prompt)
|
| 118 |
|
| 119 |
-
# Clean response (LLMs sometimes add junk)
|
| 120 |
try:
|
| 121 |
json_start = response.find("{")
|
| 122 |
json_end = response.rfind("}") + 1
|
| 123 |
clean_json = response[json_start:json_end]
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
except
|
| 128 |
-
|
| 129 |
"intent": "unknown",
|
| 130 |
"teamA": None,
|
| 131 |
"teamB": None,
|
|
@@ -135,6 +88,31 @@ def detect_intent(user_query):
|
|
| 135 |
"context": None
|
| 136 |
}
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
statistician = Agent(
|
| 139 |
role="Cricket Statistician",
|
| 140 |
goal="Analyze match probabilities using Elo ratings",
|
|
|
|
| 1 |
import os
|
| 2 |
from crewai import Agent, Task, Crew, Process, LLM
|
| 3 |
from crewai.tools import tool
|
| 4 |
+
from rapidfuzz import process
|
| 5 |
+
from data_loader import teams
|
| 6 |
from probability_engine import elo_probability, monte_carlo_group
|
| 7 |
|
| 8 |
+
# Create valid team set from dataset
|
| 9 |
+
VALID_TEAMS = set(teams["team"].str.upper())
|
| 10 |
|
| 11 |
+
def match_team_name(name):
|
| 12 |
+
if not name:
|
| 13 |
+
return None
|
| 14 |
+
match, score, _ = process.extractOne(name.upper(), VALID_TEAMS)
|
| 15 |
+
if score > 70:
|
| 16 |
+
return match
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
# os.environ["GROQ_API_KEY"] = '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'
|
| 20 |
os.environ["GROQ_API_KEY"] = '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'
|
| 21 |
os.environ["OPENAI_API_KEY"] = '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'
|
|
|
|
| 50 |
def detect_intent(user_query):
|
| 51 |
|
| 52 |
prompt = f"""
|
| 53 |
+
You are an intent classification and entity extraction engine for a cricket analytics app.
|
| 54 |
+
You understand common cricketing abbreviations and culture.
|
| 55 |
+
|
| 56 |
+
Return STRICT JSON only.
|
| 57 |
+
|
| 58 |
+
Supported intents:
|
| 59 |
+
- match_prediction
|
| 60 |
+
- group_progression
|
| 61 |
+
- tournament_prediction
|
| 62 |
+
- context_update
|
| 63 |
+
|
| 64 |
+
Rules:
|
| 65 |
+
- Team names should be cricket country names or abbreviations
|
| 66 |
+
- If not found β return null
|
| 67 |
+
|
| 68 |
+
User: {user_query}
|
| 69 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
response = llm.call(prompt)
|
| 72 |
|
|
|
|
| 73 |
try:
|
| 74 |
json_start = response.find("{")
|
| 75 |
json_end = response.rfind("}") + 1
|
| 76 |
clean_json = response[json_start:json_end]
|
| 77 |
|
| 78 |
+
parsed = json.loads(clean_json)
|
| 79 |
|
| 80 |
+
except:
|
| 81 |
+
parsed = {
|
| 82 |
"intent": "unknown",
|
| 83 |
"teamA": None,
|
| 84 |
"teamB": None,
|
|
|
|
| 88 |
"context": None
|
| 89 |
}
|
| 90 |
|
| 91 |
+
# π₯ STEP 1 β normalize using fuzzy match
|
| 92 |
+
parsed["teamA"] = match_team_name(parsed.get("teamA"))
|
| 93 |
+
parsed["teamB"] = match_team_name(parsed.get("teamB"))
|
| 94 |
+
|
| 95 |
+
# π₯ STEP 2 β fallback extraction from query if LLM failed
|
| 96 |
+
if parsed["intent"] == "match_prediction":
|
| 97 |
+
if not parsed["teamA"] or not parsed["teamB"]:
|
| 98 |
+
|
| 99 |
+
query_lower = user_query.lower()
|
| 100 |
+
|
| 101 |
+
found = []
|
| 102 |
+
for team in VALID_TEAMS:
|
| 103 |
+
if team.lower() in query_lower:
|
| 104 |
+
found.append(team)
|
| 105 |
+
|
| 106 |
+
if len(found) >= 2:
|
| 107 |
+
parsed["teamA"] = found[0]
|
| 108 |
+
parsed["teamB"] = found[1]
|
| 109 |
+
|
| 110 |
+
# π₯ STEP 3 β final safety
|
| 111 |
+
if parsed["intent"] == "match_prediction":
|
| 112 |
+
if not parsed["teamA"] or not parsed["teamB"]:
|
| 113 |
+
parsed["intent"] = "unknown"
|
| 114 |
+
return parsed
|
| 115 |
+
|
| 116 |
statistician = Agent(
|
| 117 |
role="Cricket Statistician",
|
| 118 |
goal="Analyze match probabilities using Elo ratings",
|