@@ -83,7 +83,8 @@ def create(db_mux_rule: db_models.MuxRule, route: ModelRoute) -> MuxingRuleMatch
8383 mux_models .MuxMatcherType .filename_match : FileMuxingRuleMatcher ,
8484 mux_models .MuxMatcherType .fim_filename : RequestTypeAndFileMuxingRuleMatcher ,
8585 mux_models .MuxMatcherType .chat_filename : RequestTypeAndFileMuxingRuleMatcher ,
86- mux_models .MuxMatcherType .persona_description : PersonaDescriptionMuxingRuleMatcher ,
86+ mux_models .MuxMatcherType .persona_description : UserMsgsPersonaDescMuxMatcher ,
87+ mux_models .MuxMatcherType .sys_prompt_persona_desc : SysPromptPersonaDescMuxMatcher ,
8788 }
8889
8990 try :
@@ -173,12 +174,42 @@ async def match(self, thing_to_match: mux_models.ThingToMatchMux) -> bool:
173174 return is_rule_matched
174175
175176
176- class PersonaDescriptionMuxingRuleMatcher (MuxingRuleMatcher ):
177+ class PersonaDescMuxMatcher (MuxingRuleMatcher ):
177178 """Muxing rule to match the request content to a persona description."""
178179
179- def _get_user_messages_from_body (self , body : Dict ) -> List [str ]:
180+ @abstractmethod
181+ def _get_queries_for_persona_match (self , body : Dict ) -> List [str ]:
182+ """
183+ Get the queries to use for persona matching.
184+ """
185+ pass
186+
187+ async def match (self , thing_to_match : mux_models .ThingToMatchMux ) -> bool :
188+ """
189+ Return True if the matcher is the persona description matched with the queries.
190+
191+ The queries are extracted from the body and will depend on the type of matcher.
192+ 1. UserMessagesPersonaDescMuxMatcher: Extracts queries from the user messages in the body.
193+ 2. SysPromptPersonaDescMuxMatcher: Extracts queries from the system messages in the body.
194+ """
195+ queries = self ._get_queries_for_persona_match (thing_to_match .body )
196+ if not queries :
197+ return False
198+
199+ persona_manager = PersonaManager ()
200+ is_persona_matched = await persona_manager .check_persona_match (
201+ persona_name = self ._mux_rule .matcher , queries = queries
202+ )
203+ if is_persona_matched :
204+ logger .info ("Persona rule matched" , persona = self ._mux_rule .matcher )
205+ return is_persona_matched
206+
207+
208+ class UserMsgsPersonaDescMuxMatcher (PersonaDescMuxMatcher ):
209+
210+ def _get_queries_for_persona_match (self , body : Dict ) -> List [str ]:
180211 """
181- Get the user messages from the body to use as queries .
212+ Get the queries from the user messages in the body .
182213 """
183214 user_messages = []
184215 for msg in body .get ("messages" , []):
@@ -194,22 +225,34 @@ def _get_user_messages_from_body(self, body: Dict) -> List[str]:
194225 user_messages .append (msgs_content )
195226 return user_messages
196227
197- async def match (self , thing_to_match : mux_models .ThingToMatchMux ) -> bool :
228+
229+ class SysPromptPersonaDescMuxMatcher (PersonaDescMuxMatcher ):
230+
231+ def _get_queries_for_persona_match (self , body : Dict ) -> List [str ]:
198232 """
199- Return True if the matcher is the persona description matched with the
200- user messages.
233+ Get the queries from the system messages in the body.
201234 """
202- user_messages = self ._get_user_messages_from_body (thing_to_match .body )
203- if not user_messages :
204- return False
235+ system_messages = []
236+ for msg in body .get ("messages" , []):
237+ if msg .get ("role" , "" ) in ["system" , "developer" ]:
238+ msgs_content = msg .get ("content" )
239+ if not msgs_content :
240+ continue
241+ if isinstance (msgs_content , list ):
242+ for msg_content in msgs_content :
243+ if msg_content .get ("type" , "" ) == "text" :
244+ system_messages .append (msg_content .get ("text" , "" ))
245+ elif isinstance (msgs_content , str ):
246+ system_messages .append (msgs_content )
205247
206- persona_manager = PersonaManager ()
207- is_persona_matched = await persona_manager .check_persona_match (
208- persona_name = self ._mux_rule .matcher , queries = user_messages
209- )
210- if is_persona_matched :
211- logger .info ("Persona rule matched" , persona = self ._mux_rule .matcher )
212- return is_persona_matched
248+ # Handling the anthropic system prompt
249+ anthropic_sys_prompt = body .get ("system" )
250+ if anthropic_sys_prompt :
251+ system_messages .append (anthropic_sys_prompt )
252+
253+ # In an ideal world, the length of system_messages should be 1. Returnin the list
254+ # to handle any edge cases and to not break parent function's signature.
255+ return system_messages
213256
214257
215258class MuxingRulesinWorkspaces :
0 commit comments