If we assume absolutely nothing about the $\{a_i\}_{i=1}^N$...
Make a graph. The set of vertices of the graph, $V$, are the $a_i$. Add an edge between the vertices $a_i$ and $a_j$ labelled $\gcd(a_i,a_j)$. It is convenient to keep the collection of edges, $E$, and also, for each distinct $\gcd$ the set of labels having that $\gcd$, call it $E_g$ for $g \in \{\gcd(a_i,a_j) \mid a_i,a_j \in V\}$.
For each edge $e \in E$, labelled $t$, construct the graph with edges $e$ and the union of all the $E_g$ with $\gcd(g,t)=1$. (It may be helpful to construct the graph of pairs of $E_g$ that have relatively prime labels and cache these unions, since you may need them many times.) Count the number of triangles in this constructed graph. (Note that the adjacency matrices used at the given link are the unions of the adjacency matrices for the various $E_g$.) Accumulate these counts for each $e$ to get the total count of coprime triples.
This algorithm can easily take $O(N^3)$ time and $O(N^2)$ space. The trivial solution (not described here) also takes $O(N^3)$ time but only $O(1)$ space. In spite of this, I expect the above to be much faster than the trivial solution given the amount of redundant work it avoids.
Aside: My first try at a solution counted triples of edges with label "1", but this is incorrect since $\{6,10,15\}$ would not be found by this method, assuming those integers are in the $a_i$.