From 34be9577c2bb0086da29bbd8ab173854f354d618 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 7 Jul 2023 01:33:15 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=80=20refactor(constants.py):=20refact?= =?UTF-8?q?or=20YourComponent=20class=20to=20improve=20readability=20and?= =?UTF-8?q?=20remove=20unused=20imports=20=F0=9F=94=80=20refactor(constant?= =?UTF-8?q?s.py):=20refactor=20build=20method=20in=20YourComponent=20class?= =?UTF-8?q?=20to=20use=20PromptTemplate=20and=20limit=20response=20text=20?= =?UTF-8?q?length=20The=20YourComponent=20class=20in=20constants.py=20has?= =?UTF-8?q?=20been=20refactored=20to=20improve=20readability=20and=20remov?= =?UTF-8?q?e=20unused=20imports.=20The=20build=20method=20now=20uses=20Pro?= =?UTF-8?q?mptTemplate=20to=20handle=20the=20template=20parameter=20and=20?= =?UTF-8?q?limits=20the=20length=20of=20the=20response=20text=20to=20300?= =?UTF-8?q?=20characters=20to=20avoid=20potential=20issues.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/interface/custom/constants.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/interface/custom/constants.py b/src/backend/langflow/interface/custom/constants.py index 9e7b27750..b99dd7bfb 100644 --- a/src/backend/langflow/interface/custom/constants.py +++ b/src/backend/langflow/interface/custom/constants.py @@ -21,20 +21,30 @@ LANGCHAIN_BASE_TYPES = { "Embeddings": Embeddings, "BaseRetriever": BaseRetriever, } + + DEFAULT_CUSTOM_COMPONENT_CODE = """ +from langflow import Prompt +from langchain.llms.base import BaseLLM from langchain.chains import LLMChain from langflow.interface.custom import CustomComponent +from langchain import PromptTemplate from langchain.schema import Document import requests -class YourComponent(CustomComponent): +class YourComponent: display_name: str = "Your Component" description: str = "Your description" field_config = { "url": { "multiline": True, "required": True } } - def build(self, url: str, llm: BaseLLM, prompt: prompt) -> Document: + def build(self, url: str, llm: BaseLLM, template: Prompt) -> Document: response = requests.get(url) + prompt = PromptTemplate.from_template(template) chain = LLMChain(llm=llm, prompt=prompt) - result = chain.run(response.text) + result = chain.run(response.text[:300]) return Document(page_content=str(result)) """ + + +# Create a new class that can be used as a type +# that returns type "prompt" if we get a certain param