script generation!
Browse files- classes.py +4 -0
- generate_module_content.png +0 -0
- pipelines/generate_module_content.py +106 -0
- pipelines/generate_module_content.yml +50 -0
classes.py
CHANGED
|
@@ -22,3 +22,7 @@ class CourseOutline(BaseModel):
|
|
| 22 |
duration: str
|
| 23 |
modules: List[Module]
|
| 24 |
summary: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
duration: str
|
| 23 |
modules: List[Module]
|
| 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
|
generate_module_content.png
ADDED
|
pipelines/generate_module_content.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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 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)
|
| 42 |
+
|
| 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": 10000, "stop": ["<stop>"]},
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
pipeline = Pipeline()
|
| 51 |
+
pipeline.add_component("prompt_builder", PromptBuilder(template=content_template))
|
| 52 |
+
pipeline.add_component("llm", generator)
|
| 53 |
+
pipeline.add_component("json_grabber", instance=JSONGrabber())
|
| 54 |
+
pipeline.add_component(
|
| 55 |
+
"output_validator", instance=OutputValidator(pydantic_model=ModuleContent)
|
| 56 |
+
)
|
| 57 |
+
pipeline.connect("prompt_builder", "llm")
|
| 58 |
+
pipeline.connect("llm.replies", "json_grabber.replies")
|
| 59 |
+
pipeline.connect("json_grabber.body", "output_validator.body")
|
| 60 |
+
|
| 61 |
+
# Save the pipeline as a YAML file and generate a diagram
|
| 62 |
+
with open("./pipelines/generate_module_content.yml", "w") as file:
|
| 63 |
+
pipeline.dump(file)
|
| 64 |
+
|
| 65 |
+
pipeline.draw(path=Path("generate_module_content.png"))
|
| 66 |
+
|
| 67 |
+
# Function to run the pipeline for all modules in the outline
|
| 68 |
+
def generate_content_for_outline(outline):
|
| 69 |
+
results = {}
|
| 70 |
+
for module in outline["modules"]:
|
| 71 |
+
module_result = pipeline.run(
|
| 72 |
+
{
|
| 73 |
+
"prompt_builder": {
|
| 74 |
+
"module": module,
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
)
|
| 78 |
+
results[module["name"]] = module_result
|
| 79 |
+
return results
|
| 80 |
+
|
| 81 |
+
# Example outline to test the script
|
| 82 |
+
updated_outline = {
|
| 83 |
+
"title": "Customer Service Essentials",
|
| 84 |
+
"modules": [
|
| 85 |
+
{
|
| 86 |
+
"name": "Introduction to Customer Service",
|
| 87 |
+
"duration": "10 minutes",
|
| 88 |
+
"subtopics": [
|
| 89 |
+
"Definition of customer service",
|
| 90 |
+
"Importance of customer service",
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"name": "Handling Customer Complaints",
|
| 95 |
+
"duration": "20 minutes",
|
| 96 |
+
"subtopics": [
|
| 97 |
+
"Types of customer complaints",
|
| 98 |
+
"How to handle customer complaints",
|
| 99 |
+
]
|
| 100 |
+
},
|
| 101 |
+
],
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
# Generate dialog and content for the updated outline
|
| 105 |
+
module_content = generate_content_for_outline(updated_outline)
|
| 106 |
+
print(module_content)
|
pipelines/generate_module_content.yml
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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: 10000
|
| 12 |
+
stop:
|
| 13 |
+
- <stop>
|
| 14 |
+
stop_sequences: []
|
| 15 |
+
streaming_callback: null
|
| 16 |
+
token:
|
| 17 |
+
env_vars:
|
| 18 |
+
- HF_API_TOKEN
|
| 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 |
+
\ 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:
|
| 43 |
+
- receiver: llm.prompt
|
| 44 |
+
sender: prompt_builder.prompt
|
| 45 |
+
- receiver: json_grabber.replies
|
| 46 |
+
sender: llm.replies
|
| 47 |
+
- receiver: output_validator.body
|
| 48 |
+
sender: json_grabber.body
|
| 49 |
+
max_runs_per_component: 100
|
| 50 |
+
metadata: {}
|