2023-10-25 18:06:58 +00:00
|
|
|
"""
|
2023-10-30 23:39:39 +00:00
|
|
|
Development Scripts for template packages
|
2023-10-25 18:06:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
2023-11-01 22:29:34 +00:00
|
|
|
from langserve import add_routes
|
|
|
|
from langserve.packages import get_langserve_export
|
2023-10-27 00:01:31 +00:00
|
|
|
|
2023-10-25 18:06:58 +00:00
|
|
|
from langchain_cli.utils.packages import get_package_root
|
|
|
|
|
|
|
|
|
|
|
|
def create_demo_server():
|
|
|
|
"""
|
2023-10-30 23:39:39 +00:00
|
|
|
Creates a demo server for the current template.
|
2023-10-25 18:06:58 +00:00
|
|
|
"""
|
|
|
|
app = FastAPI()
|
|
|
|
package_root = get_package_root()
|
2023-11-01 22:29:34 +00:00
|
|
|
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
|
|
|
|
|
2023-10-25 18:06:58 +00:00
|
|
|
return app
|