Replies: 2 comments
-
Hi @frankShih you can also use the ConditionalRouter for what you describe. Let me adapt the code example from our documentation page: https://docs.haystack.deepset.ai/docs/conditionalrouter from haystack import Pipeline
from haystack.components.routers import ConditionalRouter
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack.components.generators import OpenAIGenerator
routes = [
{
"condition": "{{query|length > 10}}",
"output": "{{query}}",
"output_name": "long_query",
"output_type": str,
},
{
"condition": "{{query|length <= 10}}",
"output": "{{query}}",
"output_name": "short_query",
"output_type": str,
},
]
router = ConditionalRouter(routes=routes)
pipe = Pipeline()
pipe.add_component("router", router)
pipe.add_component("prompt_builder_short", PromptBuilder("Answer the following query by giving a detailed answer. {{query}}"))
pipe.add_component("generator_short", OpenAIGenerator())
pipe.add_component("prompt_builder_long", PromptBuilder("Answer the following query but keep the answer short. {{query}}"))
pipe.add_component("generator_long", SomeOtherLLM())
pipe.connect("router.short_query", "prompt_builder_short.query")
pipe.connect("prompt_builder_short", "generator_short")
pipe.connect("router.long_query", "prompt_builder_long.query")
pipe.connect("prompt_builder_long", "generator_long")
pipe.run(data={"router": {"query": "Berlin"}})
# {'generator_short': {'replies': ['...'], ...}
pipe.run(data={"router": {"query": "What is the capital of Italy?"}})
# {'generator_long': {'replies': ['...'], ...} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @frankShih please let us know how you get on with this. I am the product manager of Haystack and I'm currently collecting feedback about the library. Would you be available for a 15 min catch up? It would really help us! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a self-implement RAG-like workflow. In the workflow, it has many 'if-else' condition deciding which function should be called next.
As I imagine, if I re-write my workflow using haystack, the pipeline would be like a tree structure. Each run will end up reach one of the leaf node (component).
To reach my goal, I try conditional routing. But it seems mores like "on differet condition, return different value". However, my requirement is "on different condition, send current component output to the next corresponding component.
Any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions