Spaces:
Sleeping
Sleeping
Added intent recognition
Browse files- src/agents.py +99 -0
src/agents.py
CHANGED
|
@@ -35,6 +35,105 @@ llm = LLM(
|
|
| 35 |
temperature=0.2
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
statistician = Agent(
|
| 40 |
role="Cricket Statistician",
|
|
|
|
| 35 |
temperature=0.2
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# Get the intent
|
| 39 |
+
def detect_intent(user_query):
|
| 40 |
+
|
| 41 |
+
prompt = f"""
|
| 42 |
+
You are an intent classification and entity extraction engine for a cricket analytics app.
|
| 43 |
+
You understand common cricketing abbreviations and culture.
|
| 44 |
+
Your job is to read a user query and return a STRICT JSON output.
|
| 45 |
+
---
|
| 46 |
+
Supported intents:
|
| 47 |
+
1. match_prediction
|
| 48 |
+
2. group_progression
|
| 49 |
+
3. tournament_prediction
|
| 50 |
+
4. context_update
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
Rules:
|
| 54 |
+
- ALWAYS return valid JSON (no explanation)
|
| 55 |
+
- Team names MUST be 3-letter abbreviations:
|
| 56 |
+
Examples: IND, AUS, ENG, PAK, NZ, SA, WI, SL, BAN, AFG, NED
|
| 57 |
+
- If teams are not present → return null
|
| 58 |
+
- If venue not mentioned → return null
|
| 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 |
+
return json.loads(clean_json)
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return {
|
| 129 |
+
"intent": "unknown",
|
| 130 |
+
"teamA": None,
|
| 131 |
+
"teamB": None,
|
| 132 |
+
"group": None,
|
| 133 |
+
"venue": None,
|
| 134 |
+
"tournament_team": None,
|
| 135 |
+
"context": None
|
| 136 |
+
}
|
| 137 |
|
| 138 |
statistician = Agent(
|
| 139 |
role="Cricket Statistician",
|