langchain/libs/cli/langchain_cli/dev_scripts.py
Erick Friis 4421ba46d7
Demo Server, Fix Timescale (#12746)
- improve demo server
- missing deps
2023-11-01 15:29:34 -07:00

32 lines
860 B
Python

"""
Development Scripts for template packages
"""
from fastapi import FastAPI
from langserve import add_routes
from langserve.packages import get_langserve_export
from langchain_cli.utils.packages import get_package_root
def create_demo_server():
"""
Creates a demo server for the current template.
"""
app = FastAPI()
package_root = get_package_root()
pyproject = package_root / "pyproject.toml"
try:
package = get_langserve_export(pyproject)
mod = __import__(package["module"], fromlist=[package["attr"]])
chain = getattr(mod, package["attr"])
add_routes(app, chain)
except KeyError as e:
raise KeyError("Missing fields from pyproject.toml") from e
except ImportError as e:
raise ImportError("Could not import module defined in pyproject.toml") from e
return app