From 0fa15d2d91cdc5a361f146ccbb8e798b0b3b4e7c Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Tue, 1 Aug 2023 22:18:54 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20feat(custom.py):=20add=20new=20?= =?UTF-8?q?custom=20component=20'YourComponent'=20to=20the=20project?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a new file 'custom.py' under the 'src/backend/langflow/components/custom_components' directory. The file contains the implementation of a custom component called 'YourComponent'. The 'YourComponent' class extends the 'CustomComponent' class from the 'langflow' library. It has a display name of "Custom Component" and a description of "My description". The component has a 'build_config' method that returns a configuration object with a single property 'url'. The 'url' property is multiline and required. The 'build' method of the component takes in a 'url' string, a 'llm' object of type 'BaseLLM', and a 'prompt' object of type 'PromptTemplate'. It makes a GET request to the provided 'url', runs the response text through an 'LLMChain' with the given 'llm' and 'prompt', and returns a 'Document' object with the resulting page content. --- .../components/custom_components/custom.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/backend/langflow/components/custom_components/custom.py diff --git a/src/backend/langflow/components/custom_components/custom.py b/src/backend/langflow/components/custom_components/custom.py new file mode 100644 index 000000000..3ebc5bda8 --- /dev/null +++ b/src/backend/langflow/components/custom_components/custom.py @@ -0,0 +1,21 @@ +from langflow import CustomComponent + +from langchain.llms.base import BaseLLM +from langchain.chains import LLMChain +from langchain import PromptTemplate +from langchain.schema import Document + +import requests + +class YourComponent(CustomComponent): + display_name: str = "Custom Component" + description: str = "My description" + + def build_config(self): + return { "url": { "multiline": True, "required": True } } + + def build(self, url: str, llm: BaseLLM, prompt: PromptTemplate) -> Document: + response = requests.get(url) + chain = LLMChain(llm=llm, prompt=prompt) + result = chain.run(response.text[:300]) + return Document(page_content=str(result)) From a09e57b9ef800b17cde806d4fcb9fb8b8f09dccb Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 1 Aug 2023 18:24:19 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=A7=20chore(=5F=5Fmain=5F=5F.py):?= =?UTF-8?q?=20update=20components=5Fpath=20default=20value=20to=20include?= =?UTF-8?q?=20"components"=20directory=20for=20better=20organization=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(custom.py):=20update=20description=20of=20?= =?UTF-8?q?YourComponent=20to=20be=20more=20descriptive=20and=20accurate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/__main__.py | 2 +- src/backend/langflow/components/custom_components/custom.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py index b20a2a902..58789908a 100644 --- a/src/backend/langflow/__main__.py +++ b/src/backend/langflow/__main__.py @@ -127,7 +127,7 @@ def serve( timeout: int = typer.Option(300, help="Worker timeout in seconds."), port: int = typer.Option(7860, help="Port to listen on.", envvar="LANGFLOW_PORT"), components_path: Optional[Path] = typer.Option( - Path(__file__).parent, + Path(__file__).parent / "components", help="Path to the directory containing custom components.", envvar="LANGFLOW_COMPONENTS_PATH", ), diff --git a/src/backend/langflow/components/custom_components/custom.py b/src/backend/langflow/components/custom_components/custom.py index 3ebc5bda8..cec2c7acd 100644 --- a/src/backend/langflow/components/custom_components/custom.py +++ b/src/backend/langflow/components/custom_components/custom.py @@ -7,12 +7,13 @@ from langchain.schema import Document import requests + class YourComponent(CustomComponent): display_name: str = "Custom Component" - description: str = "My description" + description: str = "Create any custom component you want!" def build_config(self): - return { "url": { "multiline": True, "required": True } } + return {"url": {"multiline": True, "required": True}} def build(self, url: str, llm: BaseLLM, prompt: PromptTemplate) -> Document: response = requests.get(url) From 5b9bed07b5d04b489a95e14933f38ad8c95df738 Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Tue, 1 Aug 2023 23:17:23 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=94=A5=20chore(custom.py):=20remove?= =?UTF-8?q?=20custom=20component=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The custom component file `custom.py` has been removed from the project as it is no longer needed. --- .../components/custom_components/custom.py | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 src/backend/langflow/components/custom_components/custom.py diff --git a/src/backend/langflow/components/custom_components/custom.py b/src/backend/langflow/components/custom_components/custom.py deleted file mode 100644 index cec2c7acd..000000000 --- a/src/backend/langflow/components/custom_components/custom.py +++ /dev/null @@ -1,22 +0,0 @@ -from langflow import CustomComponent - -from langchain.llms.base import BaseLLM -from langchain.chains import LLMChain -from langchain import PromptTemplate -from langchain.schema import Document - -import requests - - -class YourComponent(CustomComponent): - display_name: str = "Custom Component" - description: str = "Create any custom component you want!" - - def build_config(self): - return {"url": {"multiline": True, "required": True}} - - def build(self, url: str, llm: BaseLLM, prompt: PromptTemplate) -> Document: - response = requests.get(url) - chain = LLMChain(llm=llm, prompt=prompt) - result = chain.run(response.text[:300]) - return Document(page_content=str(result))