(CodeBlockExtractor.py): Add a new component 'Code Block Extractor' to extract code blocks from text for langflow application.

This commit is contained in:
ogabrielluiz 2024-06-07 14:40:39 -03:00
commit 578619a5e3
2 changed files with 29 additions and 4 deletions

8
poetry.lock generated
View file

@ -4395,13 +4395,13 @@ url = "src/backend/base"
[[package]]
name = "langfuse"
version = "2.35.0"
version = "2.35.2"
description = "A client library for accessing langfuse"
optional = false
python-versions = "<4.0,>=3.8.1"
files = [
{file = "langfuse-2.35.0-py3-none-any.whl", hash = "sha256:e9df2474a01f8e167b7b13674c554915415b27064e48ad207054475f7fa8f82d"},
{file = "langfuse-2.35.0.tar.gz", hash = "sha256:b1d4b478233eefbc8a6fc63ca00ca82f6afecf2b0fdc1835ca65e751cf901577"},
{file = "langfuse-2.35.2-py3-none-any.whl", hash = "sha256:d01a23842cab484594f03878aacb9732ef8fd361158eb819c7bf43f758a0954b"},
{file = "langfuse-2.35.2.tar.gz", hash = "sha256:32b2e6c5bc71b4efdc430c6b964ab1c1e1ba1e105a4a73912c38b3959dc4502d"},
]
[package.dependencies]
@ -10081,4 +10081,4 @@ local = ["ctransformers", "llama-cpp-python", "sentence-transformers"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.10,<3.13"
content-hash = "2ba268be17a69253c9631ec721ece465a85a22949c2df7c712b7aa12d1a002fa"
content-hash = "d87bda272f67450430630924263690c2ae62416d0b240e029baaa8da07154bec"

View file

@ -0,0 +1,25 @@
import re
from langflow.custom import Component
from langflow.field_typing import Input, Output, Text
class CodeBlockExtractor(Component):
display_name = "Code Block Extractor"
description = "Extracts code block from text."
inputs = [Input(name="text", field_type=Text, description="Text to extract code blocks from.")]
outputs = [Output(name="code_block", display_name="Code Block", method="get_code_block")]
def get_code_block(self) -> Text:
text = self.text.strip()
# Extract code block
# It may start with ``` or ```language
# It may end with ```
pattern = r"^```(?:\w+)?\s*\n(.*?)(?=^```)```"
match = re.search(pattern, text, re.MULTILINE)
code_block = ""
if match:
code_block = match.group(1)
return code_block