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
|
|
|
"""
|
|
|
|
|
2023-11-02 18:42:57 +00:00
|
|
|
from typing import Sequence
|
|
|
|
|
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
|
2023-10-27 00:01:31 +00:00
|
|
|
|
2023-11-07 19:06:08 +00:00
|
|
|
from langchain_cli.utils.packages import get_langserve_export, get_package_root
|
2023-10-25 18:06:58 +00:00
|
|
|
|
|
|
|
|
2023-11-02 18:42:57 +00:00
|
|
|
def create_demo_server(
|
|
|
|
*,
|
|
|
|
config_keys: Sequence[str] = (),
|
2024-03-26 22:08:56 +00:00
|
|
|
playground_type: str = "default",
|
2023-11-02 18:42:57 +00:00
|
|
|
):
|
2023-10-25 18:06:58 +00:00
|
|
|
"""
|
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"])
|
2023-11-02 18:42:57 +00:00
|
|
|
add_routes(
|
|
|
|
app,
|
|
|
|
chain,
|
|
|
|
config_keys=config_keys,
|
2024-03-26 22:08:56 +00:00
|
|
|
playground_type=playground_type,
|
2023-11-02 18:42:57 +00:00
|
|
|
)
|
2023-11-01 22:29:34 +00:00
|
|
|
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
|
2023-11-02 18:42:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_demo_server_configurable():
|
|
|
|
return create_demo_server(config_keys=["configurable"])
|
2024-03-26 22:08:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_demo_server_chat():
|
|
|
|
return create_demo_server(playground_type="chat")
|