aimanzoqi commited on
Commit
b79cefc
·
1 Parent(s): db60f58

- intial script development in JSON output.

Browse files
classes.py CHANGED
@@ -24,5 +24,5 @@ class CourseOutline(BaseModel):
24
  summary: str
25
 
26
  class ModuleContent(BaseModel):
27
- dialog: dict[str, str] # A dictionary mapping subtopics to dialog strings
28
  activities: dict[str, str] # A dictionary mapping subtopics to activity descriptions
 
24
  summary: str
25
 
26
  class ModuleContent(BaseModel):
27
+ content: dict[str, str] # A dictionary mapping subtopics to dialog strings
28
  activities: dict[str, str] # A dictionary mapping subtopics to activity descriptions
components/output_validator.py CHANGED
@@ -49,6 +49,24 @@ class OutputValidator:
49
 
50
  ## Try to parse the LLM's reply ##
51
  # If the LLM's reply is a valid object, return `"valid_replies"`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  try:
53
  output_dict = json.loads(body)
54
  self.pydantic_model.model_validate(output_dict)
 
49
 
50
  ## Try to parse the LLM's reply ##
51
  # If the LLM's reply is a valid object, return `"valid_replies"`
52
+ # Trim any extra whitespace
53
+ body = body.strip()
54
+
55
+ # If there is a <start> marker, remove everything before and including it
56
+ if "<start>" in body:
57
+ body = body.split("<start>", 1)[1]
58
+
59
+ # If there is a <stop> marker, remove everything from it onward
60
+ if "<stop>" in body:
61
+ body = body.split("<stop>", 1)[0]
62
+
63
+ # Also, remove any text before the first '{'
64
+ first_brace = body.find('{')
65
+ if first_brace != -1:
66
+ body = body[first_brace:]
67
+
68
+ body = body.strip() # Final cleanup
69
+
70
  try:
71
  output_dict = json.loads(body)
72
  self.pydantic_model.model_validate(output_dict)
generate_module_content.png CHANGED
pipelines/generate_module_content.py CHANGED
@@ -9,33 +9,27 @@ from haystack.core.pipeline.template import Path
9
 
10
  # Template for generating dialog and content for each module
11
  content_template = """
12
- You are an instructional designer tasked with creating engaging dialog and interactive content for training modules.
13
- **Task**: Generate valid JSON for the following module.
14
- **Module Details**:
 
 
 
15
  {{module}}
16
- **Instructions**:
17
- - Only return a JSON object that follows the schema (no extra text).
18
- - Enclose the JSON between <start> and <stop>.
19
- - Do not include code fences or commentary.
20
 
21
- Example Format:
22
- <start>
23
- {
24
- "dialog": {
25
- "Subtopic1": "some dialog",
26
- "Subtopic2": "some dialog"
27
- },
28
- "activities": {
29
- "Subtopic1": "some activity",
30
- "Subtopic2": "some activity"
31
- }
32
- }
33
- <stop>
34
 
35
- {% if invalid_replies and error_message %}
36
- Your last JSON was invalid. Error: {{error_message}}
37
- Return only valid JSON, with no extra text, enclosed by <start> and <stop>.
38
- {% endif %}
39
  """
40
 
41
  builder = PromptBuilder(template=content_template)
@@ -44,7 +38,7 @@ generator = HuggingFaceAPIGenerator(
44
  api_type="serverless_inference_api",
45
  api_params={"model": "meta-llama/Llama-3.2-3B-Instruct"},
46
  token=Secret.from_env_var("HF_API_TOKEN"),
47
- generation_kwargs={"max_new_tokens": 10000, "stop": ["<stop>"]},
48
  )
49
 
50
  pipeline = Pipeline()
 
9
 
10
  # Template for generating dialog and content for each module
11
  content_template = """
12
+ You are an instructional designer tasked with creating engaging and comprehensive content alongside interactive learning activities
13
+ for training modules. Your goal is to generate detailed educational material that fully explains each sub module, ensuring that
14
+ employees gain a complete understanding of the subject matter.
15
+
16
+ Task: Produce valid JSON output for the following training module.
17
+ Module Details:
18
  {{module}}
 
 
 
 
19
 
20
+ Instructions:
21
+ - Return only a JSON object that strictly follows the provided schema (no extra text).
22
+ - Enclose the JSON between <start> and <stop>.
23
+ - Do not include code fences or additional commentary.
24
+ - Ensure that the content in each sub module is sufficiently detailed to properly educate an employee.
25
+ Example Format: <start> {
26
+ "content": { "Subtopic1": "detailed content explaining the topic comprehensively",
27
+ "Subtopic2": "in-depth explanation covering all necessary details" },
28
+ "activities": { "Subtopic1": "interactive activity or exercise for practice",
29
+ "Subtopic2": "engaging learning activity to reinforce the content" } } <stop>
 
 
 
30
 
31
+ {% if invalid_replies and error_message %} Your previous JSON output was invalid. Error: {{error_message}}.
32
+ Please return only valid JSON, with no extra text, enclosed by <start> and <stop>. {% endif %}
 
 
33
  """
34
 
35
  builder = PromptBuilder(template=content_template)
 
38
  api_type="serverless_inference_api",
39
  api_params={"model": "meta-llama/Llama-3.2-3B-Instruct"},
40
  token=Secret.from_env_var("HF_API_TOKEN"),
41
+ generation_kwargs={"max_new_tokens": 1000, "stop": ["<stop>"]},
42
  )
43
 
44
  pipeline = Pipeline()
pipelines/generate_module_content.yml CHANGED
@@ -8,7 +8,7 @@ components:
8
  model: meta-llama/Llama-3.2-3B-Instruct
9
  api_type: serverless_inference_api
10
  generation_kwargs:
11
- max_new_tokens: 10000
12
  stop:
13
  - <stop>
14
  stop_sequences: []
@@ -26,17 +26,22 @@ components:
26
  init_parameters:
27
  required_variables: null
28
  template: "\nYou are an instructional designer tasked with creating engaging\
29
- \ dialog and interactive content for training modules.\n**Task**: Generate\
30
- \ valid JSON for the following module.\n**Module Details**:\n{{module}}\n\
31
- **Instructions**:\n- Only return a JSON object that follows the schema (no\
32
- \ extra text).\n- Enclose the JSON between <start> and <stop>.\n- Do not include\
33
- \ code fences or commentary.\n\nExample Format:\n<start>\n{\n \"dialog\"\
34
- : {\n \"Subtopic1\": \"some dialog\",\n \"Subtopic2\": \"some\
35
- \ dialog\"\n },\n \"activities\": {\n \"Subtopic1\": \"some activity\"\
36
- ,\n \"Subtopic2\": \"some activity\"\n }\n}\n<stop>\n\n{% if invalid_replies\
37
- \ and error_message %}\nYour last JSON was invalid. Error: {{error_message}}\n\
38
- Return only valid JSON, with no extra text, enclosed by <start> and <stop>.\n\
39
- {% endif %}\n"
 
 
 
 
 
40
  variables: null
41
  type: haystack.components.builders.prompt_builder.PromptBuilder
42
  connections:
 
8
  model: meta-llama/Llama-3.2-3B-Instruct
9
  api_type: serverless_inference_api
10
  generation_kwargs:
11
+ max_new_tokens: 1000
12
  stop:
13
  - <stop>
14
  stop_sequences: []
 
26
  init_parameters:
27
  required_variables: null
28
  template: "\nYou are an instructional designer tasked with creating engaging\
29
+ \ and comprehensive content alongside interactive learning activities \nfor\
30
+ \ training modules. Your goal is to generate detailed educational material\
31
+ \ that fully explains each sub module, ensuring that \nemployees gain a complete\
32
+ \ understanding of the subject matter.\n\nTask: Produce valid mark down output\
33
+ \ for the following training module.\nModule Details:\n{{module}}\n\nInstructions:\n\
34
+ - Enclose the mark down between <start> and <stop>.\n- Do not include code\
35
+ \ fences or additional commentary.\n- Ensure that the content in each sub\
36
+ \ module is sufficiently detailed to properly educate an employee.\nExample\
37
+ \ Format: <start> {\n \"content\": { \"Subtopic1\": \"detailed content explaining\
38
+ \ the topic comprehensively\", \n\"Subtopic2\": \"in-depth explanation covering\
39
+ \ all necessary details\" }, \n\"activities\": { \"Subtopic1\": \"interactive\
40
+ \ activity or exercise for practice\",\n \"Subtopic2\": \"engaging learning\
41
+ \ activity to reinforce the content\" } } <stop>\n\n{% if invalid_replies\
42
+ \ and error_message %} Your previous JSON output was invalid. Error: {{error_message}}.\
43
+ \ \nPlease return only valid JSON, with no extra text, enclosed by <start>\
44
+ \ and <stop>. {% endif %}\n"
45
  variables: null
46
  type: haystack.components.builders.prompt_builder.PromptBuilder
47
  connections: