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

activities & script content

Browse files
components/output_validator.py CHANGED
@@ -49,24 +49,6 @@ 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
- # 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)
 
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)
generate_module_activities_pipeline.png ADDED
generate_module_content.png CHANGED
generate_module_content_pipeline.png ADDED
pipelines/generate_module_activities.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from haystack import Pipeline
2
+ from haystack.components.builders import PromptBuilder
3
+ from haystack.components.generators import HuggingFaceAPIGenerator
4
+ from haystack.utils import Secret
5
+ from pathlib import Path # Correct import
6
+
7
+ # Template for generating content for each module
8
+ content_template = """
9
+ You are an instructional designer tasked with creating engaging and comprehensive content for training modules.
10
+ Your goal is to generate detailed educational material that fully explains each sub module, ensuring that employees gain
11
+ a complete understanding of the subject matter.
12
+
13
+ Task: Produce valid Markdown output containing the **content** for the following training module.
14
+
15
+ Module Details:
16
+ {{module}}
17
+
18
+ Instructions:
19
+ - Return only a Markdown document that follows the provided format (no extra text).
20
+ - Do not include code fences or additional commentary.
21
+ - Ensure that the content in each sub module is sufficiently detailed to properly educate an employee.
22
+
23
+ Example Format:
24
+ <start>
25
+ ## Content
26
+
27
+ ### Subtopic 1
28
+ Detailed content explaining the topic comprehensively.
29
+
30
+ ### Subtopic 2
31
+ In-depth explanation covering all necessary details.
32
+ <stop>
33
+
34
+ {% if invalid_replies and error_message %}
35
+ Your previous Markdown output was invalid. Error: {{error_message}}.
36
+ Please return only valid Markdown, with no extra text, enclosed by <start> and <stop>.
37
+ {% endif %}
38
+ """
39
+
40
+ # Create pipeline components
41
+ builder = PromptBuilder(template=content_template)
42
+ generator = HuggingFaceAPIGenerator(
43
+ api_type="serverless_inference_api",
44
+ api_params={"model": "meta-llama/Llama-3.2-3B-Instruct"},
45
+ token=Secret.from_env_var("HF_API_TOKEN"),
46
+ generation_kwargs={"max_new_tokens": 5000, "stop": ["<stop>"]},
47
+ )
48
+
49
+ pipeline = Pipeline()
50
+ pipeline.add_component("prompt_builder", instance=builder)
51
+ pipeline.add_component("llm", instance=generator)
52
+
53
+ pipeline.connect("prompt_builder", "llm")
54
+
55
+ # Save the pipeline as a YAML file and generate a diagram
56
+ with open("./pipelines/generate_module_content.yml", "w") as file:
57
+ pipeline.dump(file)
58
+
59
+ pipeline.draw(path=Path("generate_module_content.png"))
60
+
61
+ # Function to generate content for all modules in the outline
62
+ def generate_content_for_outline(outline):
63
+ content_results = {}
64
+ for module in outline["modules"]:
65
+ module_details = f"Title: {module['name']}\nDuration: {module['duration']}\nSubtopics:\n"
66
+ module_details += "\n".join([f"- {subtopic}" for subtopic in module["subtopics"]])
67
+
68
+ try:
69
+ content_result = pipeline.run({"prompt_builder": {"module": module_details}})
70
+ content_results[module["name"]] = content_result["llm"]["replies"] # Extract Markdown output
71
+ except Exception as e:
72
+ print(f"Error generating content for module '{module['name']}': {e}")
73
+ content_results[module["name"]] = {"error": str(e)}
74
+ return content_results
75
+
76
+ # Example outline for testing
77
+ updated_outline = {
78
+ "title": "Customer Service Essentials",
79
+ "modules": [
80
+ {
81
+ "name": "Introduction to Customer Service",
82
+ "duration": "10 minutes",
83
+ "subtopics": [
84
+ "Definition of customer service",
85
+ "Importance of customer service",
86
+ ],
87
+ },
88
+ {
89
+ "name": "Handling Customer Complaints",
90
+ "duration": "20 minutes",
91
+ "subtopics": [
92
+ "Types of customer complaints",
93
+ "How to handle customer complaints",
94
+ ],
95
+ },
96
+ ],
97
+ }
98
+
99
+ # Generate content and print results
100
+ module_content = generate_content_for_outline(updated_outline)
101
+ print(module_content)
pipelines/generate_module_activities.yml ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ components:
2
+ llm:
3
+ init_parameters:
4
+ api_params:
5
+ model: meta-llama/Llama-3.2-3B-Instruct
6
+ api_type: serverless_inference_api
7
+ generation_kwargs:
8
+ max_new_tokens: 4000
9
+ stop:
10
+ - <stop>
11
+ stop_sequences: []
12
+ streaming_callback: null
13
+ token:
14
+ env_vars:
15
+ - HF_API_TOKEN
16
+ strict: true
17
+ type: env_var
18
+ type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
19
+ prompt_builder:
20
+ init_parameters:
21
+ required_variables: null
22
+ template: "\nYou are an instructional designer creating engaging activities\
23
+ \ for training modules.\nYour task is to design one **interactive activity**\
24
+ \ for each subtopic listed in the module below.\n\nModule Details:\n{{module}}\n\
25
+ \nInstructions:\n- Return only valid Markdown output.\n- Include a brief description\
26
+ \ of the activity, its objective, and how employees should complete it.\n\
27
+ - Make sure each activity is relevant, creative, and suitable for adult learners.\n\
28
+ - Enclose the response within <start> and <stop> tags without extra text.\n\
29
+ \nExample Format:\n<start>\n## Activities\n\n### Subtopic 1: [Subtopic Name]\n\
30
+ **Activity:** [Activity Name] \n**Objective:** [Learning objective of the\
31
+ \ activity] \n**Instructions:** [Step-by-step guide on how to complete the\
32
+ \ activity]\n\n### Subtopic 2: [Subtopic Name]\n**Activity:** [Activity Name]\
33
+ \ \n**Objective:** [Learning objective of the activity] \n**Instructions:**\
34
+ \ [Step-by-step guide on how to complete the activity]\n<stop>\n"
35
+ variables: null
36
+ type: haystack.components.builders.prompt_builder.PromptBuilder
37
+ connections:
38
+ - receiver: llm.prompt
39
+ sender: prompt_builder.prompt
40
+ max_runs_per_component: 100
41
+ metadata: {}
pipelines/generate_module_content.py CHANGED
@@ -2,75 +2,80 @@ from haystack import Pipeline
2
  from haystack.components.builders import PromptBuilder
