Split Langflow into Langflow and Langflow Base (#1562)
* Initial Restructure * Replace import langflow for import langflow_base * Fix dependencies * 🔧 chore(Makefile): refactor build process to separate base and frontend builds for better organization and maintainability * 🚀 chore(Makefile): update build_frontend command to copy frontend build to the correct directory 🔖 chore(pyproject.toml): update python and httpx dependencies versions 🔧 chore(__init__.py): update import statement for load_flow_from_json function * 🔖 chore(pyproject.toml): update package version from 0.0.6 to 0.0.8 to reflect changes in the codebase * 🚀 feat(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics 🚀 feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port * 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics ✨ feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port 🚚 chore(pyproject.toml): update langflow-base version from 0.0.8 to 0.0.10 ✨ feat(server.ts): add new agent component LCAgentComponent to langflow_base ✨ feat(server.ts): add new model component LCModelComponent to langflow_base ✨ feat(server.ts): add new helper functions docs_to_records and records_to_text to langflow_base ✨ feat(server.ts): add new flow helper functions list_flows, load_flow, run_flow, generate_function_for_flow, get_flow_inputs, build_schema_from_inputs to langflow_base ✨ feat(server.ts): add new prompt component PromptComponent to langflow_base ✨ feat(server.ts): add new chat components ChatInput and ChatOutput to langflow_base ✨ feat(server.ts): add new model component OpenAIModelComponent to langflow_base 🚚 chore(main.py): update import path from langflow.main to langflow_base.main 🚚 chore(service.py): update import path from langflow.services.database.manager to langflow_base.services.database.manager 🚚 chore(factory.py): update import path from langflow.services to langflow_base.services 🚚 chore(service.py): update import path from langflow.services.plugins to langflow_base.services.plugins 🚚 chore(utils.py): update import path from langflow.services to langflow_base.services 🚚 chore(validate.py): update import path from langflow.field_typing to langflow_base.field_typing 🚚 chore(pyproject.toml): update langflow-base version from 0.0.8 to 0.0.10 * Update Makefile to install backend dependencies and build langflow * Add langflow main module and update __init__.py * Update langflow install process to use implicit namespace * Add langflow-base as a local dependency * Add setup_poetry target to Makefile * Update Poetry version and add poetry-monorepo-dependency-plugin * Refactor code to improve performance and readability * Update imports to custom and load * Update content-hash in poetry.lock --------- Co-authored-by: Matheus <jacquesmats@gmail.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai>
This commit is contained in:
parent
9c0bad3883
commit
8172e62236
466 changed files with 5418 additions and 1144 deletions
125
src/backend/base/langflow/schema/schema.py
Normal file
125
src/backend/base/langflow/schema/schema.py
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
import copy
|
||||
|
||||
from langchain_core.documents import Document
|
||||
from pydantic import BaseModel, model_validator
|
||||
|
||||
|
||||
class Record(BaseModel):
|
||||
"""
|
||||
Represents a record with text and optional data.
|
||||
|
||||
Attributes:
|
||||
data (dict, optional): Additional data associated with the record.
|
||||
"""
|
||||
|
||||
data: dict = {}
|
||||
_default_value: str = ""
|
||||
|
||||
@model_validator(mode="before")
|
||||
def validate_data(cls, values):
|
||||
if not values.get("data"):
|
||||
values["data"] = {}
|
||||
# Any other keyword should be added to the data dictionary
|
||||
for key in values:
|
||||
if key not in values["data"] and key != "data":
|
||||
values["data"][key] = values[key]
|
||||
return values
|
||||
|
||||
@classmethod
|
||||
def from_document(cls, document: Document) -> "Record":
|
||||
"""
|
||||
Converts a Document to a Record.
|
||||
|
||||
Args:
|
||||
document (Document): The Document to convert.
|
||||
|
||||
Returns:
|
||||
Record: The converted Record.
|
||||
"""
|
||||
data = document.metadata
|
||||
data["text"] = document.page_content
|
||||
return cls(data=data)
|
||||
|
||||
def __add__(self, other: "Record") -> "Record":
|
||||
"""
|
||||
Concatenates the text of two records and combines their data.
|
||||
|
||||
Args:
|
||||
other (Record): The other record to concatenate with.
|
||||
|
||||
Returns:
|
||||
Record: The concatenated record.
|
||||
"""
|
||||
combined_data = {**self.data, **other.data}
|
||||
return Record(data=combined_data)
|
||||
|
||||
def to_lc_document(self) -> Document:
|
||||
"""
|
||||
Converts the Record to a Document.
|
||||
|
||||
Returns:
|
||||
Document: The converted Document.
|
||||
"""
|
||||
return Document(page_content=self.text, metadata=self.data)
|
||||
|
||||
def __getattr__(self, key):
|
||||
"""
|
||||
Allows attribute-like access to the data dictionary.
|
||||
"""
|
||||
try:
|
||||
if key == "data" or key.startswith("_"):
|
||||
return super().__getattr__(key)
|
||||
|
||||
return self.data.get(key, self._default_value)
|
||||
except KeyError:
|
||||
# Fallback to default behavior to raise AttributeError for undefined attributes
|
||||
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{key}'")
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
"""
|
||||
Allows attribute-like setting of values in the data dictionary,
|
||||
while still allowing direct assignment to class attributes.
|
||||
"""
|
||||
if key == "data" or key.startswith("_"):
|
||||
super().__setattr__(key, value)
|
||||
else:
|
||||
self.data[key] = value
|
||||
|
||||
def __delattr__(self, key):
|
||||
"""
|
||||
Allows attribute-like deletion from the data dictionary.
|
||||
"""
|
||||
if key == "data" or key.startswith("_"):
|
||||
super().__delattr__(key)
|
||||
else:
|
||||
del self.data[key]
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
"""
|
||||
Custom deepcopy implementation to handle copying of the Record object.
|
||||
"""
|
||||
cls = self.__class__
|
||||
result = cls.__new__(cls)
|
||||
memo[id(self)] = result
|
||||
for k, v in self.__dict__.items():
|
||||
setattr(result, k, copy.deepcopy(v, memo))
|
||||
return result
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
Returns a string representation of the Record, including text and data.
|
||||
"""
|
||||
# Assuming a method to dump model data as JSON string exists.
|
||||
# If it doesn't, you might need to implement it or use json.dumps() directly.
|
||||
# build the string considering all keys in the data dictionary
|
||||
prefix = "Record("
|
||||
suffix = ")"
|
||||
text = ", ".join([f"{k}={v}" for k, v in self.data.items()])
|
||||
return prefix + text + suffix
|
||||
|
||||
# check which attributes the Record has by checking the keys in the data dictionary
|
||||
def __dir__(self):
|
||||
return super().__dir__() + list(self.data.keys())
|
||||
|
||||
|
||||
INPUT_FIELD_NAME = "input_value"
|
||||
Loading…
Add table
Add a link
Reference in a new issue