📝 docs(flow-runner.mdx): add example code for a Flow Runner custom component
ℹ️ 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
This commit is contained in:
parent
16935ec198
commit
5c42683b40
1 changed files with 42 additions and 2 deletions
|
|
@ -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.
|
||||
|
||||
<details>
|
||||
|
||||
<summary>Example Code</summary>
|
||||
|
||||
```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))
|
||||
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<CH.Scrollycoding rows={20} className={""}>
|
||||
|
||||
```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.
|
||||
</Admonition>
|
||||
|
||||
---
|
||||
|
||||
</CH.Scrollycoding>
|
||||
|
||||
Done! This is what our script and custom component look like:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue