Skip to content

Commit 2f766ad

Browse files
Add user team context to RAG queries
- RAGChat accepts teamContext prop with players, budget, transfers - TeamManagement passes team data to RAGChat - LLM service API accepts user_context in query request - RAG pipeline includes team context in LLM prompt for personalized advice
1 parent 80d8eec commit 2f766ad

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

frontend/src/components/RAGChat.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useRef, useEffect } from "react";
22
import axios from "axios";
33
import "./RAGChat.css";
44

5-
const RAGChat = () => {
5+
const RAGChat = ({ teamContext }) => {
66
const [query, setQuery] = useState("");
77
const [messages, setMessages] = useState([]);
88
const [loading, setLoading] = useState(false);
@@ -33,10 +33,23 @@ const RAGChat = () => {
3333
try {
3434
// Call LLM service - use environment variable or default to localhost
3535
const llmApiUrl = process.env.REACT_APP_LLM_API_URL || "http://localhost:5002";
36-
const response = await axios.post(`${llmApiUrl}/api/v1/query`, {
36+
37+
// Build request with optional team context
38+
const requestBody = {
3739
query: userMessage,
3840
include_sources: true,
39-
});
41+
};
42+
43+
// Add team context if available
44+
if (teamContext) {
45+
requestBody.user_context = {
46+
team_players: teamContext.players || [],
47+
budget: teamContext.budget,
48+
free_transfers: teamContext.freeTransfers,
49+
};
50+
}
51+
52+
const response = await axios.post(`${llmApiUrl}/api/v1/query`, requestBody);
4053

4154
// Add assistant response
4255
setMessages([

frontend/src/components/TeamManagement.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,14 @@ const TeamManagement = () => {
180180
</button>
181181
</div>
182182

183-
{/* RAG Chat Component */}
184-
<RAGChat />
183+
{/* RAG Chat Component - pass team context */}
184+
<RAGChat
185+
teamContext={{
186+
players: teamData || [],
187+
budget: optimizationSettings.budget,
188+
freeTransfers: optimizationSettings.freeTransfers,
189+
}}
190+
/>
185191

186192
<div className="team-content">
187193
{/* Only show import section if no team data */}

llm-service/app/core/rag.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def generate_response(
6565
self,
6666
query: str,
6767
context_chunks: list[dict],
68-
system_prompt: str = None
68+
system_prompt: str = None,
69+
user_context: dict = None
6970
) -> str:
7071
"""
7172
Generate response using LLM with retrieved context
@@ -74,6 +75,7 @@ def generate_response(
7475
query: User query
7576
context_chunks: Retrieved context chunks
7677
system_prompt: Custom system prompt (optional)
78+
user_context: User's team context (optional)
7779
7880
Returns:
7981
Generated response
@@ -84,9 +86,40 @@ def generate_response(
8486
for chunk in context_chunks
8587
])
8688

87-
# Default system prompt
89+
# Build user team context if provided
90+
team_context_text = ""
91+
if user_context:
92+
team_context_text = "\n\n--- USER'S TEAM CONTEXT ---\n"
93+
94+
if user_context.get('team_players'):
95+
players = user_context['team_players']
96+
team_context_text += f"Current Squad ({len(players)} players):\n"
97+
for p in players[:15]:
98+
name = p.get('name', 'Unknown')
99+
position = p.get('position', '?')
100+
team = p.get('team', '?')
101+
points = p.get('totalPoints', p.get('total_points', 0))
102+
value = p.get('value', 0)
103+
team_context_text += f"- {name} ({position}, {team}) - £{value}m, {points} pts\n"
104+
105+
if user_context.get('budget'):
106+
team_context_text += f"\nRemaining Budget: £{user_context['budget']}m\n"
107+
108+
if user_context.get('free_transfers') is not None:
109+
team_context_text += f"Free Transfers: {user_context['free_transfers']}\n"
110+
111+
team_context_text += "--- END TEAM CONTEXT ---\n"
112+
113+
# Default system prompt - updated to reference team context
88114
if system_prompt is None:
89-
system_prompt = """You are an expert Fantasy Premier League (FPL) assistant.
115+
if user_context:
116+
system_prompt = """You are an expert Fantasy Premier League (FPL) assistant.
117+
You provide helpful, accurate advice based on expert analysis and data.
118+
Use the provided context to answer questions, but also apply your FPL knowledge.
119+
IMPORTANT: The user has provided their current team. Tailor your advice specifically to their squad, budget, and available transfers.
120+
Be concise and actionable in your recommendations."""
121+
else:
122+
system_prompt = """You are an expert Fantasy Premier League (FPL) assistant.
90123
You provide helpful, accurate advice based on expert analysis and data.
91124
Use the provided context to answer questions, but also apply your FPL knowledge.
92125
Be concise and actionable in your recommendations."""
@@ -95,7 +128,7 @@ def generate_response(
95128
user_message = f"""Context from FPL experts:
96129
97130
{context_text}
98-
131+
{team_context_text}
99132
Question: {query}
100133
101134
Please provide a helpful answer based on the expert context above."""
@@ -113,7 +146,8 @@ def answer_question(
113146
query: str,
114147
category: str = None,
115148
gameweek: int = None,
116-
include_sources: bool = True
149+
include_sources: bool = True,
150+
user_context: dict = None
117151
) -> dict:
118152
"""
119153
Complete RAG pipeline: retrieve + generate
@@ -123,11 +157,15 @@ def answer_question(
123157
category: Filter by category
124158
gameweek: Filter by gameweek
125159
include_sources: Include source metadata in response
160+
user_context: User's team context (optional)
126161
127162
Returns:
128163
Dict with 'answer' and optionally 'sources'
129164
"""
130165
logger.info(f"RAG query: {query}")
166+
if user_context:
167+
player_count = len(user_context.get('team_players', []))
168+
logger.info(f"User context: {player_count} players, budget={user_context.get('budget')}, transfers={user_context.get('free_transfers')}")
131169

132170
# Retrieve context
133171
context_chunks = self.retrieve_context(
@@ -144,8 +182,8 @@ def answer_question(
144182
"sources": []
145183
}
146184

147-
# Generate response
148-
answer = self.generate_response(query, context_chunks)
185+
# Generate response with user context
186+
answer = self.generate_response(query, context_chunks, user_context=user_context)
149187

150188
result = {"answer": answer}
151189

llm-service/app/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,18 @@ async def service_info():
7575
}
7676

7777

78+
class UserContext(BaseModel):
79+
team_players: Optional[list] = None
80+
budget: Optional[float] = None
81+
free_transfers: Optional[int] = None
82+
83+
7884
class QueryRequest(BaseModel):
7985
query: str
8086
category: Optional[str] = None
8187
gameweek: Optional[int] = None
8288
include_sources: bool = True
89+
user_context: Optional[UserContext] = None
8390

8491

8592
class QueryResponse(BaseModel):
@@ -92,11 +99,22 @@ async def query_rag(request: QueryRequest):
9299
"""Query the RAG system"""
93100
try:
94101
rag = get_rag_pipeline()
102+
103+
# Convert user_context to dict if provided
104+
user_context = None
105+
if request.user_context:
106+
user_context = {
107+
'team_players': request.user_context.team_players,
108+
'budget': request.user_context.budget,
109+
'free_transfers': request.user_context.free_transfers,
110+
}
111+
95112
result = rag.answer_question(
96113
query=request.query,
97114
category=request.category,
98115
gameweek=request.gameweek,
99-
include_sources=request.include_sources
116+
include_sources=request.include_sources,
117+
user_context=user_context
100118
)
101119
return QueryResponse(**result)
102120
except Exception as e:

0 commit comments

Comments
 (0)