code comibner fixes
Browse files- app.py +3 -3
- scripts/format_response.py +29 -10
- scripts/tier_maker.py +3 -1
- src/agents/agents.py +12 -28
- src/managers/ai_manager.py +2 -1
- src/managers/chat_manager.py +23 -3
app.py
CHANGED
@@ -474,12 +474,12 @@ async def chat_with_all(
|
|
474 |
"status": "error"
|
475 |
}) + "\n"
|
476 |
return
|
477 |
-
if
|
478 |
total_response += str(response) if response else ""
|
479 |
total_inputs += str(inputs) if inputs else ""
|
480 |
|
481 |
yield json.dumps({
|
482 |
-
"agent": agent_name,
|
483 |
"content": formatted_response,
|
484 |
"status": "success" if response else "error"
|
485 |
}) + "\n"
|
@@ -490,7 +490,7 @@ async def chat_with_all(
|
|
490 |
|
491 |
# Track the code combiner agent response
|
492 |
if "refined_complete_code" in response:
|
493 |
-
model_name = "
|
494 |
provider = app.state.ai_manager.get_provider_for_model(model_name)
|
495 |
input_tokens = len(app.state.ai_manager.tokenizer.encode(str(inputs)))
|
496 |
completion_tokens = len(app.state.ai_manager.tokenizer.encode(str(response)))
|
|
|
474 |
"status": "error"
|
475 |
}) + "\n"
|
476 |
return
|
477 |
+
if "code_combiner_agent" in agent_name:
|
478 |
total_response += str(response) if response else ""
|
479 |
total_inputs += str(inputs) if inputs else ""
|
480 |
|
481 |
yield json.dumps({
|
482 |
+
"agent": agent_name.split("__")[0] if "__" in agent_name else agent_name,
|
483 |
"content": formatted_response,
|
484 |
"status": "success" if response else "error"
|
485 |
}) + "\n"
|
|
|
490 |
|
491 |
# Track the code combiner agent response
|
492 |
if "refined_complete_code" in response:
|
493 |
+
model_name = agent_name.split("__")[0] if "__" in agent_name else agent_name
|
494 |
provider = app.state.ai_manager.get_provider_for_model(model_name)
|
495 |
input_tokens = len(app.state.ai_manager.tokenizer.encode(str(inputs)))
|
496 |
completion_tokens = len(app.state.ai_manager.tokenizer.encode(str(response)))
|
scripts/format_response.py
CHANGED
@@ -28,6 +28,10 @@ def clean_print_statements(code_block):
|
|
28 |
# This regex targets print statements, even if they have newlines inside
|
29 |
return re.sub(r'print\((.*?)(\\n.*?)(.*?)\)', r'print(\1\3)', code_block, flags=re.DOTALL)
|
30 |
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def remove_main_block(code):
|
33 |
# Match the __main__ block
|
@@ -130,6 +134,9 @@ def format_code_backticked_block(code_str):
|
|
130 |
# Remove empty lines that might have been created after cleaning
|
131 |
code_clean = re.sub(r"\n\s*\n+", "\n\n", code_clean)
|
132 |
|
|
|
|
|
|
|
133 |
return f'```python\n{code_clean}\n```'
|
134 |
|
135 |
|
@@ -267,16 +274,17 @@ def execute_code_from_markdown(code_str, dataframe=None):
|
|
267 |
def format_response_to_markdown(api_response, agent_name = None, dataframe=None):
|
268 |
try:
|
269 |
markdown = []
|
270 |
-
|
271 |
-
# Handle error responses
|
272 |
-
if isinstance(api_response, dict) and "error" in api_response:
|
273 |
-
return f"**Error**: {api_response['error']}"
|
274 |
|
275 |
if isinstance(api_response, dict):
|
276 |
for key in api_response:
|
277 |
if "error" in api_response[key]:
|
278 |
return f"**Error**: Rate limit exceeded. Please try switching models from the settings."
|
279 |
# You can add more checks here if needed for other keys
|
|
|
|
|
|
|
|
|
280 |
if "response" in api_response and isinstance(api_response['response'], str):
|
281 |
if any(err in api_response['response'].lower() for err in ["auth", "api", "lm"]):
|
282 |
return "**Error**: Authentication failed. Please check your API key in settings and try again."
|
@@ -284,6 +292,7 @@ def format_response_to_markdown(api_response, agent_name = None, dataframe=None)
|
|
284 |
return "**Error**: Model configuration error. Please verify your model selection in settings."
|
285 |
|
286 |
for agent, content in api_response.items():
|
|
|
287 |
if "memory" in agent or not content:
|
288 |
continue
|
289 |
|
@@ -315,21 +324,30 @@ def format_response_to_markdown(api_response, agent_name = None, dataframe=None)
|
|
315 |
|
316 |
if 'summary' in content:
|
317 |
# make the summary a bullet-point list
|
|
|
318 |
summary_lines = content['summary'].split('\n')
|
|
|
319 |
markdown.append("### Summary\n")
|
320 |
for line in summary_lines:
|
321 |
markdown.append(f"• {line.strip().replace('•', '').replace('-', '')}\n")
|
322 |
|
323 |
-
if 'refined_complete_code' in content:
|
|
|
324 |
try:
|
325 |
-
|
326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
except Exception as e:
|
328 |
logger.log_message(f"Error in execute_code_from_markdown: {str(e)}", level=logging.ERROR)
|
329 |
markdown.append(f"**Error**: {str(e)}")
|
330 |
# continue
|
331 |
|
332 |
-
markdown.append(f"### Refined Complete Code\n{
|
333 |
|
334 |
if output:
|
335 |
markdown.append("### Execution Output\n")
|
@@ -339,7 +357,7 @@ def format_response_to_markdown(api_response, agent_name = None, dataframe=None)
|
|
339 |
markdown.append("### Plotly JSON Outputs\n")
|
340 |
for idx, json_output in enumerate(json_outputs):
|
341 |
markdown.append(f"```plotly\n{json_output}\n```\n")
|
342 |
-
|
343 |
# if agent_name is not None:
|
344 |
# if f"memory_{agent_name}" in api_response:
|
345 |
# markdown.append(f"### Memory\n{api_response[f'memory_{agent_name}']}\n")
|
@@ -347,7 +365,8 @@ def format_response_to_markdown(api_response, agent_name = None, dataframe=None)
|
|
347 |
except Exception as e:
|
348 |
logger.log_message(f"Error in format_response_to_markdown: {str(e)}", level=logging.ERROR)
|
349 |
return f"{str(e)}"
|
350 |
-
|
|
|
351 |
|
352 |
|
353 |
if not markdown or len(markdown) <= 1:
|
|
|
28 |
# This regex targets print statements, even if they have newlines inside
|
29 |
return re.sub(r'print\((.*?)(\\n.*?)(.*?)\)', r'print(\1\3)', code_block, flags=re.DOTALL)
|
30 |
|
31 |
+
def remove_code_block_from_summary(summary):
|
32 |
+
# use regex to remove code block from summary list
|
33 |
+
summary = re.sub(r'```python\n(.*?)\n```', '', summary)
|
34 |
+
return summary.split("\n")
|
35 |
|
36 |
def remove_main_block(code):
|
37 |
# Match the __main__ block
|
|
|
134 |
# Remove empty lines that might have been created after cleaning
|
135 |
code_clean = re.sub(r"\n\s*\n+", "\n\n", code_clean)
|
136 |
|
137 |
+
# remove main
|
138 |
+
code_clean = remove_main_block(code_clean)
|
139 |
+
|
140 |
return f'```python\n{code_clean}\n```'
|
141 |
|
142 |
|
|
|
274 |
def format_response_to_markdown(api_response, agent_name = None, dataframe=None):
|
275 |
try:
|
276 |
markdown = []
|
277 |
+
logger.log_message(f"Formatting response to markdown for agent '{agent_name}' at {time.strftime('%Y-%m-%d %H:%M:%S')}: {api_response}, length: {len(api_response)}", level=logging.INFO)
|
|
|
|
|
|
|
278 |
|
279 |
if isinstance(api_response, dict):
|
280 |
for key in api_response:
|
281 |
if "error" in api_response[key]:
|
282 |
return f"**Error**: Rate limit exceeded. Please try switching models from the settings."
|
283 |
# You can add more checks here if needed for other keys
|
284 |
+
|
285 |
+
# Handle error responses
|
286 |
+
if isinstance(api_response, dict) and "error" in api_response:
|
287 |
+
return f"**Error**: {api_response['error']}"
|
288 |
if "response" in api_response and isinstance(api_response['response'], str):
|
289 |
if any(err in api_response['response'].lower() for err in ["auth", "api", "lm"]):
|
290 |
return "**Error**: Authentication failed. Please check your API key in settings and try again."
|
|
|
292 |
return "**Error**: Model configuration error. Please verify your model selection in settings."
|
293 |
|
294 |
for agent, content in api_response.items():
|
295 |
+
agent = agent.split("__")[0] if "__" in agent else agent
|
296 |
if "memory" in agent or not content:
|
297 |
continue
|
298 |
|
|
|
324 |
|
325 |
if 'summary' in content:
|
326 |
# make the summary a bullet-point list
|
327 |
+
summary_lines = remove_code_block_from_summary(content['summary'])
|
328 |
summary_lines = content['summary'].split('\n')
|
329 |
+
# remove code block from summary
|
330 |
markdown.append("### Summary\n")
|
331 |
for line in summary_lines:
|
332 |
markdown.append(f"• {line.strip().replace('•', '').replace('-', '')}\n")
|
333 |
|
334 |
+
if 'refined_complete_code' in content and 'summary' in content:
|
335 |
+
logger.log_message(f"Refined complete code: {content['refined_complete_code']}", level=logging.INFO)
|
336 |
try:
|
337 |
+
if content['refined_complete_code'] is not None and content['refined_complete_code'] != "":
|
338 |
+
clean_code = format_code_block(content['refined_complete_code'])
|
339 |
+
markdown_code = format_code_backticked_block(content['refined_complete_code'])
|
340 |
+
output, json_outputs = execute_code_from_markdown(clean_code, dataframe)
|
341 |
+
elif "```python" in content['summary']:
|
342 |
+
clean_code = format_code_block(content['summary'])
|
343 |
+
markdown_code = format_code_backticked_block(content['summary'])
|
344 |
+
output, json_outputs = execute_code_from_markdown(clean_code, dataframe)
|
345 |
except Exception as e:
|
346 |
logger.log_message(f"Error in execute_code_from_markdown: {str(e)}", level=logging.ERROR)
|
347 |
markdown.append(f"**Error**: {str(e)}")
|
348 |
# continue
|
349 |
|
350 |
+
markdown.append(f"### Refined Complete Code\n{markdown_code}\n")
|
351 |
|
352 |
if output:
|
353 |
markdown.append("### Execution Output\n")
|
|
|
357 |
markdown.append("### Plotly JSON Outputs\n")
|
358 |
for idx, json_output in enumerate(json_outputs):
|
359 |
markdown.append(f"```plotly\n{json_output}\n```\n")
|
360 |
+
logger.log_message(f"Refined complete code executed, {markdown}", level=logging.INFO)
|
361 |
# if agent_name is not None:
|
362 |
# if f"memory_{agent_name}" in api_response:
|
363 |
# markdown.append(f"### Memory\n{api_response[f'memory_{agent_name}']}\n")
|
|
|
365 |
except Exception as e:
|
366 |
logger.log_message(f"Error in format_response_to_markdown: {str(e)}", level=logging.ERROR)
|
367 |
return f"{str(e)}"
|
368 |
+
|
369 |
+
logger.log_message(f"Generated markdown content for agent '{agent_name}' at {time.strftime('%Y-%m-%d %H:%M:%S')}: {markdown}, length: {len(markdown)}", level=logging.INFO)
|
370 |
|
371 |
|
372 |
if not markdown or len(markdown) <= 1:
|
scripts/tier_maker.py
CHANGED
@@ -37,7 +37,8 @@ costs = {
|
|
37 |
"gemma-7b-it": {"input": 0.00007, "output": 0.00007},
|
38 |
"gemma2-9b-it": {"input": 0.0002, "output": 0.0002},
|
39 |
"llama3-groq-70b-8192-tool-use-preview": {"input": 0.00089, "output": 0.00089},
|
40 |
-
"llama3-groq-8b-8192-tool-use-preview": {"input": 0.00019, "output": 0.00019}
|
|
|
41 |
}
|
42 |
}
|
43 |
|
@@ -139,6 +140,7 @@ print(json.dumps(model_tiers, indent=4))
|
|
139 |
"claude-3-opus-latest",
|
140 |
"claude-3-7-sonnet-latest",
|
141 |
"claude-3-5-sonnet-latest",
|
|
|
142 |
"claude-3-5-haiku-latest",
|
143 |
"deepseek-r1-distill-qwen-32b",
|
144 |
"deepseek-r1-distill-llama-70b",
|
|
|
37 |
"gemma-7b-it": {"input": 0.00007, "output": 0.00007},
|
38 |
"gemma2-9b-it": {"input": 0.0002, "output": 0.0002},
|
39 |
"llama3-groq-70b-8192-tool-use-preview": {"input": 0.00089, "output": 0.00089},
|
40 |
+
"llama3-groq-8b-8192-tool-use-preview": {"input": 0.00019, "output": 0.00019},
|
41 |
+
"qwen-2.5-coder-32b": {"input": 0.0015, "output": 0.003}
|
42 |
}
|
43 |
}
|
44 |
|
|
|
140 |
"claude-3-opus-latest",
|
141 |
"claude-3-7-sonnet-latest",
|
142 |
"claude-3-5-sonnet-latest",
|
143 |
+
"qwen-2.5-coder-32b",
|
144 |
"claude-3-5-haiku-latest",
|
145 |
"deepseek-r1-distill-qwen-32b",
|
146 |
"deepseek-r1-distill-llama-70b",
|
src/agents/agents.py
CHANGED
@@ -3,6 +3,9 @@ import src.agents.memory_agents as m
|
|
3 |
import asyncio
|
4 |
from concurrent.futures import ThreadPoolExecutor
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
class analytical_planner(dspy.Signature):
|
8 |
# The planner agent which routes the query to Agent(s)
|
@@ -461,26 +464,7 @@ class auto_analyst(dspy.Module):
|
|
461 |
for i, a in enumerate(agents):
|
462 |
name = a.__pydantic_core_schema__['schema']['model_name']
|
463 |
self.agents[name] = dspy.ChainOfThought(a)
|
464 |
-
|
465 |
-
agent_str = str(agents[i].__pydantic_core_schema__['cls'])
|
466 |
-
try:
|
467 |
-
# Handle cases when the string format might change or vary
|
468 |
-
if '(' in agent_str:
|
469 |
-
# Extract the part between ( and the first -> ORR )
|
470 |
-
params_part = agent_str.split('(', 1)[1]
|
471 |
-
if '->' in params_part:
|
472 |
-
params_part = params_part.split('->', 1)[0]
|
473 |
-
if ')' in params_part:
|
474 |
-
params_part = params_part.split(')', 1)[0]
|
475 |
-
|
476 |
-
# Split by comma and clean up
|
477 |
-
self.agent_inputs[name] = {x.strip() for x in params_part.split(',') if x.strip()}
|
478 |
-
else:
|
479 |
-
# Default empty set if no parameters found
|
480 |
-
self.agent_inputs[name] = set()
|
481 |
-
except (IndexError, Exception):
|
482 |
-
# Fallback to empty set if parsing fails
|
483 |
-
self.agent_inputs[name] = set()
|
484 |
self.agent_desc.append(str(a.__pydantic_core_schema__['cls']))
|
485 |
|
486 |
# Initialize coordination agents
|
@@ -551,21 +535,21 @@ class auto_analyst(dspy.Module):
|
|
551 |
# Execute code combiner after all agents complete
|
552 |
code_list = [result['code'] for _, result in completed_results if 'code' in result]
|
553 |
try:
|
554 |
-
with dspy.settings.context(lm=dspy.
|
555 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
556 |
-
yield '
|
557 |
except:
|
558 |
try:
|
559 |
-
with dspy.settings.context(lm=dspy.
|
560 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
561 |
-
yield '
|
562 |
except:
|
563 |
try:
|
564 |
-
with dspy.settings.context(lm=dspy.GROQ(model="
|
565 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
566 |
-
yield '
|
567 |
-
except:
|
568 |
-
yield '
|
569 |
|
570 |
# Agent to make a Chat history name from a query
|
571 |
class chat_history_name_agent(dspy.Signature):
|
|
|
3 |
import asyncio
|
4 |
from concurrent.futures import ThreadPoolExecutor
|
5 |
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
|
10 |
class analytical_planner(dspy.Signature):
|
11 |
# The planner agent which routes the query to Agent(s)
|
|
|
464 |
for i, a in enumerate(agents):
|
465 |
name = a.__pydantic_core_schema__['schema']['model_name']
|
466 |
self.agents[name] = dspy.ChainOfThought(a)
|
467 |
+
self.agent_inputs[name] = {x.strip() for x in str(agents[i].__pydantic_core_schema__['cls']).split('->')[0].split('(')[1].split(',')}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
468 |
self.agent_desc.append(str(a.__pydantic_core_schema__['cls']))
|
469 |
|
470 |
# Initialize coordination agents
|
|
|
535 |
# Execute code combiner after all agents complete
|
536 |
code_list = [result['code'] for _, result in completed_results if 'code' in result]
|
537 |
try:
|
538 |
+
with dspy.settings.context(lm=dspy.GROQ(model="qwen-2.5-coder-32b", max_tokens=12000, temperature=1.0, api_key=os.getenv("GROQ_API_KEY"))):
|
539 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
540 |
+
yield 'code_combiner_agent__qwen', str(code_list), dict(combiner_result)
|
541 |
except:
|
542 |
try:
|
543 |
+
with dspy.settings.context(lm=dspy.LM(model="anthropic/claude-3-7-sonnet-latest", max_tokens=12000, temperature=1.0)):
|
544 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
545 |
+
yield 'code_combiner_agent__claude', str(code_list), dict(combiner_result)
|
546 |
except:
|
547 |
try:
|
548 |
+
with dspy.settings.context(lm=dspy.GROQ(model="deepseek-r1-distill-llama-70b", max_tokens=12000, temperature=1.0, api_key=os.getenv("GROQ_API_KEY"))):
|
549 |
combiner_result = self.code_combiner_agent(agent_code_list=str(code_list), dataset=dict_['dataset'])
|
550 |
+
yield 'code_combiner_agent__deepseek', str(code_list), dict(combiner_result)
|
551 |
+
except Exception as e:
|
552 |
+
yield 'code_combiner_agent__none', str(code_list), {"error": "Error in code combiner: "+str(e)}
|
553 |
|
554 |
# Agent to make a Chat history name from a query
|
555 |
class chat_history_name_agent(dspy.Signature):
|
src/managers/ai_manager.py
CHANGED
@@ -52,7 +52,8 @@ costs = {
|
|
52 |
"gemma-7b-it": {"input": 0.00007, "output": 0.00007},
|
53 |
"gemma2-9b-it": {"input": 0.0002, "output": 0.0002},
|
54 |
"llama3-groq-70b-8192-tool-use-preview": {"input": 0.00089, "output": 0.00089},
|
55 |
-
"llama3-groq-8b-8192-tool-use-preview": {"input": 0.00019, "output": 0.00019}
|
|
|
56 |
}
|
57 |
}
|
58 |
|
|
|
52 |
"gemma-7b-it": {"input": 0.00007, "output": 0.00007},
|
53 |
"gemma2-9b-it": {"input": 0.0002, "output": 0.0002},
|
54 |
"llama3-groq-70b-8192-tool-use-preview": {"input": 0.00089, "output": 0.00089},
|
55 |
+
"llama3-groq-8b-8192-tool-use-preview": {"input": 0.00019, "output": 0.00019},
|
56 |
+
"qwen-2.5-coder-32b": {"input": 0.0015, "output": 0.003}
|
57 |
}
|
58 |
}
|
59 |
|
src/managers/chat_manager.py
CHANGED
@@ -47,9 +47,29 @@ class ChatManager:
|
|
47 |
"claude-3-haiku-20240307": {"input": 0.00025, "output": 0.00125},
|
48 |
"claude-3-5-sonnet-latest": {"input": 0.003, "output": 0.015},
|
49 |
# Groq models
|
50 |
-
"
|
51 |
-
"
|
52 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|
54 |
|
55 |
# Add model providers mapping
|
|
|
47 |
"claude-3-haiku-20240307": {"input": 0.00025, "output": 0.00125},
|
48 |
"claude-3-5-sonnet-latest": {"input": 0.003, "output": 0.015},
|
49 |
# Groq models
|
50 |
+
"deepseek-r1-distill-qwen-32b": {"input": 0.00075, "output": 0.00099},
|
51 |
+
"deepseek-r1-distill-llama-70b": {"input": 0.00075, "output": 0.00099},
|
52 |
+
"llama-3.3-70b-versatile": {"input": 0.00059, "output": 0.00079},
|
53 |
+
"llama-3.3-70b-specdec": {"input": 0.00059, "output": 0.00099},
|
54 |
+
"llama2-70b-4096": {"input": 0.0007, "output": 0.0008},
|
55 |
+
"llama3-8b-8192": {"input": 0.00005, "output": 0.00008},
|
56 |
+
"llama-3.2-1b-preview": {"input": 0.00004, "output": 0.00004},
|
57 |
+
"llama-3.2-3b-preview": {"input": 0.00006, "output": 0.00006},
|
58 |
+
"llama-3.2-11b-text-preview": {"input": 0.00018, "output": 0.00018},
|
59 |
+
"llama-3.2-11b-vision-preview": {"input": 0.00018, "output": 0.00018},
|
60 |
+
"llama-3.2-90b-text-preview": {"input": 0.0009, "output": 0.0009},
|
61 |
+
"llama-3.2-90b-vision-preview": {"input": 0.0009, "output": 0.0009},
|
62 |
+
"llama3-70b-8192": {"input": 0.00059, "output": 0.00079},
|
63 |
+
"llama-3.1-8b-instant": {"input": 0.00005, "output": 0.00008},
|
64 |
+
"llama-3.1-70b-versatile": {"input": 0.00059, "output": 0.00079},
|
65 |
+
"llama-3.1-405b-reasoning": {"input": 0.00059, "output": 0.00079},
|
66 |
+
"mixtral-8x7b-32768": {"input": 0.00024, "output": 0.00024},
|
67 |
+
"gemma-7b-it": {"input": 0.00007, "output": 0.00007},
|
68 |
+
"gemma2-9b-it": {"input": 0.0002, "output": 0.0002},
|
69 |
+
"llama3-groq-70b-8192-tool-use-preview": {"input": 0.00089, "output": 0.00089},
|
70 |
+
"llama3-groq-8b-8192-tool-use-preview": {"input": 0.00019, "output": 0.00019},
|
71 |
+
"qwen-2.5-coder-32b": {"input": 0.0015, "output": 0.003}
|
72 |
+
|
73 |
}
|
74 |
|
75 |
# Add model providers mapping
|