@@ -160,12 +160,13 @@ def _create_feedback_blocks(
160160 # Main response block
161161 blocks = []
162162
163- # Citation buttons
163+ # Create citation buttons
164164 if citations is None or len (citations ) == 0 :
165165 logger .info ("No citations" )
166166 else :
167167 action_buttons = []
168168 for i , citation in enumerate (citations ):
169+ logger .info ("Creating citation" , extra = {"Citation" : citation })
169170 # Create citation blocks
170171 # keys = ["source number", "title", "filename", "reference text", "link"]
171172 title = (
@@ -287,11 +288,9 @@ def process_async_slack_action(body: Dict[str, Any], client: WebClient) -> None:
287288 timestamp = body ["message" ]["ts" ]
288289
289290 # Check if the action is for a citation
290- if (action_id or "" ).startswith ("cite_" ):
291- params = json .loads (action_data )
292-
291+ if action_id == "cite" :
293292 # Update message to include citation content
294- open_citation (channel_id , timestamp , message , params , client )
293+ open_citation (channel_id , timestamp , message , action_data , client )
295294 return
296295
297296 if message_ts and not is_latest_message (conversation_key = conversation_key , message_ts = message_ts ):
@@ -404,15 +403,22 @@ def process_slack_message(event: Dict[str, Any], event_id: str, client: WebClien
404403 response_text = ai_response ["text" ]
405404
406405 # Split out citation block if present
407- keys = ["source number" , "title" , "filename" , "reference text" , "link" ]
408- split = response_text .split ("------" )
409- if len (split ) == 1 :
410- citations = []
411- else :
406+ # Citations are not returned in the object without using `$output_format_instructions$` which overrides the
407+ # system prompt. Instead, pull out and format the citations in the prompt manually
408+ prompt_value_keys = ["source number" , "title" , "filename" , "reference text" , "link" ]
409+ split = response_text .split ("------" ) # Citations are separated by ------
410+ citations = []
411+ if len (split ) != 1 :
412412 response_text = split [0 ]
413413 citation_block = split [1 ]
414- citations = re .split ("<cit>" , citation_block )[1 :] or []
415- citations = [dict (zip (keys , citation .split ("|" ))) for citation in citations ]
414+
415+ citations = re .compile (r"<cit\b[^>]*>(.*?)</cit>" , citation_block , re .DOTALL | re .IGNORECASE ).findall (
416+ citation_block
417+ )
418+ if len (citations ) > 0 :
419+ logger .info ("Found citation(s)" , extra = {"Citations" : citations })
420+ citation_dict = [dict (zip (prompt_value_keys , citation .split ("|" ))) for citation in citations ]
421+ logger .info ("Parsed citation(s)" , extra = {"CitationsDict" : citation_dict })
416422
417423 # Post the answer (plain) to get message_ts
418424 post_params = {"channel" : channel , "text" : response_text }
0 commit comments