✨ feat(constants.py): update display name and description of the custom component to improve clarity and user experience 🔧 chore(custom_components.py): update display name of the custom component to improve clarity and user experience
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from langchain import PromptTemplate
|
|
from langchain.chains.base import Chain
|
|
from langchain.document_loaders.base import BaseLoader
|
|
from langchain.embeddings.base import Embeddings
|
|
from langchain.llms.base import BaseLLM
|
|
from langchain.schema import BaseRetriever, Document
|
|
from langchain.text_splitter import TextSplitter
|
|
from langchain.tools import Tool
|
|
from langchain.vectorstores.base import VectorStore
|
|
|
|
|
|
LANGCHAIN_BASE_TYPES = {
|
|
"Chain": Chain,
|
|
"Tool": Tool,
|
|
"BaseLLM": BaseLLM,
|
|
"PromptTemplate": PromptTemplate,
|
|
"BaseLoader": BaseLoader,
|
|
"Document": Document,
|
|
"TextSplitter": TextSplitter,
|
|
"VectorStore": VectorStore,
|
|
"Embeddings": Embeddings,
|
|
"BaseRetriever": BaseRetriever,
|
|
}
|
|
|
|
# Langchain base types plus Python base types
|
|
CUSTOM_COMPONENT_SUPPORTED_TYPES = {
|
|
**LANGCHAIN_BASE_TYPES,
|
|
"str": str,
|
|
"int": int,
|
|
"float": float,
|
|
"bool": bool,
|
|
"list": list,
|
|
"dict": dict,
|
|
}
|
|
|
|
|
|
DEFAULT_CUSTOM_COMPONENT_CODE = """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))
|
|
"""
|