Arslan1997 commited on
Commit
ce0eb39
·
1 Parent(s): 7ebcb78

added latest models

Browse files
Files changed (2) hide show
  1. app/services/agents.py +174 -0
  2. app/services/form_creator.py +104 -10
app/services/agents.py CHANGED
@@ -757,6 +757,63 @@ class FormChatFunction(dspy.Module):
757
 
758
 
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
  # ============================================================================
761
  # FORM GENERATION MODULE
762
  # ============================================================================
@@ -764,9 +821,71 @@ class FormChatFunction(dspy.Module):
764
  class FormGenerationModule(dspy.Module):
765
  """Main module for generating forms from natural language."""
766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767
  def __init__(self):
768
  self.planner = dspy.Predict(FormPlannerSignature)
769
  self.component_generator = dspy.Predict(ComponentSignatureGenerator)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
770
 
771
  async def aforward(self, user_query: str):
772
  """
@@ -826,6 +945,14 @@ class FormGenerationModule(dspy.Module):
826
  logger.error(f"Failed to parse component spec for {comp.get('component_id')}")
827
  continue
828
 
 
 
 
 
 
 
 
 
829
  # Ensure order is preserved
830
  comp_spec['order'] = comp.get('order', len(detailed_components))
831
  detailed_components.append(comp_spec)
@@ -845,6 +972,53 @@ class FormGenerationModule(dspy.Module):
845
  }
846
 
847
  return final_form
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
848
 
849
 
850
  # ============================================================================
 
757
 
758
 
759
 
760
+ # ============================================================================
761
+ # FORM FIXER SIGNATURE
762
+ # ============================================================================
763
+
764
+ class FormFixerSignature(dspy.Signature):
765
+ """
766
+ Fix validation errors in a generated form plan.
767
+
768
+ You are given a form that failed validation along with the specific errors.
769
+ Your job is to fix ONLY the issues mentioned in the validation errors while
770
+ preserving all other valid parts of the form.
771
+
772
+ VALID QUESTION TYPES (use ONLY these exact strings):
773
+ - short_answer: Single line text input
774
+ - long_answer: Multi-line text input
775
+ - multiple_choice: Radio buttons (one selection)
776
+ - checkboxes: Multiple selections allowed
777
+ - dropdown: Single select dropdown
778
+ - multi_select: Multi-select dropdown
779
+ - number: Numeric input
780
+ - email: Email address input
781
+ - phone: Phone number input
782
+ - link: URL input
783
+ - file_upload: File attachment
784
+ - date: Date picker
785
+ - time: Time picker
786
+ - linear_scale: Scale rating (1-10)
787
+ - matrix: Grid questions
788
+ - rating: Star rating
789
+ - payment: Payment field
790
+ - signature: Digital signature
791
+ - ranking: Rank items
792
+ - wallet_connect: Web3 wallet
793
+ - button: Action button
794
+
795
+ COMMON FIXES:
796
+ - "text" → "short_answer"
797
+ - "textarea" → "long_answer"
798
+ - "radio" → "multiple_choice"
799
+ - "checkbox" → "checkboxes"
800
+ - "select" → "dropdown"
801
+ - Missing question_type → add appropriate type based on question_text
802
+ - Missing question_text → generate appropriate text
803
+ - Missing component_id → generate as "comp_N"
804
+
805
+ RULES:
806
+ 1. Fix ONLY the errors mentioned - do not modify valid components
807
+ 2. Preserve all existing valid data (question_text, settings, etc.)
808
+ 3. Output the complete fixed form as valid JSON
809
+ 4. Ensure all components have: component_id, question_type, question_text
810
+ """
811
+ invalid_form = dspy.InputField(desc="The form JSON that failed validation")
812
+ validation_errors = dspy.InputField(desc="List of validation errors to fix")
813
+ user_query = dspy.InputField(desc="Original user request for context")
814
+ fixed_form = dspy.OutputField(desc="Complete fixed form as valid JSON with all errors corrected")
815
+
816
+
817
  # ============================================================================
818
  # FORM GENERATION MODULE
819
  # ============================================================================
 
821
  class FormGenerationModule(dspy.Module):
822
  """Main module for generating forms from natural language."""
823
 
824
+ # Type correction mapping for common AI mistakes
825
+ TYPE_CORRECTIONS = {
826
+ 'text': 'short_answer',
827
+ 'textarea': 'long_answer',
828
+ 'textbox': 'short_answer',
829
+ 'input': 'short_answer',
830
+ 'radio': 'multiple_choice',
831
+ 'radio_button': 'multiple_choice',
832
+ 'radio_buttons': 'multiple_choice',
833
+ 'checkbox': 'checkboxes',
834
+ 'select': 'dropdown',
835
+ 'select_one': 'dropdown',
836
+ 'select_multiple': 'multi_select',
837
+ 'multiselect': 'multi_select',
838
+ 'file': 'file_upload',
839
+ 'upload': 'file_upload',
840
+ 'scale': 'linear_scale',
841
+ 'slider': 'linear_scale',
842
+ 'stars': 'rating',
843
+ 'star_rating': 'rating',
844
+ 'sign': 'signature',
845
+ 'rank': 'ranking',
846
+ 'order': 'ranking',
847
+ 'wallet': 'wallet_connect',
848
+ 'web3': 'wallet_connect',
849
+ 'url': 'link',
850
+ 'website': 'link',
851
+ 'tel': 'phone',
852
+ 'telephone': 'phone',
853
+ 'datetime': 'date',
854
+ 'grid': 'matrix',
855
+ 'table': 'matrix',
856
+ 'numeric': 'number',
857
+ 'integer': 'number',
858
+ 'float': 'number',
859
+ 'submit': 'button',
860
+ 'action': 'button',
861
+ }
862
+
863
+ VALID_TYPES = [
864
+ 'short_answer', 'long_answer', 'multiple_choice', 'checkboxes',
865
+ 'dropdown', 'multi_select', 'number', 'email', 'phone', 'link',
866
+ 'file_upload', 'date', 'time', 'linear_scale', 'matrix', 'rating',
867
+ 'payment', 'signature', 'ranking', 'wallet_connect', 'button'
868
+ ]
869
+
870
  def __init__(self):
871
  self.planner = dspy.Predict(FormPlannerSignature)
872
  self.component_generator = dspy.Predict(ComponentSignatureGenerator)
873
+ self.form_fixer = dspy.Predict(FormFixerSignature)
874
+
875
+ def _correct_question_type(self, question_type: str) -> str:
876
+ """Correct common AI mistakes in question type generation."""
877
+ if not question_type:
878
+ return 'short_answer'
879
+
880
+ normalized = question_type.lower().strip().replace(' ', '_').replace('-', '_')
881
+
882
+ if normalized in self.TYPE_CORRECTIONS:
883
+ return self.TYPE_CORRECTIONS[normalized]
884
+
885
+ if normalized in self.VALID_TYPES:
886
+ return normalized
887
+
888
+ return 'short_answer' # Default fallback
889
 
890
  async def aforward(self, user_query: str):
891
  """
 
