TrainingCourseGen / pipelines /update_outline.py
martinezjandrew's picture
Update pipelines/update_outline.py
94acea6 verified
Raw History Blame Contribute Delete
4.58 kB
from haystack import Pipeline
from haystack.components.builders import PromptBuilder
from haystack.components.generators import HuggingFaceAPIGenerator
from haystack.utils import Secret
from classes import CourseOutline
from components.output_validator import OutputValidator
from components.json_grabber import JSONGrabber
from haystack.core.pipeline.template import Path
template = """
You are a training manager tasked with creating an online, asynchronous course for new employees.
**Task**: Revise the following script based on the provided notes.
**Script**:
{{script}}
**Notes**:
{{notes}}
**Schema**: The script follows the provided JSON schema:
{{schema}}.
**Instructions**:
- Ensure the response is a JSON object that strictly follows the schema.
- Make sure your response is a dict and not a list.
- Only return the updated JSON object.
- Do not include the schema in your response.
- Put this string: "<start>" right before the JSON object.
- Put this string: "<stop>" right after the JSON object.
- Ensure the response is unique and does not repeat any previous content.
- You can only choose one of "video", "image", or "interactive" for the key "features" in "modules".
**Example Format**:
<start>
{
"title": "Example Title",
"objectives": ["Objective 1", "Objective 2"],
"duration": "1 hour",
"modules": [
{
"name": "Module 1",
"duration": "10 minutes",
"subtopics": ["Subtopic 1", "Subtopic 2"],
"features": ["video"]
}
],
"summary": "Example summary."
}
<stop>
{% 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": ["<stop>"]},
)
pipeline = Pipeline()
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", instance=OutputValidator(pydantic_model=CourseOutline)
)
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/update_outline.yml", "w") as file:
pipeline.dump(file)
pipeline.draw(path=Path("update_outline.png"))
"""
test_script = """
{
"title": "Customer Service Essentials",
"objectives": [
"Understand the importance of customer service",
"Learn how to handle customer complaints",
"Develop effective communication skills",
],
"duration": "1 hour",
"modules": [
{
"name": "Introduction to Customer Service",
"duration": "10 minutes",
"subtopics": [
"Definition of customer service",
"Importance of customer service",
],
"features": ["video"],
},
{
"name": "Handling Customer Complaints",
"duration": "20 minutes",
"subtopics": [
"Types of customer complaints",
"How to handle customer complaints",
],
"features": ["interactive"],
},
{
"name": "Effective Communication Skills",
"duration": "30 minutes",
"subtopics": ["Active listening", "Clear communication"],
"features": ["image"],
},
],
"summary": "This course provides an overview of customer service essentials, including how to handle customer complaints and develop effective communication skills.",
}
"""
results = pipeline.run(
{
"prompt_builder": {
"script": test_script,
"schema": CourseOutline.model_json_schema(),
"notes": "Add a module to talk about how to handle unjustifiedly rude customers",
}
}
)
print(results)
"""