refac
This commit is contained in:
@@ -891,23 +891,25 @@ def handle_responses_streaming_event(
|
|||||||
|
|
||||||
|
|
||||||
def get_source_context(
|
def get_source_context(
|
||||||
sources: list, source_id_map: dict = None, include_content: bool = True
|
sources: list, source_ids: dict = None, include_content: bool = True
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Build <source> tag context string from citation sources.
|
Build <source> tag context string from citation sources.
|
||||||
"""
|
"""
|
||||||
context_string = ""
|
context_string = ""
|
||||||
if source_id_map is None:
|
if source_ids is None:
|
||||||
source_id_map = {}
|
source_ids = {}
|
||||||
for source in sources:
|
for source in sources:
|
||||||
for doc, meta in zip(source.get("document", []), source.get("metadata", [])):
|
for doc, meta in zip(source.get("document", []), source.get("metadata", [])):
|
||||||
src_id = meta.get("source") or source.get("source", {}).get("id") or "N/A"
|
source_id = (
|
||||||
if src_id not in source_id_map:
|
meta.get("source") or source.get("source", {}).get("id") or "N/A"
|
||||||
source_id_map[src_id] = len(source_id_map) + 1
|
)
|
||||||
|
if source_id not in source_ids:
|
||||||
|
source_ids[source_id] = len(source_ids) + 1
|
||||||
src_name = source.get("source", {}).get("name")
|
src_name = source.get("source", {}).get("name")
|
||||||
body = doc if include_content else ""
|
body = doc if include_content else ""
|
||||||
context_string += (
|
context_string += (
|
||||||
f'<source id="{source_id_map[src_id]}"'
|
f'<source id="{source_ids[source_id]}"'
|
||||||
+ (f' name="{src_name}"' if src_name else "")
|
+ (f' name="{src_name}"' if src_name else "")
|
||||||
+ f">{body}</source>\n"
|
+ f">{body}</source>\n"
|
||||||
)
|
)
|
||||||
@@ -4494,12 +4496,12 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
|
|
||||||
# Build context: file sources with content,
|
# Build context: file sources with content,
|
||||||
# tool sources as citation markers only.
|
# tool sources as citation markers only.
|
||||||
source_id_map = {}
|
source_ids = {}
|
||||||
source_context = get_source_context(
|
source_context = get_source_context(
|
||||||
metadata.get("sources", []), source_id_map
|
metadata.get("sources", []), source_ids
|
||||||
) + get_source_context(
|
) + get_source_context(
|
||||||
all_tool_call_sources,
|
all_tool_call_sources,
|
||||||
source_id_map,
|
source_ids,
|
||||||
include_content=False,
|
include_content=False,
|
||||||
)
|
)
|
||||||
source_context = source_context.strip()
|
source_context = source_context.strip()
|
||||||
|
|||||||
Reference in New Issue
Block a user