945
  logger.error(f"Failed to parse component spec for {comp.get('component_id')}")
946
  continue
947
 
948
+ # Auto-correct question_type if AI generated an invalid type
949
+ if 'question_type' in comp_spec:
950
+ original_type = comp_spec['question_type']
951
+ corrected_type = self._correct_question_type(original_type)
952
+ if corrected_type != original_type:
953
+ logger.info(f"Auto-corrected question_type '{original_type}' -> '{corrected_type}'")
954
+ comp_spec['question_type'] = corrected_type
955
+
956
  # Ensure order is preserved
957
  comp_spec['order'] = comp.get('order', len(detailed_components))
958
  detailed_components.append(comp_spec)
 
972
  }
973
 
974
  return final_form
975
+
976
+ async def fix_form(self, invalid_form: dict, validation_errors: List[str], user_query: str) -> dict:
977
+ """
978
+ Fix validation errors in a form using the FormFixerSignature.
979
+
980
+ Args:
981
+ invalid_form: The form dict that failed validation
982
+ validation_errors: List of validation error messages
983
+ user_query: Original user request for context
984
+
985
+ Returns:
986
+ Fixed form dict, or the original if fixing fails
987
+ """
988
+ try:
989
+ logger.info(f"Attempting to fix form with {len(validation_errors)} validation errors")
990
+
991
+ with dspy.context(lm=dspy.LM("openai/gpt-4o-mini", api_key=os.getenv('OPENAI_API_KEY'), max_tokens=2500)):
992
+ fix_result = self.form_fixer(
993
+ invalid_form=json.dumps(invalid_form),
994
+ validation_errors=json.dumps(validation_errors),
995
+ user_query=user_query
996
+ )
997
+
998
+ fixed_form = fix_result.fixed_form
999
+ if isinstance(fixed_form, str):
1000
+ try:
1001
+ fixed_form = json.loads(fixed_form)
1002
+ except json.JSONDecodeError:
1003
+ logger.error("Failed to parse fixed form JSON")
1004
+ return invalid_form
1005
+
1006
+ # Apply type corrections to all components in the fixed form
1007
+ if 'components' in fixed_form:
1008
+ for comp in fixed_form['components']:
1009
+ if 'question_type' in comp:
1010
+ original_type = comp['question_type']
1011
+ corrected_type = self._correct_question_type(original_type)
1012
+ if corrected_type != original_type:
1013
+ logger.info(f"Post-fix correction: '{original_type}' -> '{corrected_type}'")
1014
+ comp['question_type'] = corrected_type
1015
+
1016
+ logger.info(f"Form fixed successfully")
1017
+ return fixed_form
1018
+
1019
+ except Exception as e:
1020
+ logger.error(f"Failed to fix form: {e}", exc_info=True)
1021
+ return invalid_form
1022
 
