from haystack import Pipeline from haystack.components.builders import PromptBuilder from haystack.components.generators import HuggingFaceAPIGenerator from haystack.utils import Secret from pathlib import Path # Correct import # Template for generating content for each module content_template = """ You are an instructional designer tasked with creating engaging and comprehensive content for training modules. Your goal is to generate detailed educational material that fully explains each sub module, ensuring that employees gain a complete understanding of the subject matter. Task: Produce valid Markdown output containing the **content** for the following training module. Module Details: {{module}} Instructions: - Return only a Markdown document that follows the provided format (no extra text). - Do not include code fences or additional commentary. - Ensure that the content in each sub module is sufficiently detailed to properly educate an employee. Example Format: ## Content ### Subtopic 1 Detailed content explaining the topic comprehensively. ### Subtopic 2 In-depth explanation covering all necessary details. {% if invalid_replies and error_message %} Your previous Markdown output was invalid. Error: {{error_message}}. Please return only valid Markdown, with no extra text, enclosed by and . {% endif %} """ # Create pipeline components builder = PromptBuilder(template=content_template) generator = HuggingFaceAPIGenerator( api_type="serverless_inference_api", api_params={"model": "meta-llama/Llama-3.2-3B-Instruct"}, token=Secret.from_env_var("HF_API_TOKEN"), generation_kwargs={"max_new_tokens": 5000, "stop": [""]}, ) pipeline = Pipeline() pipeline.add_component("prompt_builder", instance=builder) pipeline.add_component("llm", instance=generator) pipeline.connect("prompt_builder", "llm") # Save the pipeline as a YAML file and generate a diagram with open("./pipelines/generate_module_activities.yml", "w") as file: pipeline.dump(file) pipeline.draw(path=Path("generate_module_activities.png")) # Function to generate content for all modules in the outline def generate_content_for_outline(outline): content_results = {} for module in outline["modules"]: module_details = f"Title: {module['name']}\nDuration: {module['duration']}\nSubtopics:\n" module_details += "\n".join([f"- {subtopic}" for subtopic in module["subtopics"]]) try: content_result = pipeline.run({"prompt_builder": {"module": module_details}}) content_results[module["name"]] = content_result["llm"]["replies"] # Extract Markdown output except Exception as e: print(f"Error generating content for module '{module['name']}': {e}") content_results[module["name"]] = {"error": str(e)} return content_results # Example outline for testing updated_outline = { "title": "Customer Service Essentials", "modules": [ { "name": "Introduction to Customer Service", "duration": "10 minutes", "subtopics": [ "Definition of customer service", "Importance of customer service", ], }, { "name": "Handling Customer Complaints", "duration": "20 minutes", "subtopics": [ "Types of customer complaints", "How to handle customer complaints", ], }, ], } # Generate content and print results module_content = generate_content_for_outline(updated_outline) print(module_content)