Skip to content

Commit 21dfc79

Browse files
updated chart agent factory
1 parent 32fa238 commit 21dfc79

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/api/agents/chart_agent_factory.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ async def create_agent(cls, config):
2222
Returns:
2323
dict: A dictionary containing the created 'agent' and its associated 'client'.
2424
"""
25-
instructions = '''You are an assistant that generates valid Chart.js v4.4.4 compatible JSON.
26-
Your goal is to produce a JSON object that includes:
27-
- `type` (chart type: bar, line, pie, etc.)
28-
- `data` (with `labels` and `datasets`)
29-
- `options` (to enhance rendering and clarity)
30-
Important Rules:
31-
- Combine both the user's query and the provided tabular/textual data to choose the best chart type.
32-
- Only generate a chart if the data contains numbers.
33-
- If no numbers are found, return:
34-
{"error": "Chart cannot be generated due to lack of numerical data."}
35-
- Do NOT include any explanations, markdown formatting, or tooltips — just clean JSON.
36-
- Remove all trailing commas.
37-
- Ensure the JSON can be parsed using `json.loads()` in Python.
38-
- Ensure axis ticks are readable (adjust `ticks.padding`, `maxWidth`, etc.).
39-
- Avoid bars being too narrow or cropped by setting reasonable `barPercentage` and `categoryPercentage`.
40-
'''
25+
instructions = """You are an assistant that helps generate valid chart data to be shown using chart.js with version 4.4.4 compatible.
26+
Include chart type and chart options.
27+
Pick the best chart type for given data.
28+
Do not generate a chart unless the input contains some numbers. Otherwise return a message that Chart cannot be generated.
29+
Only return a valid JSON output and nothing else.
30+
Verify that the generated JSON can be parsed using json.loads.
31+
Do not include tooltip callbacks in JSON.
32+
Always make sure that the generated json can be rendered in chart.js.
33+
Always remove any extra trailing commas.
34+
Verify and refine that JSON should not have any syntax errors like extra closing brackets.
35+
Ensure Y-axis labels are fully visible by increasing **ticks.padding**, **ticks.maxWidth**, or enabling word wrapping where necessary.
36+
Ensure bars and data points are evenly spaced and not squished or cropped at **100%** resolution by maintaining appropriate **barPercentage** and **categoryPercentage** values."""
37+
4138

4239
project_client = AIProjectClient(
4340
endpoint=config.ai_project_endpoint,

src/api/services/chat_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ async def process_rag_response(self, rag_response, query):
9191
Uses the ChartAgent directly (agentic call) to extract chart data for Chart.js.
9292
"""
9393
try:
94-
combined_input = f"{query}\n{rag_response}"
94+
user_prompt = f"""Generate chart data for -
95+
{query}
96+
{rag_response}
97+
"""
9598

9699
agent_info = await ChartAgentFactory.get_agent()
97100
agent = agent_info["agent"]
@@ -102,7 +105,7 @@ async def process_rag_response(self, rag_response, query):
102105
client.agents.messages.create(
103106
thread_id=thread.id,
104107
role=MessageRole.USER,
105-
content=combined_input
108+
content=user_prompt
106109
)
107110

108111
run = client.agents.runs.create_and_process(

0 commit comments

Comments
 (0)