Slide 1

Slide 1 text

Improved Results With Vector Search in Knowledge Graphs Jennifer Reif [email protected] @JMHReif github.com/JMHReif jmhreif.com linkedin.com/in/jmhreif Photo by Steve Johnson on Unsplash

Slide 2

Slide 2 text

What is Vector Search?

Slide 3

Slide 3 text

What is a vector? • Length • Direction • Components have meaning horizontal vertical

Slide 4

Slide 4 text

Vector Example - Shapes Length Width

Slide 5

Slide 5 text

Vector Example - Shapes Length Width

Slide 6

Slide 6 text

Vector arithmetic 1 a b 2 a b 3 a + b

Slide 7

Slide 7 text

Kings and Queens king − man + woman ≈ queen king man wom an 1 king man wom an 2 queen? 3

Slide 8

Slide 8 text

What are vector embeddings? Convert something to point in space • Same concepts, applied to data formats • 100s or 1000s of dimensions • Dimension = interesting feature/characteristic

Slide 9

Slide 9 text

Neighborhoods Cosine vs euclidean ● cosine ● direction / angle based vector point query nearest 4 ● Euclidean ● distance based

Slide 10

Slide 10 text

Vector index • Queries become expensive • Need to compare every vector to query • Indexes = speed • Jump right where you need (like index in a book) • Approximate nearest neighbor (k-ANN) • 20 closest vectors to this one

Slide 11

Slide 11 text

How to Add Vectors …to existing data

Slide 12

Slide 12 text

Data model

Slide 13

Slide 13 text

Data + Vectors How do I get vectors for existing data? • Generate some vector embeddings • Several models available • Store embedding as property on node

Slide 14

Slide 14 text

Examples OpenAI

Slide 15

Slide 15 text

APOC example WITH "" as apiKey MATCH (r:Review) WITH apiKey, r CALL { WITH apiKey, r CALL apoc.ml.openai.embedding([r.text], apiKey, {}) yield index, text, embedding CALL db.create.setNodeVectorProperty(r, 'embedding', embedding) RETURN r as review } RETURN review{.*};

Slide 16

Slide 16 text

Create and query vector index CALL db.index.vector.createNodeIndex('spring-ai-document-index', 'Review', 'embedding', 1536, 'cosine'); CALL db.index.vector.queryNodes('spring-ai-document-index', 10, $queryVector) YIELD node AS similarReviews, score MATCH (similarReviews)-[:WRITTEN_FOR]->(b:Book) RETURN b.title as title, avg(score) AS score ORDER BY score DESC LIMIT 10

Slide 17

Slide 17 text

Create and query vector index CALL db.index.vector.createNodeIndex( 'index-name', 'Label', 'propertyKey', 1536, 'cosine') CALL db.index.vector.queryNodes('index-name', 1000, $queryVector) valid: ● 'euclidean' ● 'cosine' OpenAI gen2 dimensions nearest 1000 to queryVector ● parameter ● alias ● property ● manually typed 😬 good luck Full documentation in Cypher manual https://neo4j.com/docs/cypher-manual/current/indexes-for-vector-search/ same name

Slide 18

Slide 18 text

Use LLM with Graph Vector Search

Slide 19

Slide 19 text

RAG • Retrieval: • Data retrieved from database • Augmented: • Augments response with facts • Generation: • Response in natural language Prompt + Relevant Information LLM API LLM
 Chat API User Database Search Prompt Response Relevant Results / Documents 2 3 1 Database

Slide 20

Slide 20 text

How much value can RAG add?

Slide 21

Slide 21 text

Explainable AI With RAG + LLM • How did the LLM get this answer? • Graphs: • Opportunity to log answers alongside context • Visualize conversations • Tools to analyze LLM performance

Slide 22

Slide 22 text

Example data model Agent Neo

Slide 23

Slide 23 text

Logging and Visualizing Conversations In same database as context Graph of an actual conversation between an Agent Neo user and the ChatGPT-4 LLM.
 Context Documents are labeled with their GDS Community.

Slide 24

Slide 24 text

Graph + Vector = Semantic Search Combine for more accurate results within a relevant context. Knowledge Graph Similarity Search Find similar documents. Vector Index Find related information. Graph Structure Pattern Matching

Slide 25

Slide 25 text

Demo Time!

Slide 26

Slide 26 text

Resources • Github repository (today’s code): github.com/JMHReif/springai-goodreads • GraphAcademy LLM courses: graphacademy.neo4j.com/categories/llms/ • Spring AI guide: neo4j.com/labs/genai-ecosystem/spring-ai • Docs for Spring AI: docs.spring.io/spring-ai/reference/api/vectordbs/neo4j.html • Docs for OpenAI embeddings: platform.openai.com/docs/guides/embeddings Jennifer Reif [email protected] @JMHReif github.com/JMHReif jmhreif.com linkedin.com/in/jmhreif