Haystack
Example requires Haystack version 2.2.0 or later
To unlock full functionality, create a custom component to wrap the RS skill that supports Root Signals Validators
from typing import Dict
from typing import List
from haystack import component
from root import RootSignals
from root.validators import Validator
@component
class RootSignalsGenerator:
"""
Component to enable skill use
"""
def __init__(self, name: str, intent: str, prompt: str, model: str, validators: List[Validator]):
self.client = RootSignals()
self.skill = self.client.skills.create(
name=name,
intent=intent,
prompt=prompt,
model=model,
validators=validators,
)
For convenience, lets create another component to parse validation results
from root.generated.openapi_client.models.skill_execution_result import SkillExecutionResult
@component
class RootSignalsValidationResultParser:
@component.output_types(passed=bool, details=Dict[str, [str, float, bool]])
def run(self, replies: Dict[str, SkillExecutionResult]):
return {"passed": replies.validation['is_valid']}
We are now equipped to have any OpenAI compatible generator being replaced with a Validated one, based on the RootSignalsGenerator
component.
from haystack.dataclasses import ChatMessage
from haystack.core.pipeline.pipeline import Pipeline
from haystack.components.builders.dynamic_chat_prompt_builder import DynamicChatPromptBuilder
generator_A = RootSignalsGenerator(
name="My Q&A chatbot",
intent="Simple Q&A chatbot",
prompt="Provide a clear answer to the question: {{question}}",
model="gpt-4o",
validators=[Validator(evaluator_name="Clarity", threshold=0.6)]
)
pipeline = Pipeline(max_loops_allowed=1)
pipeline.add_component("prompt_builder", DynamicChatPromptBuilder())
pipeline.add_component("generator_A", generator_A)
pipeline.add_component("validation_parser", RootSignalsValidationResultParser())
pipeline.connect("prompt_builder.prompt", "generator_A.messages")
pipeline.connect("generator_A.replies", "validation_parser.replies")
prompt_template = """
Answer the question below.
Question: {{question}}
"""
result = pipeline.run(
{
"prompt_builder": {
"prompt_source": [ChatMessage.from_user(prompt_template)],
"template_variables": {
"question": "In the field of software development, what is the meaning and significance of 'containerization'? Use a popular technology as example. Cite sources where available."
},
}
},
include_outputs_from={
"generator_A",
"validation_parser",
},
)
{
'validation_parser': {'passed': True}, # use this directly, i.e. for haystack routers
'generator_A': { # full response from the generator, use llm_output for the plain response
'replies': SkillExecutionResult(
llm_output='Containerization in software development refers to the practice of encapsulating an application and its dependencies into a "container" that can run consistently across different computing environments. This approach ensures that the software behaves the same regardless of where it is deployed <truncated> \nSources:\n- Docker. "What is a Container?" Docker, https://www.docker.com/resources/what-container.\n- Red Hat. "What is containerization?" Red Hat, https://www.redhat.com/en/topics/containers/what-is-containerization.',
validation={'is_valid': True, 'validator_results': [{'evaluator_name': 'Clarity', 'evaluator_id': '603eae60-790b-4215-b6d3-301c16fc37c5', 'result': 0.85, 'threshold': 0.6, 'cost': 0.006645000000000001, 'is_valid': True, 'status': 'finished'}]},
model='gpt-4o',
execution_log_id='1fbdd6fc-f5a7-4e30-a7dc-15549b7557ec',
rendered_prompt="Provide a clear answer to the question: Answer the question below.\n \n Question: In the field of software development, what is the meaning and significance of 'containerization'? Use a popular technology as example. Cite sources where available.",
cost=0.003835)
}
}
Last updated