Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for API route via prefix #236

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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