3
  from haystack.components.generators import HuggingFaceAPIGenerator
4
  from haystack.utils import Secret
5
- from components.output_validator import OutputValidator
6
  from components.json_grabber import JSONGrabber
7
- from classes import ModuleContent
8
- 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 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)
36
-
37
  generator = HuggingFaceAPIGenerator(
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()
45
- pipeline.add_component("prompt_builder", PromptBuilder(template=content_template))
46
  pipeline.add_component("llm", generator)
47
- pipeline.add_component("json_grabber", instance=JSONGrabber())
48
- pipeline.add_component(
49
- "output_validator", instance=OutputValidator(pydantic_model=ModuleContent)
50
- )
51
  pipeline.connect("prompt_builder", "llm")
52
- pipeline.connect("llm.replies", "json_grabber.replies")
53
- pipeline.connect("json_grabber.body", "output_validator.body")
54
 
55
  # Save the pipeline as a YAML file and generate a diagram
56
  with open("./pipelines/generate_module_content.yml", "w") as file:
57
- pipeline.dump(file)
58
 
59
- pipeline.draw(path=Path("generate_module_content.png"))
60
 
61
  # Function to run the pipeline for all modules in the outline
62
  def generate_content_for_outline(outline):
63
- results = {}
64
  for module in outline["modules"]:
65
- module_result = pipeline.run(
66
- {
67
- "prompt_builder": {
68
- "module": module,
69
- }
70
- }
71
- )
72
- results[module["name"]] = module_result
73
- return results
 
 
 
 
74
 
75
  # Example outline to test the script
76
  updated_outline = {
@@ -82,7 +87,7 @@ updated_outline = {
82
  "subtopics": [
83
  "Definition of customer service",
84
  "Importance of customer service",
85
- ]
86
  },
87
  {
88
  "name": "Handling Customer Complaints",
@@ -90,11 +95,11 @@ updated_outline = {
90
  "subtopics": [
91
  "Types of customer complaints",
92
  "How to handle customer complaints",
93
- ]
94
  },
95
  ],
96
  }
97
 
98
- # Generate dialog and content for the updated outline
99
  module_content = generate_content_for_outline(updated_outline)
100
  print(module_content)
 
2
  from haystack.components.builders import PromptBuilder
3
  from haystack.components.generators import HuggingFaceAPIGenerator
4
  from haystack.utils import Secret
 
5
  from components.json_grabber import JSONGrabber
6
+ from pathlib import Path # Fixed import
 
7
 
8
+ # Template for generating detailed module content
9
  content_template = """
10
+ You are an instructional designer tasked with creating engaging and comprehensive content for training modules.
11
+ Your goal is to generate detailed educational material that fully explains each sub-module, ensuring that employees gain
12
+ a complete understanding of the subject matter.
13
+
14
+ Task: Produce valid Markdown output containing the **content** for the following training module.
15
 
 
16
  Module Details:
17
  {{module}}
18
 
19
  Instructions:
20
+ - Return only a Markdown document that follows the provided format (no extra text).
 
21
  - Do not include code fences or additional commentary.
22
+ - Ensure that the content in each sub-module is sufficiently detailed to properly educate an employee.
23
+
24
+ Example Format:
25
+ <start>
26
+ ## Content
27
+
28
+ ### Subtopic 1
29
+ Detailed content explaining the topic comprehensively.
30
+
31
+ ### Subtopic 2
32
+ In-depth explanation covering all necessary details.
33
+ <stop>
34
+
35
+ {% if invalid_replies and error_message %}
36
+ Your previous Markdown output was invalid. Error: {{error_message}}.
37
+ Please return only valid Markdown, with no extra text, enclosed by <start> and <stop>.
38
+ {% endif %}
39
  """
40
 
41
+ # Create pipeline components
42
  builder = PromptBuilder(template=content_template)
 
43
  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": 5000, "stop": ["<stop>"]},
48
  )
