🔀 refactor(constants.py): refactor YourComponent class to improve readability and remove unused imports

🔀 refactor(constants.py): refactor build method in YourComponent class to use PromptTemplate and limit response text length
The YourComponent class in constants.py has been refactored to improve readability and remove unused imports. The build method now uses PromptTemplate to handle the template parameter and limits the length of the response text to 300 characters to avoid potential issues.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-07 01:33:15 -03:00
commit 34be9577c2

View file

@ -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