Skip to content

Commit

Permalink
Add support for API route via prefix (#236)
Browse files Browse the repository at this point in the history
* Fix serving of playground assets when using an API router with a
custom prefix

---------

Co-authored-by: Andreas Hildebrandt <[email protected]>
  • Loading branch information
eyurtsev and anhi authored Nov 16, 2023
1 parent 65dfbce commit f0ed983
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,17 @@ async def playground(
request=request,
per_req_config_modifier=per_req_config_modifier,
)

if isinstance(app, FastAPI): # type: ignore
base_url = f"{namespace}/playground"
else:
base_url = f"{app.prefix}{namespace}/playground"

return await serve_playground(
runnable.with_config(config),
runnable.with_config(config).input_schema,
config_keys,
f"{namespace}/playground",
base_url,
file_path,
)

Expand Down
21 changes: 21 additions & 0 deletions tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@ def test_serve_playground(app: FastAPI) -> None:
assert response.status_code == 404


@pytest.mark.asyncio
async def test_serve_playground_with_api_router() -> None:
"""Test serving playground from an api router with a prefix."""
app = FastAPI()

# Make sure that we can add routers
# to an API router
router = APIRouter(prefix="/langserve_runnables")

add_routes(
router,
RunnableLambda(lambda foo: "hello"),
path="/chat",
)

app.include_router(router)
async_client = AsyncClient(app=app, base_url="http://localhost:9999")
response = await async_client.get("/langserve_runnables/chat/playground/index.html")
assert response.status_code == 200


@pytest.mark.asyncio
async def test_server_async(app: FastAPI) -> None:
"""Test the server directly via HTTP requests."""
Expand Down

0 comments on commit f0ed983

Please sign in to comment.