From 5c42683b40141cf263552af0c7460cd9322d429e Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 31 Jul 2023 10:58:02 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20docs(flow-runner.mdx):=20add=20e?= =?UTF-8?q?xample=20code=20for=20a=20Flow=20Runner=20custom=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ℹ️ The example code demonstrates how to create a custom component called "Flow Runner" that runs other flows using a document as input. The code includes the implementation of the `build_config` and `build` methods. ✨ feat(flow-runner.mdx): add example code for a Flow Runner custom component to provide a practical example for users to follow --- docs/docs/examples/flow-runner.mdx | 44 ++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/docs/examples/flow-runner.mdx b/docs/docs/examples/flow-runner.mdx index 846a5b186..ffa4d2e2e 100644 --- a/docs/docs/examples/flow-runner.mdx +++ b/docs/docs/examples/flow-runner.mdx @@ -29,6 +29,48 @@ We will cover how to: - Load a flow using the _`load_flow`_ method. - Configure a dropdown input field using the _`options`_ parameter. +
+ +Example Code + +```python +from langflow import CustomComponent +from langchain.schema import Document + +class FlowRunner(CustomComponent): +display_name = "Flow Runner" +description = "Run other flows using a document as input." + + def build_config(self): + flows = self.list_flows() + flow_names = [f.name for f in flows] + return {"flow_name": {"options": flow_names, + "display_name": "Flow Name", + }, + "document": {"display_name": "Document"} + } + + + def build(self, flow_name: str, document: Document) -> Document: + # List the flows + flows = self.list_flows() + # Get the flow that matches the selected name + # You can also get the flow by id + # using self.get_flow(flow_id=flow_id) + flow = self.get_flow(flow_name=flow_name) + # Load the flow + tweaks = {} + flow = self.load_flow(flow.id, tweaks) + # Get the page_content from the document + page_content = document.page_content + # Use it in the flow + result = flow(page_content) + return Document(page_content=str(result)) + +``` + +
+ ```python @@ -282,8 +324,6 @@ Finally, we can add field customizations through the _`build_config`_ method. He Make sure that the field type is _`str`_ and _`options`_ values are strings. ---- - Done! This is what our script and custom component look like: