cli pyproject updating (#12945)

`langchain app add` and `langchain app remove` will now keep the
dependencies list updated.

---------

Co-authored-by: Nuno Campos <nuno@boringbits.io>
This commit is contained in:
Erick Friis
2023-11-07 11:06:08 -08:00
committed by GitHub
parent d9abcf1aae
commit 74134dd7e1
8 changed files with 347 additions and 177 deletions

View File

@@ -1,5 +1,7 @@
from pathlib import Path
from typing import Optional, Set
from typing import Any, Dict, Optional, Set, TypedDict
from tomlkit import load
def get_package_root(cwd: Optional[Path] = None) -> Path:
@@ -14,3 +16,30 @@ def get_package_root(cwd: Optional[Path] = None) -> Path:
return package_root
package_root = package_root.parent
raise FileNotFoundError("No pyproject.toml found")
class LangServeExport(TypedDict):
"""
Fields from pyproject.toml that are relevant to LangServe
Attributes:
module: The module to import from, tool.langserve.export_module
attr: The attribute to import from the module, tool.langserve.export_attr
package_name: The name of the package, tool.poetry.name
"""
module: str
attr: str
package_name: str
def get_langserve_export(filepath: Path) -> LangServeExport:
with open(filepath) as f:
data: Dict[str, Any] = load(f)
try:
module = data["tool"]["langserve"]["export_module"]
attr = data["tool"]["langserve"]["export_attr"]
package_name = data["tool"]["poetry"]["name"]
except KeyError as e:
raise KeyError("Invalid LangServe PyProject.toml") from e
return LangServeExport(module=module, attr=attr, package_name=package_name)