49
 
50
  pipeline = Pipeline()
51
+ pipeline.add_component("prompt_builder", builder)
52
  pipeline.add_component("llm", generator)
53
+
 
 
 
54
  pipeline.connect("prompt_builder", "llm")
 
 
55
 
56
  # Save the pipeline as a YAML file and generate a diagram
57
  with open("./pipelines/generate_module_content.yml", "w") as file:
58
+ pipeline.dump(file)
59
 
60
+ pipeline.draw(path=Path("generate_module_content_pipeline.png")) # Fixed Path import
61
 
62
  # Function to run the pipeline for all modules in the outline
63
  def generate_content_for_outline(outline):
64
+ content_results = {}
65
  for module in outline["modules"]:
66
+ module_details = f"Title: {module['name']}\nDuration: {module['duration']}\nSubtopics:\n"
67
+ module_details += "\n".join([f"- {subtopic}" for subtopic in module["subtopics"]])
68
+
69
+ try:
70
+ module_result = pipeline.run(
71
+ {"prompt_builder": {"module": module_details}}
72
+ )
73
+ content_results[module["name"]] = module_result
74
+ except Exception as e:
75
+ print(f"Error generating content for module '{module['name']}': {e}")
76
+ content_results[module["name"]] = {"error": str(e)}
77
+
78
+ return content_results
79
 
80
  # Example outline to test the script
81
  updated_outline = {
 
87
  "subtopics": [
88
  "Definition of customer service",
89
  "Importance of customer service",
90
+ ],
91
  },
92
  {
93
  "name": "Handling Customer Complaints",
 
95
  "subtopics": [
96
  "Types of customer complaints",
97
  "How to handle customer complaints",
98
+ ],
99
  },
100
  ],
101
  }
102
 
103
+ # Generate content for the updated outline
104
  module_content = generate_content_for_outline(updated_outline)
105
  print(module_content)
pipelines/generate_module_content.yml CHANGED
@@ -1,14 +1,11 @@
1
  components:
2
- json_grabber:
3
- init_parameters: {}
4
- type: components.json_grabber.JSONGrabber
5
  llm:
6
  init_parameters:
7
  api_params:
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: []
@@ -19,37 +16,28 @@ components:
19
  strict: true
20
  type: env_var
21
  type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
22
- output_validator:
23
- pydantic_model: classes.ModuleContent
24
- type: components.output_validator.OutputValidator
25
  prompt_builder:
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:
48
  - receiver: llm.prompt
49
  sender: prompt_builder.prompt
50
- - receiver: json_grabber.replies
51
- sender: llm.replies
52
- - receiver: output_validator.body
53
- sender: json_grabber.body
54
  max_runs_per_component: 100
55
  metadata: {}
 
1
  components:
 
 
 
2
  llm:
3
  init_parameters:
4
  api_params:
5
  model: meta-llama/Llama-3.2-3B-Instruct
6
  api_type: serverless_inference_api
7
  generation_kwargs:
8
+ max_new_tokens: 5000
9
  stop:
10
  - <stop>
11
  stop_sequences: []
 
16
  strict: true
17
  type: env_var
18
  type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
 
 
 
19
  prompt_builder:
20
  init_parameters:
21
  required_variables: null
22
  template: "\nYou are an instructional designer tasked with creating engaging\
23
+ \ and comprehensive content for training modules. \nYour goal is to generate\
24
+ \ detailed educational material that fully explains each sub module, ensuring\
25
+ \ that employees gain \na complete understanding of the subject matter.\n\n\
26
+ Task: Produce valid Markdown output containing the **content** for the following\
27
+ \ training module.\n\nModule Details:\n{{module}}\n\nInstructions:\n- Return\
28
+ \ only a Markdown document that follows the provided format (no extra text).\n\
29
+ - Do not include code fences or additional commentary.\n- Ensure that the\
30
+ \ content in each sub module is sufficiently detailed to properly educate\
31
+ \ an employee.\n\nExample Format:\n<start>\n## Content\n\n### Subtopic 1\n\
32
+ Detailed content explaining the topic comprehensively.\n\n### Subtopic 2\n\
33
+ In-depth explanation covering all necessary details.\n<stop>\n\n{% if invalid_replies\
34
+ \ and error_message %}\nYour previous Markdown output was invalid. Error:\
35
+ \ {{error_message}}. \nPlease return only valid Markdown, with no extra text,\
36
+ \ enclosed by <start> and <stop>.\n{% endif %}\n"
 
 
37
  variables: null
38
  type: haystack.components.builders.prompt_builder.PromptBuilder
39
  connections:
40
  - receiver: llm.prompt
41
  sender: prompt_builder.prompt
 
 
 
 
42
  max_runs_per_component: 100
43
  metadata: {}