Skip to content
7 changes: 4 additions & 3 deletions src/api/agents/conversation_agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ async def create_agent(cls, config):

agent_name = f"KM-ConversationKnowledgeAgent-{config.solution_name}"
agent_instructions = '''You are a helpful assistant.
Always return the citations as is in final response.
Always return citation markers exactly as they appear in the source data, placed in the "answer" field at the correct location. Do not modify, convert, or simplify these markers.
Only include citation markers if their sources are present in the "citations" list. Only include sources in the "citations" list if they are used in the answer.
Use the structure { "answer": "", "citations": [ {"url":"","title":""} ] }.
Always return citation markers exactly as they appear in the source data, placed in the "answer" field at the correct location.
Do not modify, convert, normalize, or simplify citation markers or the citations list; the plugin is solely responsible for citation formatting and content.
Only include citation markers if their sources are present in the "citations" list. Only include sources in the "citations" list if they are used in the answer.
Use prior conversation history only for context or vague follow-up requests, and reuse it as a data source solely when the required values are explicitly listed, complete, and unambiguous; never reuse citation markers or sources from previous responses.
When using prior conversation history without calling tools/plugins, omit citation markers and the "citations" list; return only the "answer" field.
If a request explicitly specifies metrics, entities, filters, or time ranges, or if the required data is not available in conversation history, treat it as a new data query and use the appropriate tools or plugins to retrieve the data before responding.
If the question is unrelated to data but is conversational (e.g., greetings or follow-ups), respond appropriately using context.
You MUST NOT generate a chart without numeric data.
Expand Down
1 change: 1 addition & 0 deletions src/api/agents/search_agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async def create_agent(cls, config):
instructions="You are a helpful agent. Use the tools provided and always cite your sources.",
tools=ai_search.definitions,
tool_resources=ai_search.resources,
temperature=0.7
)
logger.info(f"Created new agent: {agent_name} (ID: {agent.id})")

Expand Down
32 changes: 21 additions & 11 deletions src/api/plugins/chat_with_data_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,6 @@ async def get_call_insights(
if run.status == "failed":
print(f"Run failed: {run.last_error}")
else:
def convert_citation_markers(text):
def replace_marker(match):
parts = match.group(1).split(":")
if len(parts) == 2 and parts[1].isdigit():
new_index = int(parts[1]) + 1
return f"[{new_index}]"
return match.group(0)

return re.sub(r'【(\d+:\d+)†source】', replace_marker, text)

for run_step in project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id):
if isinstance(run_step.step_details, RunStepToolCallDetails):
for tool_call in run_step.step_details.tool_calls:
Expand All @@ -157,10 +147,30 @@ def replace_marker(match):
answer["citations"].append({"url": url, "title": title})

messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)

# Convert citation markers and extract used indices
citation_mapping: dict[int, int] = {}

def replace_marker(match):
parts = match.group(1).split(":")
if len(parts) == 2 and parts[1].isdigit():
original_index = int(parts[1])
if original_index not in citation_mapping:
citation_mapping[original_index] = len(citation_mapping) + 1
return f"[{citation_mapping[original_index]}]"
return match.group(0)

for msg in messages:
if msg.role == MessageRole.AGENT and msg.text_messages:
answer["answer"] = msg.text_messages[-1].text.value
answer["answer"] = convert_citation_markers(answer["answer"])
answer["answer"] = re.sub(r'【(\d+:\d+)†source】', replace_marker, answer["answer"])
# Filter and reorder citations based on actual usage
if citation_mapping:
filtered_citations = []
for original_idx in sorted(citation_mapping.keys()):
if original_idx < len(answer["citations"]):
filtered_citations.append(answer["citations"][original_idx])
answer["citations"] = filtered_citations
break
project_client.agents.threads.delete(thread_id=thread.id)
except Exception:
Expand Down
Loading