Skip to content

Commit 460a2a1

Browse files
Merge pull request #570 from microsoft/psl-bug-25080
fix: Add regular expression to avoid unnecessary citation in response
2 parents 2310d99 + 9319c1e commit 460a2a1

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/backend/v3/callbacks/response_handlers.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import asyncio
77
import logging
88
import time
9-
9+
import re
1010
from semantic_kernel.contents import ChatMessageContent, StreamingChatMessageContent
1111
from v3.config.settings import connection_config
1212
from v3.models.messages import (
@@ -18,6 +18,24 @@
1818
)
1919

2020

21+
def clean_citations(text: str) -> str:
22+
"""Remove citation markers from agent responses while preserving formatting."""
23+
if not text:
24+
return text
25+
26+
# Remove citation patterns like [9:0|source], [9:1|source], etc.
27+
text = re.sub(r'\[\d+:\d+\|source\]', '', text)
28+
29+
# Remove other common citation pattern
30+
text = re.sub(r'\[\s*source\s*\]', '', text, flags=re.IGNORECASE)
31+
text = re.sub(r'\[\d+\]', '', text)
32+
text = re.sub(r'【[^】]*】', '', text) # Unicode brackets
33+
text = re.sub(r'\(source:[^)]*\)', '', text, flags=re.IGNORECASE)
34+
text = re.sub(r'\[source:[^\]]*\]', '', text, flags=re.IGNORECASE)
35+
36+
return text
37+
38+
2139
def agent_response_callback(message: ChatMessageContent, user_id: str = None) -> None:
2240
"""Observer function to print detailed information about streaming messages."""
2341
# import sys
@@ -55,7 +73,7 @@ def agent_response_callback(message: ChatMessageContent, user_id: str = None) ->
5573
final_message = AgentMessage(
5674
agent_name=agent_name,
5775
timestamp=time.time() or "",
58-
content=message.content or "",
76+
content=clean_citations(message.content) or "",
5977
)
6078

6179
asyncio.create_task(
@@ -80,7 +98,7 @@ async def streaming_agent_response_callback(
8098
try:
8199
message = AgentMessageStreaming(
82100
agent_name=streaming_message.name or "Unknown Agent",
83-
content=streaming_message.content,
101+
content=clean_citations(streaming_message.content),
84102
is_final=is_final,
85103
)
86104
await connection_config.send_status_update_async(

0 commit comments

Comments
 (0)