From 0fa15d2d91cdc5a361f146ccbb8e798b0b3b4e7c Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Tue, 1 Aug 2023 22:18:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(custom.py):=20add=20new=20cust?= =?UTF-8?q?om=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))