1023
 
1024
  # ============================================================================
app/services/form_creator.py CHANGED
@@ -15,6 +15,73 @@ import dspy
15
 
16
  logger = logging.getLogger(__name__)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Validation metrics for signature outputs
20
  def validate_form_plan(form_result: Dict[str, Any]) -> Tuple[bool, List[str]]:
@@ -39,6 +106,13 @@ def validate_form_plan(form_result: Dict[str, Any]) -> Tuple[bool, List[str]]:
39
 
40
  # Validate each component has required fields
41
  components = form_result.get("components", [])
 
 
 
 
 
 
 
42
  for idx, comp in enumerate(components):
43
  if not comp.get("component_id"):
44
  errors.append(f"Component {idx}: missing component_id")
@@ -47,13 +121,16 @@ def validate_form_plan(form_result: Dict[str, Any]) -> Tuple[bool, List[str]]:
47
  if not comp.get("question_text"):
48
  errors.append(f"Component {idx}: missing question_text")
49
 
50
- # Validate question type
51
- valid_types = [
52
- "short_answer", "long_answer", "multiple_choice", "checkboxes",
53
- "dropdown", "multi_select", "number", "email", "phone", "link",
54
- "file_upload", "date", "time", "linear_scale", "matrix", "rating",
55
- "payment", "signature", "ranking", "wallet_connect"
56
- ]
 
 
 
57
  if comp.get("question_type") not in valid_types:
58
  errors.append(f"Component {idx}: invalid question_type '{comp.get('question_type')}'")
59
 
