Skip to content

Commit

Permalink
Graph query working v1
Browse files Browse the repository at this point in the history
  • Loading branch information
maxluk committed Oct 10, 2024
1 parent 52e8769 commit 5d21e50
Show file tree
Hide file tree
Showing 7 changed files with 814 additions and 33 deletions.
45 changes: 45 additions & 0 deletions demo/graphrag_query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
WITH
embedding_query AS (
SELECT azure_openai.create_embeddings('text-embedding-3-small', query)::vector AS embedding
),
vector AS (
SELECT cases.id, RANK() OVER (ORDER BY description_vector <=> embedding) AS vector_rank
FROM cases, embedding_query
WHERE (cases.data#>>'{court, id}')::integer IN (9029, 8985) -- Washington Supreme Court (9029) or Washington Court of Appeals (8985)
ORDER BY description_vector <=> embedding
LIMIT 50
),
semantic AS (
SELECT *
FROM semantic_relevance(query, 50)
),
semantic_ranked AS (
SELECT semantic.relevance::DOUBLE PRECISION AS relevance,
FROM vector
JOIN semantic ON vector.vector_rank = semantic.ordinality
ORDER BY semantic.relevance DESC
),
graph AS (
SELECT * from semantic_ranked
JOIN cypher('case_graph', $$
MATCH ()-[r]->(n)
RETURN n.case_id, COUNT(r) AS refs
$$) as graph_query(case_id TEXT, refs BIGINT)
ON semantic_ranked.id = graph_query.case_id
),
graph_ranked AS (
SELECT RANK() OVER (ORDER BY graph.refs DESC) AS graph_rank
FROM graph ORDER BY graph_rank DESC
),
rrf AS (
SELECT
COALESCE(1.0 / (60 + graph_ranked.graph_rank), 0.0) +
COALESCE(1.0 / (60 + semantic_ranked.semantic_rank), 0.0) AS score,
semantic_ranked.*
FROM graph_ranked
JOIN semantic_ranked ON graph_ranked.id = semantic_ranked.id
ORDER BY score DESC
LIMIT 20
)
SELECT *
FROM rrf;
91 changes: 71 additions & 20 deletions playground/case_graph.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,58 @@ LOAD 'age';
SET search_path = ag_catalog, "$user", public;

-- search
SELECT * from cypher('graph_name', $$
MATCH (n:label)
RETURN n
$$) as (n agtype);


SELECT * from cypher('graph_name', $$
MATCH (n {property:"Node B"})
RETURN n
$$) as (n agtype);

SELECT * from cypher('graph_name', $$
MATCH ()-[r]->(n {property:"Node A"})
RETURN COUNT(r) AS refs
$$) as (refs BIGINT);

SELECT * from cypher('graph_name', $$
WITH
embedding_query AS (
SELECT azure_openai.create_embeddings('text-embedding-3-small', 'Water leaking into the apartment from the floor above.')::vector AS embedding
),
vector AS (
SELECT cases.id, RANK() OVER (ORDER BY description_vector <=> embedding) AS vector_rank
FROM cases, embedding_query
WHERE (cases.data#>>'{court, id}')::integer IN (9029)--, 8985) -- Washington Supreme Court (9029) or Washington Court of Appeals (8985)
ORDER BY description_vector <=> embedding
LIMIT 10
),
semantic AS (
SELECT *
FROM jsonb_array_elements(
semantic_relevance('Water leaking into the apartment from the floor above.',
10)
) WITH ORDINALITY AS elem(relevance)
),
semantic_ranked AS (
SELECT semantic.relevance::DOUBLE PRECISION AS relevance, semantic.*, vector.*
FROM vector
JOIN semantic ON vector.vector_rank = semantic.ordinality
ORDER BY semantic.relevance DESC
),
graph AS (
SELECT * from semantic_ranked
JOIN cypher('case_graph', $$
MATCH ()-[r]->(n)
WHERE n.property IN ["Node A", "Node B"]
RETURN n.property, COUNT(r) AS refs
$$) as (node TEXT, refs BIGINT);
RETURN n.case_id, COUNT(r) AS refs
$$) as graph_query(case_id TEXT, refs BIGINT)
ON semantic_ranked.id = graph_query.case_id
)
SELECT * FROM graph;


graph_ranked AS (
SELECT RANK() OVER (ORDER BY graph.refs DESC) AS graph_rank, semantic_ranked.*
FROM graph ORDER BY graph_rank DESC
),
rrf AS (
SELECT
COALESCE(1.0 / (60 + graph_ranked.graph_rank), 0.0) +
COALESCE(1.0 / (60 + semantic_ranked.semantic_rank), 0.0) AS score,
semantic_ranked.*
FROM graph_ranked
JOIN semantic_ranked ON graph_ranked.id = semantic_ranked.id
ORDER BY score DESC
LIMIT 20
)
SELECT *
FROM rrf;

-- query edges in cases table
SELECT c1.id AS id_from, c1.data ->> 'name_abbreviation', cites_to_element ->> 'cite' AS cite_to_id, c2.id AS id_to
FROM cases c1
Expand All @@ -48,6 +78,27 @@ WHERE data ->> 'name_abbreviation'::text LIKE '%Dubreuil%';
-- creation
SELECT create_graph('case_playground_graph');

SELECT *
FROM cypher('graph_name', $$
MATCH (a:case), (b:case)
WHERE a.case_id = '670242' AND b.case_id = '591482'
CREATE d = (a)-[e:RELTYPE]->(b)
RETURN d
$$) as (d agtype);

commit;

SELECT * from cypher('graph_name', $$
MATCH (r:case)
RETURN r
$$) as (r agtype);

SELECT * from cypher('graph_name', $$
MATCH ()-[r]->(n)
WHERE n.case_id IN ['782330', '615468']
RETURN r
$$) as (r agtype);

WITH cases_data AS (
SELECT id FROM cases_playground
)
Expand Down
Loading

0 comments on commit 5d21e50

Please sign in to comment.