Skip to content

Commit

Permalink
Merge branch 'nc/playground' of https://github.com/langchain-ai/langs…
Browse files Browse the repository at this point in the history
…erve into nc/playground
  • Loading branch information
jacoblee93 committed Oct 19, 2023
2 parents c3b89fb + 6210639 commit f1d8e3e
Show file tree
Hide file tree
Showing 26 changed files with 1,802 additions and 331 deletions.
35 changes: 24 additions & 11 deletions langserve/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,46 @@
import mimetypes
import os
from string import Template
from typing import List
from typing import List, Type

from fastapi.responses import Response
from langchain.schema.runnable import Runnable

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel


class PlaygroundTemplate(Template):
delimiter = "____"


async def serve_playground(
runnable: Runnable, config_keys: List[str], base_url: str, file_path: str
runnable: Runnable,
input_schema: Type[BaseModel],
config_keys: List[str],
base_url: str,
file_path: str,
) -> Response:
local_file_path = os.path.join(
os.path.dirname(__file__),
"./playground/dist",
file_path or "index.html",
)
with open(local_file_path) as f:
html = f.read()
mime_type = mimetypes.guess_type(local_file_path)[0]
formatted = PlaygroundTemplate(html).substitute(
LANGSERVE_BASE_URL=base_url[1:] if base_url.startswith("/") else base_url,
LANGSERVE_CONFIG_SCHEMA=json.dumps(
runnable.config_schema(include=config_keys).schema()
),
LANGSERVE_INPUT_SCHEMA=json.dumps(runnable.input_schema.schema()),
)
if mime_type in ("text/html", "text/css", "application/javascript"):
res = PlaygroundTemplate(f.read()).substitute(
LANGSERVE_BASE_URL=base_url[1:]
if base_url.startswith("/")
else base_url,
LANGSERVE_CONFIG_SCHEMA=json.dumps(
runnable.config_schema(include=config_keys).schema()
),
LANGSERVE_INPUT_SCHEMA=json.dumps(input_schema.schema()),
)
else:
res = f.buffer.read()

return Response(formatted, media_type=mime_type)
return Response(res, media_type=mime_type)
165 changes: 0 additions & 165 deletions langserve/playground/dist/assets/index-37507859.js

This file was deleted.

216 changes: 216 additions & 0 deletions langserve/playground/dist/assets/index-38194dca.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion langserve/playground/dist/assets/index-b1108ade.css

This file was deleted.

Loading

0 comments on commit f1d8e3e

Please sign in to comment.