from haystack import Pipeline from haystack.components.builders import PromptBuilder from haystack.components.generators import HuggingFaceAPIGenerator from haystack.utils import Secret from components.output_validator import OutputValidator from components.json_grabber import JSONGrabber from classes import CourseOutline from haystack.core.pipeline.template import Path output_validator = OutputValidator(pydantic_model=CourseOutline) template = """ You are a training manager. You are tasked to create an online, asynchronous course intended for new employees. Create one JSON object based on the requested topic of {{topic}} within the requested estimated time of {{time}}. Follow this JSON schema, but only return the actual instance without any additional schema definitions: {{schema}} Make sure your response is a dict and not a list. Put this string: "" right before the JSON object. Put this string: "" right after the JSON object. {% if invalid_replies and error_message %} You already created the following output in a previous attempt: {{invalid_replies}} However, this doesn't comply with the format requirements from above and triggered this Python exception: {{error_message}} Correct the output and try again. Just return the corrected output without any extra explanations. {% endif %} """ builder = PromptBuilder(template=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": 1024, "stop": [""]}, ) pipeline = Pipeline(max_runs_per_component=5) pipeline.add_component("prompt_builder", PromptBuilder(template=template)) pipeline.add_component("llm", generator) pipeline.add_component("json_grabber", instance=JSONGrabber()) pipeline.add_component("output_validator", output_validator) pipeline.connect("prompt_builder", "llm") pipeline.connect("llm.replies", "json_grabber.replies") pipeline.connect("json_grabber.body", "output_validator.body") pipeline.connect("output_validator.invalid_replies", "prompt_builder.invalid_replies") pipeline.connect("output_validator.error_message", "prompt_builder.error_message") with open("./pipelines/generate_outline.yml", "w") as file: pipeline.dump(file) pipeline.draw(path=Path("generate_outline.png")) """ json_schema = CourseOutline.model_json_schema() import json results = pipeline.run( { "prompt_builder": { "topic": "Artificial Intelligence", "time": "one hour", "schema": json_schema, } } ) print(type(results)) print(results) """