@@ -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"\n Remaining 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.
90123You provide helpful, accurate advice based on expert analysis and data.
91124Use the provided context to answer questions, but also apply your FPL knowledge.
92125Be 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 }
99132Question: { query }
100133
101134Please 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
0 commit comments