Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Improved Results with Vector Search in Knowledg...

Jennifer Reif
February 09, 2024

Improved Results with Vector Search in Knowledge Graphs

Vector search within knowledge graphs enables users to improve responses from their applications by combining the natural-language strength of an LLM and data accuracy of a graph. In this presentation, we will discuss what vector search is, what it looks like in a graph, how to add vectors to enhance the data, and how to use an LLM with graph vector search to harness relevant and contextual responses to questions.

Code repository: https://www.github.com/JMHReif/springai-goodreads

Jennifer Reif

February 09, 2024
Tweet

More Decks by Jennifer Reif

Other Decks in Technology

Transcript

  1. 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
  2. Kings and Queens king − man + woman ≈ queen

    king man wom an 1 king man wom an 2 queen? 3
  3. 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
  4. Neighborhoods Cosine vs euclidean • cosine • direction / angle

    based vector point query nearest 4 • Euclidean • distance based
  5. 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
  6. Data + Vectors How do I get vectors for existing

    data? • Generate some vector embeddings • Several models available • Store embedding as property on node
  7. APOC example WITH "<apiKey>" 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{.*};
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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.
  13. 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
  14. 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