Spaces:
Runtime error
Runtime error
File size: 46,526 Bytes
f4c5608 616afbc 28f11ce 025d333 ff33505 3757f89 05a7853 1df8046 0446082 ff33505 8e9804e af6f39f ff33505 1df8046 0446082 1df8046 69cbcdc 1df8046 025d333 1df8046 025d333 1df8046 69cbcdc 1df8046 025d333 1df8046 025d333 1df8046 9a97bef 025d333 0446082 69cbcdc 0446082 69cbcdc 0446082 3aca5a7 025d333 1df8046 b8166be 1df8046 ff33505 1df8046 28f11ce f4c5608 8b8f8c6 025d333 ff33505 025d333 ff33505 bd9069b 46f84a2 bd9069b 28f11ce bd9069b ff33505 0446082 1df8046 0446082 6bd39eb 025d333 6bd39eb ff33505 0446082 3757f89 0446082 ff33505 3757f89 0446082 accbe57 ff33505 025d333 ff33505 eb0dac7 28f11ce ff33505 accbe57 ff33505 0446082 3757f89 0446082 3757f89 0446082 37a3bd4 0446082 8d9f639 b645eee 8d9f639 0446082 37a3bd4 ff33505 0446082 1df8046 f4c5608 05a7853 f4c5608 05a7853 f4c5608 05a7853 69cbcdc f4c5608 69cbcdc f4c5608 69cbcdc f4c5608 69cbcdc 1df8046 ff33505 73cc22a ff33505 b8166be ff33505 b8166be ff33505 b8166be ff33505 73cc22a ff33505 69cbcdc 025d333 3757f89 ff33505 2a9812a 3757f89 2a9812a 0446082 ff33505 0446082 ff33505 217d90f b0b5c53 ff33505 b0b5c53 4ee4b24 ff33505 b0b5c53 4ee4b24 b0b5c53 4ee4b24 b0b5c53 ff33505 b0b5c53 4ee4b24 ff33505 285d72c 217d90f 96393a7 8338bd9 96393a7 af6f39f 27a6243 8e9804e 96393a7 8e9804e 96393a7 8e9804e 550edf4 3757f89 8e9804e 96393a7 8e9804e 96393a7 8e9804e 550edf4 8e9804e 8338bd9 8e9804e 96393a7 8e9804e 550edf4 8e9804e 96393a7 8e9804e 96393a7 8338bd9 8e9804e 96393a7 8e9804e 550edf4 8e9804e 96393a7 8e9804e 96393a7 8e9804e 28f11ce 8e9804e 28f11ce 8e9804e 96393a7 8e9804e 28f11ce 96393a7 8e9804e 96393a7 8e9804e 28f11ce 8e9804e 96393a7 28f11ce 96393a7 8e9804e 28f11ce 96393a7 8338bd9 96393a7 550edf4 96393a7 68997b5 9a97bef 68997b5 9a97bef 68997b5 8338bd9 68997b5 28f11ce 96393a7 28f11ce 96393a7 56cc93b 28f11ce 96393a7 8e9804e 96393a7 28f11ce 8e9804e 28f11ce af6f39f 28f11ce af6f39f 28f11ce 96393a7 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce 550edf4 28f11ce 68997b5 af6f39f 68997b5 af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce 550edf4 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 28f11ce 550edf4 28f11ce af6f39f 28f11ce af6f39f 28f11ce af6f39f 0872dd8 28f11ce af6f39f 0872dd8 af6f39f 28f11ce 0872dd8 af6f39f 28f11ce af6f39f 28f11ce af6f39f 0872dd8 af6f39f 28f11ce 550edf4 28f11ce af6f39f 28f11ce 68997b5 0872dd8 af6f39f 0872dd8 af6f39f 28f11ce 56cc93b 28f11ce af6f39f 28f11ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | import ast
import csv
import re
from typing import Any, Dict
from common_functions_v4 import *
from prompt_configs import PROMPTS, ModelType
from config_classes import *
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
import openai
from contextlib import asynccontextmanager, contextmanager
import json
from sample_permutations import *
from io import StringIO
import pandas as pd
@contextmanager
def openai_session():
"""Context manager to properly handle OpenAI API sessions"""
try:
client = openai.OpenAI()
yield client
finally:
if hasattr(client, 'close'):
client.close()
@asynccontextmanager
async def async_openai_session():
"""Context manager to properly handle OpenAI API sessions"""
try:
client = openai.AsyncOpenAI()
yield client
finally:
if hasattr(client, 'close'):
client.close()
@with_langtrace_root_span()
def call_o1_mini(prompt):
print(f"calling o1-mini with prompt: {prompt[:500]}")
with openai_session() as client:
try:
response = client.chat.completions.create(
model="o1-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
except Exception as e:
return f"Error generating output: {str(e)}"
@with_langtrace_root_span()
def call_4o_mini(prompt):
print(f"calling 4o-mini with prompt: {prompt[:500]}")
with openai_session() as client:
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
except Exception as e:
return f"Error generating output: {str(e)}"
@with_langtrace_root_span()
async def async_call_o1_mini(prompt):
print(f"calling o1-mini with prompt: {prompt[:500]}")
async with async_openai_session() as client:
try:
response = await client.chat.completions.create(
model="o1-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
except Exception as e:
return f"Error generating output: {str(e)}"
@with_langtrace_root_span()
async def async_call_4o_mini(prompt):
print(f"[DEBUG] Entering async_call_4o_mini")
print(f"[DEBUG] Prompt received: {prompt[:500]}")
async with async_openai_session() as client:
print(f"[DEBUG] OpenAI client session established")
try:
print(f"[DEBUG] Attempting to create chat completion")
# Create completion without await since it returns a regular object
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
print(f"[DEBUG] Chat completion successful")
print(f"[DEBUG] Response content: {response.choices[0].message.content}")
return response.choices[0].message.content
except Exception as e:
print(f"[DEBUG] Error occurred: {str(e)}")
print(f"[DEBUG] Error type: {type(e)}")
return f"Error generating output: {str(e)}"
finally:
print("[DEBUG] Exiting async_call_4o_mini")
class Project:
def __init__(self, project_type, session_id=None):
self.project_type = project_type
self.session_id = session_id
self.rubric = []
self.rubric_section_names = []
self.component_list = []
self.project_detail = []
# Add these new attributes
self.mandays_results = []
self.mvp_mandays_results = []
self._db_config = {}
# Initialize all prompt outputs as attributes
for config in PROMPTS.values():
for output in config.outputs:
setattr(self, output, "")
# Input mapping for prompt execution
INPUT_MAPPINGS = {
'project_detail': lambda self: self.get_project_detail(),
'generated_prd': lambda self: self.generated_prd,
'configuration_type': lambda self: self.configuration_type,
'project_detail': lambda self: self.project_detail,
'follow_up_questions': lambda self: self.follow_up_questions,
'requirements_rubric': lambda self: self.requirements_rubric,
'generated_engage_follow_up_questions': lambda self: self.generated_engage_follow_up_questions,
'generated_page_follow_up_questions': lambda self: self.generated_page_follow_up_questions,
'generated_engage_further_follow_up_questions': lambda self: self.generated_engage_further_follow_up_questions,
'generated_page_further_follow_up_questions': lambda self: self.generated_page_further_follow_up_questions,
'generated_intent_list': lambda self: self.generated_intent_list,
'generated_plan_test_components': lambda self: self.generated_plan_test_components,
'generated_page_dev_components': lambda self: self.generated_page_dev_components,
'generated_engage_dev_components': lambda self: self.generated_engage_dev_components,
'reformatted_dev_components': lambda self: self.reformatted_dev_components,
'generated_intents_csv': lambda self: self.generated_intents_csv,
'generated_plan_test_mandays': lambda self: self.generated_plan_test_mandays,
'generated_dev_mandays': lambda self: self.generated_dev_mandays,
'generated_mvp_prd': lambda self: self.generated_mvp_prd,
'combined_cost_summary': lambda self: self.combined_cost_summary,
'generated_BD_SOW': lambda self: self.generated_BD_SOW,
'generated_Tech_SOW': lambda self: self.generated_Tech_SOW,
'identified_planning_testing_components': lambda self: self.identified_planning_testing_components,
'identified_development_components': lambda self: self.identified_development_components,
'identified_mvp_intents': lambda self: self.identified_mvp_intents,
'identified_priority_components': lambda self: self.identified_priority_components,
'revised_mandays_estimates': lambda self: self.revised_mandays_estimates,
'generated_MVP_mandays': lambda self: self.generated_MVP_mandays,
}
def _build_prompt(self, config, input_variables):
"""Build the prompt string from config and input variables"""
formatted_inputs_list = []
for key in config.inputs:
value = input_variables[key]
if isinstance(value, list):
value = "".join(value)
formatted_inputs_list.append("# " + str(key) + ":\n" + str(value) + "\n")
formatted_inputs = " ".join(formatted_inputs_list)
return f"""
{config.prompt}
{formatted_inputs}
"""
def _validate_and_fill_inputs(self, config, input_variables):
"""Validate and auto-fill input variables"""
input_variables = input_variables or {}
# Auto-fill inputs from project attributes using INPUT_MAPPINGS
for input_name in config.inputs:
if input_name not in input_variables:
if input_name not in self.INPUT_MAPPINGS:
raise ValueError(f"No mapping defined for required input: {input_name}")
try:
input_variables[input_name] = self.INPUT_MAPPINGS[input_name](self)
except Exception as e:
raise ValueError(f"Failed to get value for input {input_name}: {str(e)}")
# Validate all required inputs are available and not empty
missing_inputs = []
for input_name in config.inputs:
if input_name not in input_variables or not input_variables[input_name]:
missing_inputs.append(input_name)
if missing_inputs:
raise ValueError(f"Missing or empty required inputs: {missing_inputs}")
return input_variables
def _store_outputs(self, config, result):
"""Store outputs in project attributes"""
for output in config.outputs:
if hasattr(self, output):
setattr(self, output, result)
print(f"Stored output {output} with value: {result}")
def execute_prompt(self, prompt_name: str, input_variables: Dict[str, Any] = None) -> str:
"""Execute a prompt with given input variables synchronously"""
print(f"Attempting to execute prompt: {prompt_name}")
# if prompt_name not in PROMPTS:
if prompt_name not in self._db_config:
raise ValueError(f"Unknown prompt: {prompt_name}")
# config = PROMPTS[prompt_name]
config = self._db_config[prompt_name]
input_variables = self._validate_and_fill_inputs(config, input_variables)
prompt = self._build_prompt(config, input_variables)
print(f"Final prompt to be executed: {prompt}")
# Execute prompt with appropriate model
result = (
call_4o_mini(prompt)
if config.model == ModelType.GPT_4O_MINI
else call_o1_mini(prompt)
)
print(f"Result from executing prompt: {result[:800]}")
self._store_outputs(config, result)
return result
async def async_execute_prompt(self, prompt_name: str, input_variables: Dict[str, Any] = None) -> str:
"""Execute a prompt with given input variables asynchronously"""
print(f"Attempting to execute prompt: {prompt_name}")
# if prompt_name not in PROMPTS:
if prompt_name not in self._db_config:
raise ValueError(f"Unknown prompt: {prompt_name}")
# config = PROMPTS[prompt_name]
config = self._db_config[prompt_name]
input_variables = self._validate_and_fill_inputs(config, input_variables)
prompt = self._build_prompt(config, input_variables)
# print(f"Final prompt to be executed: {prompt}")
# Execute prompt with appropriate model
result = (
await async_call_o1_mini(prompt)
if config.model == ModelType.O1_MINI
else await async_call_4o_mini(prompt)
)
# print(f"Result from executing prompt: {result[:800]}")
self._store_outputs(config, result)
return result
def get_db_config(self):
"""Get the configuration fetched from the database"""
return self._db_config
def set_db_config(self, value):
"""Set the configuration fetched from the database"""
self._db_config = value
def load_config_from_db(self):
"""Load and parse the latest configuration from the database"""
try:
raw_config = get_latest_prompt_from_db()
parser = ConfigParser(raw_config)
parsed_config = parser.parse_config()
self._db_config = parsed_config
if parsed_config:
msg = f"Successfully reloaded {len(parsed_config)} prompts from database"
else:
msg = "No prompts found in database config"
print(msg)
return msg
except Exception as e:
print(f"Error loading config from database: {str(e)}")
raise
#Functions to interact with common_functions_v4.py#
def set_rubric(self, rubric_list):
"""Set the rubric for the project"""
self.rubric = rubric_list
def set_rubric_section_names(self, section_names):
"""Set the rubric section names for the project"""
self.rubric_section_names = section_names
def set_component_list(self, component_list):
"""Set the component list for the project"""
self.component_list = component_list
def get_project_detail(self):
"""Get the project details as a formatted string"""
return "\n".join(self.project_detail) if self.project_detail else ""
def add_project_detail(self, detail):
"""Add a new project detail"""
if detail:
self.project_detail.append(detail)
def reset_project(self):
"""Reset all project attributes"""
print("Resetting project")
self.project_detail = []
self.load_config_from_db()
# for config in PROMPTS.values():
for config in self._db_config.values():
for output in config.outputs:
setattr(self, output, "")
async def generate_client_initial_question(self):
"""Generate follow-up questions after initial client response"""
# return PROMPTS["client_initial_question"].prompt
return self._db_config["client_initial_question"].prompt
async def generate_client_follow_up(self):
"""Generate follow-up questions after initial client response"""
return await self.async_execute_prompt(
"generate_client_follow_up",
{
"project_detail": self.get_project_detail()
}
)
#TODO: To change
async def gather_project_input(self, prompt_name):
"""Generate context-aware questions to gather project requirements"""
return await self.async_execute_prompt(
f"{prompt_name}",
{
"project_detail": self.get_project_detail()
}
)
async def generate_general_questions(self):
"""Review project input and generate general deployment / intergretion questions to address gaps"""
return await self.async_execute_prompt(
"generate_general_questions",
{
"project_detail": self.get_project_detail()
}
)
async def generate_further_follow_up_questions(self):
"""Review project input and generate follow-up questions to address gaps"""
return await self.async_execute_prompt(
"generate_further_follow_up_questions",
{
"project_detail": self.get_project_detail()
}
)
##########################################################
def _parse_json_response(self, response: str) -> Any:
try:
# If response is not a string, return as-is
if not isinstance(response, str):
return response
# Extract JSON from code blocks if present
if "```json" in response:
response = response.split("```json")[1].split("```")[0].strip()
elif "```" in response:
response = response.split("```")[1].split("```")[0].strip()
return json.loads(response)
except json.JSONDecodeError:
return response
def _get_input_from_previous_results(self) -> Any:
"""Get input value from previous results"""
# For step_1 results (PRD generation))
return None
## Generate PRD and components from project details ##
def generate_prd_and_components(self, progress=gr.Progress()):
"""Generate PRD and components from project details, streaming results"""
results = []
# Generate PRD
yield "Generating PRD...", results
prd_response = self.execute_prompt(
"generate_prd",
{"project_detail": self.get_project_detail()}
)
log_prompt(PROMPTS['generate_prd'].step,
PROMPTS['generate_prd'].description,
PROMPTS["generate_prd"].prompt,
prd_response)
# log_prompt(self._db_config["generate_prd"].step,
# self._db_config["generate_prd"].description,
# self._db_config["generate_prd"].prompt,
# prd_response)
# Parse and format the PRD response
try:
prd_json = self._parse_json_response(prd_response)
# Extract PRD content
if isinstance(prd_json, dict):
if "detailed_breakdown" in prd_json:
self.generated_prd = prd_json["detailed_breakdown"]
formatted_prd = {
"function_name": "generate_prd",
"result": {
"detailed_breakdown": prd_json["detailed_breakdown"],
"summary": prd_json.get("summary", "")
}
}
else:
self.generated_prd = prd_json
formatted_prd = {
"function_name": "generate_prd",
"result": prd_json
}
else:
self.generated_prd = str(prd_json)
formatted_prd = {
"function_name": "generate_prd",
"result": str(prd_json)
}
# Add formatted PRD to results
results.append(formatted_prd)
yield "PRD generation complete", results
except Exception as e:
print(f"Warning: Could not parse PRD: {str(e)}")
self.generated_prd = prd_response
results.append({
"function_name": "generate_prd",
"result": prd_response
})
yield "PRD generation complete", results
try:
yield "Analyzing configuration with component agent...", results
configuration_output = self.execute_prompt(
"component_agent",
{"generated_prd": self.generated_prd}
)
log_prompt(PROMPTS['component_agent'].step,
PROMPTS['component_agent'].description,
PROMPTS["component_agent"].prompt,
configuration_output)
# Parse and format configuration output
try:
config = self._parse_json_response(configuration_output)
self.config = config
formatted_config = {
"function_name": "component_agent",
"result": json.dumps(config, indent=2)
}
results.append(formatted_config)
selected_functions = config[0]["selected_functions"]
yield f"Selected {len(selected_functions)} components to generate", results
except (KeyError, IndexError) as e:
yield f"Warning: Could not parse configuration output ({str(e)})", results
return
except Exception as e:
yield f"Error in analyzing configuration: {str(e)}", results
return
# Execute each function and stream results
for i, function_name in enumerate(selected_functions, 1):
try:
yield f"Generating component {i}/{len(selected_functions)}: {function_name}...", results
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Format the component result
try:
parsed_result = self._parse_json_response(result)
formatted_result = {
"function_name": function_name,
"result": json.dumps(parsed_result, indent=2) if isinstance(parsed_result, (dict, list)) else str(parsed_result)
}
results.append(formatted_result)
yield f"Successfully generated {function_name}", results
except Exception as e:
print(f"Warning: Error formatting result for {function_name}: {str(e)}")
results.append({
"function_name": function_name,
"result": str(result)
})
yield f"Generated {function_name} (raw format)", results
except Exception as e:
yield f"Error executing {function_name}: {str(e)}", results
continue
yield "All components generated successfully!", results
def generate_mandays_estimate(self, progress=gr.Progress()):
"""Generate mandays estimation based on configuration type and selected functions, streaming results"""
results = []
try:
if not hasattr(self, 'config'):
yield "Configuration not found. Please run 'generate_prd_and_components' first.", results , None
return
config = self.config
configuration_type = config[0]["configuration_type"]
yield f"Configuration type detected: {configuration_type}", results , None
# Map configuration type to enum
try:
config_enum = ConfigurationType(configuration_type)
except ValueError:
yield f"Unsupported configuration type: {configuration_type}", results , None
return
# Get functions to execute based on configuration type
functions_to_execute = CONFIGURATION_TYPE_FUNCTIONS.get(config_enum, [])
if not functions_to_execute:
yield f"No functions defined for configuration type: {configuration_type}", results , None
return
# Execute each function and stream results
for function_name in functions_to_execute:
try:
yield f"Executing {function_name}...", results , None
# Execute the prompt with gathered input variables
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Process CSV sections if there's a section break
if "----SECTION BREAK----" in result:
sections = result.split("----SECTION BREAK----")
sections = [section.strip().replace('```csv', '').replace('```', '') for section in sections]
processed_result = {}
doc_csv = StringIO(sections[0].strip())
try:
doc_df = pd.read_csv(doc_csv, keep_default_na=False)
except pd.errors.ParserError as e:
print(f"Error processing Document Extraction CSV: {str(e)}")
continue
chat_csv = StringIO(sections[1].strip())
try:
chat_df = pd.read_csv(chat_csv, keep_default_na=False)
except pd.errors.ParserError as e:
print(f"Error processing Chatbot CSV: {str(e)}")
continue
processed_result = {
f"{function_name}": pd.concat([
doc_df.assign(section='Document Extraction'),
chat_df.assign(section='Chatbot')
]).to_dict('records')
}
else:
# Single CSV processing with error handling
try:
# Clean up the CSV data
clean_result = (result
.replace('```csv', '')
.replace('```', '')
.strip()
.replace('\r\n', '\n')
.replace('\r', '\n')
)
csv_data = StringIO(clean_result)
df = pd.read_csv(csv_data,
keep_default_na=False,
quoting=csv.QUOTE_ALL,
escapechar='\\',
on_bad_lines='warn'
)
processed_result = {
f"{function_name}": df.to_dict('records')
}
except Exception as e:
print(f"Error processing CSV: {str(e)}")
continue
# Format and store results
formatted_result = {
"function_name": function_name,
"result": processed_result
}
results.append(formatted_result)
yield f"Successfully completed {function_name}", results , None
except Exception as e:
print(f"Error executing {function_name}: {str(e)}")
yield f"Error in {function_name}: {str(e)}", results , None
continue
total_mandays, total_cost, estimated_months = calculate_mandays_and_costs(results)
general_cost_summary = f"""Original Estimate:
Total Mandays: {total_mandays:.2f}
Total Cost: ${total_cost:,.2f}
({estimated_months:.2f} months)"""
self.general_cost_summary = general_cost_summary
# Store the results for later recalculation
self.mandays_results = results
yield "Mandays estimation completed!", results, general_cost_summary
except Exception as e:
print(f"Error in generate_mandays_estimate: {str(e)}")
yield f"Error during mandays estimation: {str(e)}", results , None
def analyze_mvp_components(self, progress=gr.Progress()):
"""Analyze MVP components based on configuration type and selected functions, streaming results"""
results = []
try:
if not hasattr(self, 'config'):
yield "Configuration not found. Please run 'generate_prd_and_components' first.", results
return
config = self.config
configuration_type = config[0]["configuration_type"]
yield f"Configuration type detected: {configuration_type}", results
# Map configuration type to enum
try:
config_enum = ConfigurationType(configuration_type)
except ValueError:
yield f"Unsupported configuration type: {configuration_type}", results
return
# Get functions to execute based on configuration type
functions_to_execute = ANALYZE_COMPONENTS_FUNCTIONS.get(config_enum, [])
if not functions_to_execute:
yield f"No functions defined for configuration type: {configuration_type}", results
return
# Execute each function and stream results
for function_name in functions_to_execute:
try:
yield f"Executing {function_name}...", results
# Execute the prompt with gathered input variables
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Clean up the CSV data
clean_result = (result
.replace('```csv', '')
.replace('```', '')
.strip()
.replace('\r\n', '\n')
.replace('\r', '\n')
)
csv_data = StringIO(clean_result)
df = pd.read_csv(csv_data,
keep_default_na=False,
quoting=csv.QUOTE_ALL,
escapechar='\\',
on_bad_lines='warn'
)
processed_result = {
f"{function_name}": df.to_dict('records')
}
# Format and store results
formatted_result = {
"function_name": function_name,
"result": processed_result
}
results.append(formatted_result)
yield f"Successfully completed {function_name}", results
except Exception as e:
print(f"Error executing {function_name}: {str(e)}")
yield f"Error in {function_name}: {str(e)}", results
continue
except Exception as e:
print(f"Error in analyzing_mvp_components: {str(e)}")
yield f"Error during mvp components analysis: {str(e)}", results
def recalculate_mvp_mandays(self, progress=gr.Progress()):
"""Recalculate MVP Mandays based on configuration type and selected functions, streaming results"""
results = []
try:
if not hasattr(self, 'config'):
yield "Configuration not found. Please run 'generate_prd_and_components' first.", results
return
config = self.config
configuration_type = config[0]["configuration_type"]
yield f"Configuration type detected: {configuration_type}", results
# Map configuration type to enum
try:
config_enum = ConfigurationType(configuration_type)
except ValueError:
yield f"Unsupported configuration type: {configuration_type}", results
return
# Get functions to execute based on configuration type
functions_to_execute = RECALCULATE_MVP_MANDAYS_FUNCTIONS.get(config_enum, [])
if not functions_to_execute:
yield f"No functions defined for configuration type: {configuration_type}", results
return
# Execute each function and stream results
for function_name in functions_to_execute:
try:
yield f"Executing {function_name}...", results
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Format the component result
try:
formatted_result = {
"function_name": function_name,
"result": result
}
results.append(formatted_result)
yield f"Successfully generated {function_name}", results
except Exception as e:
print(f"Warning: Error formatting result for {function_name}: {str(e)}")
results.append({
"function_name": function_name,
"result": str(result)
})
yield f"Generated {function_name}", results
except Exception as e:
yield f"Error executing {function_name}: {str(e)}", results
continue
except Exception as e:
print(f"Error in recalculate_mvp_mandays: {str(e)}")
yield f"Error during mvp components analysis: {str(e)}", results
yield "Analysis completed", results
def generate_mvp_mandays(self, progress=gr.Progress()):
"""Generate MVP Mandays based on configuration type and selected functions, streaming results"""
results =[]
yield "Generating MVP Mandays...", results , None
try:
if not hasattr(self, 'config'):
yield "Configuration not found.", [] , None
return
config = self.config
configuration_type = config[0]["configuration_type"]
yield f"Configuration type detected: {configuration_type}", [] , None
# Map configuration type to enum
try:
config_enum = ConfigurationType(configuration_type)
except ValueError:
yield f"Unsupported configuration type: {configuration_type}", [] , None
return
# Get functions to execute based on configuration type
functions_to_execute = GENERATE_MVP_MANDAYS_FUNCTIONS.get(config_enum, [])
if not functions_to_execute:
yield f"No functions defined for configuration type: {configuration_type}", [] , None
return
for function_name in functions_to_execute:
try:
yield f"Executing {function_name}...", results , None
# Execute the prompt with gathered input variables
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Process CSV sections if there's a section break
if "----SECTION BREAK----" in result:
sections = result.split("----SECTION BREAK----")
# Clean up the CSV data before parsing
sections = [section.strip().replace('```csv', '').replace('```', '') for section in sections]
processed_result = {}
# Process each section with error handling
for i, section in enumerate(sections):
try:
clean_section = (section
.replace('\r\n', '\n')
.replace('\r', '\n')
)
csv_data = StringIO(clean_section)
df = pd.read_csv(csv_data,
keep_default_na=False,
quoting=csv.QUOTE_ALL,
escapechar='\\',
on_bad_lines='warn'
)
# Use appropriate section names based on function and index
if function_name == "generate_page_MVP_mandays":
section_name = 'MVP Plan_Test' if i == 0 else 'MVP Dev'
elif function_name == "generate_engage_MVP_mandays":
section_name = 'MVP Plan_Test' if i == 0 else ('MVP Dev' if i == 1 else 'MVP Intents')
else:
# Default section names if function is not recognized
section_name = f'Section_{i}'
# Convert mandays column to float and handle any non-numeric values
df['mandays'] = pd.to_numeric(df['mandays'], errors='coerce').fillna(0)
processed_result[section_name] = df.to_dict('records')
except Exception as e:
print(f"Error processing section {i}: {str(e)}")
processed_result[f'section_{i}_error'] = str(e)
# Format and store results
formatted_result = {
"function_name": function_name,
"result": processed_result
}
results.append(formatted_result)
yield f"Successfully completed {function_name}", results, None
except Exception as e:
print(f"Error executing {function_name}: {str(e)}")
yield f"Error in {function_name}: {str(e)}", results, None
continue
total_mvp_mandays, total_mvp_cost, estimated_mvp_months = calculate_mvp_mandays_and_costs(results)
mvp_cost_summary = f"""MVP Estimate:
Total Mandays: {total_mvp_mandays:.2f}
Total Cost: ${total_mvp_cost:,.2f}
({estimated_mvp_months:.2f} months)"""
self.mvp_cost_summary = mvp_cost_summary
if hasattr(self, 'general_cost_summary'):
self.combined_cost_summary = f"""
{self.general_cost_summary}
{mvp_cost_summary}"""
else:
self.combined_cost_summary = mvp_cost_summary
# Store the results for later recalculation
self.mvp_mandays_results = results
yield "MVP Mandays generation completed!", results, self.combined_cost_summary
except Exception as e:
print(f"Error in generating MVP mandays: {str(e)}")
yield f"Error in generating MVP mandays: {str(e)}", []
def generate_final_documentation(self, progress=gr.Progress()):
"""Generate Final Documentation based on configuration type and selected functions, streaming results"""
results = []
try:
if not hasattr(self, 'config'):
yield "Configuration not found.", []
return
config = self.config
configuration_type = config[0]["configuration_type"]
yield f"Configuration type detected: {configuration_type}", []
# Map configuration type to enum
try:
config_enum = ConfigurationType(configuration_type)
except ValueError:
yield f"Unsupported configuration type: {configuration_type}", []
return
# Get functions to execute based on configuration type
functions_to_execute = GENERATE_FINAL_DOCUMENT_FUNCTIONS.get(config_enum, [])
if not functions_to_execute:
yield f"No functions defined for configuration type: {configuration_type}", []
return
for function_name in functions_to_execute:
try:
yield f"Executing {function_name}...", results
result = self.execute_prompt(function_name)
log_prompt(PROMPTS[function_name].step,
PROMPTS[function_name].description,
PROMPTS[function_name].prompt,
result)
# Parse JSON with improved handling
try:
if isinstance(result, str):
# Remove any JSON code block markers
clean_result = re.sub(r'```json\s*|\s*```', '', result.strip())
# Parse the JSON
parsed_result = json.loads(clean_result)
# Directly use the values from the parsed JSON as markdown
formatted_result = {
"function_name": function_name,
"result": parsed_result["scope_summary"] + "\n\n" +
parsed_result["modules_and_functional_requirements"] + "\n\n" +
parsed_result["out_of_scope"] + "\n\n" +
parsed_result["system_flow"]
}
else:
formatted_result = {
"function_name": function_name,
"result": result
}
results.append(formatted_result)
except json.JSONDecodeError:
results.append({
"function_name": function_name,
"result": result
})
except Exception as e:
print(f"Error executing {function_name}: {str(e)}")
yield f"Error in {function_name}: {str(e)}", results
continue
yield f"Successfully completed {function_name}", results
yield "Final Documentation Generation completed!", results
except Exception as e:
print(f"Error in generating Final Documentation: {str(e)}")
yield f"Error in generating Final Documentation: {str(e)}", []
def recalculate_mandays_costs(self, progress=gr.Progress()):
"""Recalculate mandays and costs for both original and MVP estimates"""
try:
# Recalculate original estimate
original_results = []
if hasattr(self, 'generated_plan_test_mandays') and self.generated_plan_test_mandays:
try:
plan_test_data = self.generated_plan_test_mandays
if isinstance(plan_test_data, dict):
# Get the configuration type functions from the plan test data
for function_name in plan_test_data.keys():
if function_name.startswith('generate_') and function_name.endswith('_plan_test_mandays'):
original_results.append({
'function_name': function_name,
'result': plan_test_data
})
break
except Exception as e:
print(f"Error processing plan test mandays: {str(e)}")
if hasattr(self, 'generated_dev_mandays') and self.generated_dev_mandays:
try:
dev_data = self.generated_dev_mandays
if isinstance(dev_data, dict) and dev_data.get('generate_dev_mandays'):
original_results.append({
'function_name': 'generate_dev_mandays',
'result': dev_data
})
except Exception as e:
print(f"Error processing dev mandays: {str(e)}")
# Calculate original estimate
total_mandays, total_cost, estimated_months = calculate_mandays_and_costs(original_results)
# Format original estimate summary
self.general_cost_summary = f"""Original Estimate:
Total Mandays: {total_mandays:.2f}
Total Cost: ${total_cost:,.2f}
({estimated_months:.2f} months)"""
# Recalculate MVP estimate
mvp_results = []
if hasattr(self, 'generated_MVP_mandays') and self.generated_MVP_mandays:
try:
mvp_data = self.generated_MVP_mandays
if isinstance(mvp_data, dict):
mvp_results.append({
'function_name': 'generate_MVP_mandays',
'result': mvp_data
})
except Exception as e:
print(f"Error processing MVP mandays: {str(e)}")
# Calculate MVP estimate
total_mvp_mandays, total_mvp_cost, estimated_mvp_months = calculate_mvp_mandays_and_costs(mvp_results)
# Format MVP estimate summary
self.mvp_cost_summary = f"""MVP Estimate:
Total MVP Mandays: {total_mvp_mandays:.2f}
Total MVP Cost: ${total_mvp_cost:,.2f}
({estimated_mvp_months:.2f} months)"""
# Combine both summaries
self.combined_cost_summary = f"""
{self.general_cost_summary}
{self.mvp_cost_summary}"""
return self.combined_cost_summary
except Exception as e:
print(f"Error in recalculating mandays and costs: {str(e)}")
return "Error recalculating estimates"
|