def build_factory_api_endpoint(
    path: str, endpoint: Callable[..., Any]
) -> Callable[..., Awaitable[Any]]:
    """Build a factory endpoint for API routes.

    Args:
        path: The path for the route.
        endpoint: LangChain instance factory function.
    """
    chain = compile_chain_factory(endpoint)

    # index 1 of `compile_path` contains path_format output
    model_prefix = compile_model_prefix(compile_path(path)[1], chain)
    request_model = create_request_model(chain, model_prefix)

    callbacks = get_streaming_callbacks(chain)

    async def factory_endpoint(
        request: request_model, chain: Chain = Depends(endpoint)
    ):
        return StreamingResponse(
            chain=chain, config={"inputs": model_dump(request), "callbacks": callbacks}
        )

    return factory_endpoint
Build a factory endpoint for API routes.

    Args:
        path: The path for the route.
        endpoint: LangChain instance factory function.
    
lanarky/adapters/langchain/utils.py