@@ -152,11 +229,28 @@ async def generate_form_spec(
152
  logger.error(f"Form generation error: {form_result['error']}")
153
  raise Exception(form_result['error'])
154
 
155
- # VALIDATION METRIC: Validate form plan structure
156
  is_valid, validation_errors = validate_form_plan(form_result)
 
 
157
  if not is_valid:
158
- logger.error(f"Form plan validation failed: {validation_errors}")
159
- raise Exception(f"Invalid form plan: {'; '.join(validation_errors)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  # Extract form metadata
162
  form_data = {
 
15
 
16
  logger = logging.getLogger(__name__)
17
 
18
+ # Maximum retries for form generation when validation fails
19
+ MAX_GENERATION_RETRIES = 3
20
+
21
+ # Common AI type mistakes -> correct type mapping
22
+ TYPE_CORRECTION_MAP = {
23
+ 'text': 'short_answer',
24
+ 'textarea': 'long_answer',
25
+ 'textbox': 'short_answer',
26
+ 'input': 'short_answer',
27
+ 'radio': 'multiple_choice',
28
+ 'radio_button': 'multiple_choice',
29
+ 'radio_buttons': 'multiple_choice',
30
+ 'checkbox': 'checkboxes',
31
+ 'select': 'dropdown',
32
+ 'select_one': 'dropdown',
33
+ 'select_multiple': 'multi_select',
34
+ 'multiselect': 'multi_select',
35
+ 'file': 'file_upload',
36
+ 'upload': 'file_upload',
37
+ 'scale': 'linear_scale',
38
+ 'slider': 'linear_scale',
39
+ 'stars': 'rating',
40
+ 'star_rating': 'rating',
41
+ 'sign': 'signature',
42
+ 'rank': 'ranking',
43
+ 'order': 'ranking',
44
+ 'wallet': 'wallet_connect',
45
+ 'web3': 'wallet_connect',
46
+ 'url': 'link',
47
+ 'website': 'link',
48
+ 'tel': 'phone',
49
+ 'telephone': 'phone',
50
+ 'datetime': 'date',
51
+ 'grid': 'matrix',
52
+ 'table': 'matrix',
53
+ 'numeric': 'number',
54
+ 'integer': 'number',
55
+ 'float': 'number',
56
+ 'submit': 'button',
57
+ 'action': 'button',
58
+ }
59
+
60
+
61
+ def correct_question_type(question_type: str) -> str:
62
+ """
63
+ Correct common AI mistakes in question type generation.
64
+
65
+ Args:
66
+ question_type: The question type string from AI
67
+
68
+ Returns:
69
+ Corrected question type string
70
+ """
71
+ if not question_type:
72
+ return 'short_answer'
73
+
74
+ # Normalize: lowercase and strip
75
+ normalized = question_type.lower().strip().replace(' ', '_').replace('-', '_')
76
+
77
+ # Check if it needs correction
78
+ if normalized in TYPE_CORRECTION_MAP:
79
+ corrected = TYPE_CORRECTION_MAP[normalized]
80
+ logger.info(f"Corrected question type '{question_type}' -> '{corrected}'")
81
+ return corrected
82
+
83
+ return normalized
84
+
85
 
86
  # Validation metrics for signature outputs
87
  def validate_form_plan(form_result: Dict[str, Any]) -> Tuple[bool, List[str]]:
 
106
 
107
  # Validate each component has required fields
108
  components = form_result.get("components", [])
109
+ valid_types = [
110
+ "short_answer", "long_answer", "multiple_choice", "checkboxes",
111
+ "dropdown", "multi_select", "number", "email", "phone", "link",
112
+ "file_upload", "date", "time", "linear_scale", "matrix", "rating",
113
+ "payment", "signature", "ranking", "wallet_connect", "button"
114
+ ]
115
+
116
  for idx, comp in enumerate(components):
117
  if not comp.get("component_id"):
118
  errors.append(f"Component {idx}: missing component_id")
 
121
  if not comp.get("question_text"):
122
  errors.append(f"Component {idx}: missing question_text")
123
 
124
+ # Auto-correct question type before validation
125
+ original_type = comp.get("question_type", "")
126
+ corrected_type = correct_question_type(original_type)
127
+
128
+ # Update the component with corrected type
129
+ if corrected_type != original_type:
130
+ comp["question_type"] = corrected_type
131
+ logger.info(f"Auto-corrected component {idx} type: '{original_type}' -> '{corrected_type}'")
132
+
133
+ # Validate question type (after correction)
134
  if comp.get("question_type") not in valid_types:
135
  errors.append(f"Component {idx}: invalid question_type '{comp.get('question_type')}'")
136
 
 
229
  logger.error(f"Form generation error: {form_result['error']}")
230
  raise Exception(form_result['error'])
231
 
232
+ # VALIDATION METRIC: Validate form plan structure (includes auto-correction)
233
  is_valid, validation_errors = validate_form_plan(form_result)
234
+
235
+ # If validation fails, try to fix the form using the AI fixer
236
  if not is_valid:
237
+ logger.warning(f"Form plan validation failed: {validation_errors}")
238
+ logger.info("Attempting to fix form using FormFixerSignature...")
239
+
240
+ # Use the fixer to correct validation errors
241
+ form_result = await generator.fix_form(
242
+ invalid_form=form_result,
243
+ validation_errors=validation_errors,
244
+ user_query=user_query
245
+ )
246
+
247
+ # Re-validate after fixing
248
+ is_valid, validation_errors = validate_form_plan(form_result)
249
+ if not is_valid:
250
+ logger.error(f"Form still invalid after fix attempt: {validation_errors}")
251
+ raise Exception(f"Invalid form plan: {'; '.join(validation_errors)}")
252
+ else:
253
+ logger.info("Form successfully fixed by AI fixer")
254
 
255
  # Extract form metadata
256
  form_data = {