Recommandations avec Neo4j et le GraphAware Recommendation Engine

62
GraphAware TM Christophe Willemsen graphaware.com @graph_aware Recommandations avec Neo4j Construction d’un moteur de recommandation haute-performance

Transcript of Recommandations avec Neo4j et le GraphAware Recommendation Engine

Page 1: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Christophe Willemsen

graphaware.com

@graph_aware

Recommandations avec Neo4j

Construction d’un moteur de recommandation haute-performance

Page 2: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Rapide introductionPourquoi les graphes ?Challenges Business et TechniquesGraphAware Recommendation Engine

A propos

Page 3: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Articles que vous pourriez lireLivres que vous pourriez acheterPersonnes que vous pourriez connaitrePersonnes que vous pourriez rencontrerPersonnes auxquelles vous pourriez présentervotre produit…

Recommandation

Page 4: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Approche contenu (caractéristiques)Filtrage collaboratif (relations utilisateur <-> produit)

Recommandation

Page 5: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Les caractéristiques tout comme les relations peuvent être représentées naturellement dans un graphe.

Bonnes nouvelles

Page 6: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Exemple

IS_OF_GENRE

title: “Love Actually”

Movie

name: “Bob”

User

name: “Comedy”

Genre

RATEDrating: 5

name: “Alice”

Username:

“Romance”

Genre

title: “American Pie”

Movie

IS_OF_GENRE

IS_OF_GENRE

RATEDrating: 5

INTERESTED_IN

rating: 5RATED

Page 7: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Facile à comprendreModélisation naturelleFlexible (schema-free)Temps de requête ultra-rapides

Graphes (Neo4j)

Page 8: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Bien pour un PoC rapideBien pour des petits jeux de donnéesBien pour des logiques relativement simples

Cypher

Page 9: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

CypherMATCH (u:User)-[:LIKED]->(m:Movie), (m)<-[:LIKED]-(another:User), (another)-[:LIKED]->(reco:Movie)

WHERE NOT (u)-[:LIKED|DISLIKED]->(reco)

RETURN reco;

Page 10: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Les spécificités d’un moteur de recommandation pour le monde réel sont souvent beaucoup plus complexes.

La réalité

Page 11: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Imaginez que vous construisez la fonction ”personnes que vous pourriez connaître” sur LinkedIn.

Exemple

Page 12: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Après une session de brainstorming, votre équipe définit de la façon suivante comment trouver des personnes que vous pourriez connaître:

Exemple

Page 13: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Contacts en communAmis Facebook en communsContacts email/mobile en communTous les autres contacts email/mobileTravaillé pour la même sociétéEtudié à la même écolePartage le même intérêtVit dans la même ville

Personnes que vous pourriez connaître

Page 14: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Mais c’est seulement le début!

Retournons en arrière et améliorons tout cela.

Page 15: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Plus de contacts en commun = meilleure chance?Même ville / école / société = la taille compte?Qu’en est-t-il des emails qui ne représentent pas une personne ?Et les personnes déjà connectées? Et celles en attente… Et celles rejetées…Et celles ignorées répétivement…

Personnes que vous pourriez connaître

Page 16: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Trouver des “choses” à recommanderServir les recommandations les plus rélévantesMesurer la qualité des recommandationsTime to market / coût du développement

Challenges Business

Page 17: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Performance (real-time!)

Challenges Techniques

Page 18: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Mauvaise Recommandation

Page 19: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Mauvaise Recommandation

Page 20: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Performance (real-time!)SimplicitéFlexibilité

Challenges Techniques

Page 21: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Alors on a sorti une architecture de base open-source d’un moteur de recommandation qui vous aidera à résoudre tous ces challenges.

On l’a fait

Page 22: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

plugin Neo4j (utilise GraphAware Framework)vous devez utiliser un langage JVMarchitecture dogmatiqueultra rapideultra flexiblegère toute l’encapsulation

Moteur de recommandation

Page 23: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Un moteur par “raison” de recommandation (core logic)Le moteur effectuée une traversée dans le graphe pour trouver des produits/personnes/..Les moteurs sont assemblés ensemble dans un moteur de haut-niveau

Décisions d’architecture

Page 24: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Exemple

IS_OF_GENRE

title: “Love Actually”

Movie

name: “Bob”

User

name: “Comedy”

Genre

RATEDrating: 5

name: “Alice”

Username:

“Romance”

Genre

title: “American Pie”

Movie

IS_OF_GENRE

IS_OF_GENRE

RATEDrating: 5

INTERESTED_IN

rating: 5RATED

Page 25: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Un moteur par “raison” de recommandation (core logic)Le moteur effectuée une traversée dans le graphe pour trouver des produits/personnes/..Les moteurs sont assemblés ensemble dans un moteur de haut-niveauLes produits trouvés plusieurs fois sont plus rélévantsLa rélévance dépend de la vitesse à laquelle un produit a été trouvé

Décisions d’architecture

Page 26: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Exemple

IS_OF_GENRE

title: “Love Actually”

Movie

name: “Bob”

User

name: “Comedy”

Genre

RATEDrating: 5

name: “Alice”

Username:

“Romance”

Genre

title: “American Pie”

Movie

IS_OF_GENRE

IS_OF_GENRE

RATEDrating: 5

INTERESTED_IN

rating: 5RATED

Page 27: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Un moteur par “raison” de recommandation (core logic)Le moteur effectuée une traversée dans le graphe pour trouver des produits/personnes/..Les moteurs sont assemblés ensemble dans un moteur de haut-niveauLes produits trouvés plusieurs fois sont plus rélévantsLa rélévance dépend de la vitesse à laquelle un produit a été trouvéProduits qui ne doivent pas être recommandés

Décisions d’architecture

Page 28: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Example

IS_OF_GENRE

title: “Love Actually”

Movie

name: “Bob”

User

name: “Comedy”

Genre

RATEDrating: 5

name: “Alice”

Username:

“Romance”

Genre

title: “American Pie”

Movie

IS_OF_GENRE

IS_OF_GENRE

RATEDrating: 5

INTERESTED_IN

rating: 5RATED

Page 29: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Input -> Engine -> RecommandationsScores et Score TransformersBlacklistsFiltersPost-processorsContext (combien, en combien de temps,…?)Loggers

Architecture

Page 30: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

En 5 minutes, on va construire un moteur de recommandation qui vous proposera des personnes avec qui vous pourriez vous connecter.

Let’s code

Page 31: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

0) Modèle

Page 32: Recommandations avec Neo4j et le GraphAware Recommendation Engine
Page 33: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

1) Découverte

Page 34: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public class FriendsInCommon extends SomethingInCommon { @Override public String name() { return "friendsInCommon"; } @Override protected RelationshipType getType() { return FRIEND_OF; } @Override protected Direction getDirection() { return BOTH; }}

Page 35: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

2) Score

Page 36: Recommandations avec Neo4j et le GraphAware Recommendation Engine
Page 37: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public class FriendsInCommon extends SomethingInCommon {

@Override protected ScoreTransformer scoreTransformer() { return new ParetoScoreTransformer(100, 10); } @Override public String name() { return "friendsInCommon"; } @Override protected RelationshipType getType() { return FRIEND_OF; } @Override protected Direction getDirection() { return BOTH; }}

Page 38: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

3) Post-Process

Page 39: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public class RewardSameLocation extends RewardSomethingShared { @Override protected RelationshipType type() { return LIVES_IN; } @Override protected Direction direction() { return OUTGOING; } @Override protected float scoreValue(Node reco, Node in, Node shared) { return 10; } @Override protected String scoreName() { return "sameLocation"; }}

Page 40: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public class RewardSameLabels implements PostProcessor<Node, Node> { @Override public void postProcess(Recommendations<Node> out, Node in) { Label[] inLabels = toArray(in.getLabels()); for (Recommendation<Node> reco : out.get()) { if (Arrays.equals(inLabels, toArray(reco.getItem().getLabels()))) { reco.add("sameGender", 10); } } }}

Page 41: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

4) Filter

Page 42: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

... @Override protected List<BlacklistBuilder<Node, Node>> blacklistBuilders() { return asList( new ExistingRelationshipBlacklistBuilder(FRIEND_OF, BOTH) ); } @Override protected List<Filter<Node, Node>> filters() { return asList( new ExcludeSelf() ); }

Page 43: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

5) Assemble

Page 44: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public final class FriendsComputingEngine extends Neo4jTopLevelDelegatingEngine { @Override protected List<RecommendationEngine<Node, Node>> engines() { return Arrays.<RecommendationEngine<Node, Node>>asList( new FriendsInCommon(), new RandomPeople() ); } @Override protected List<PostProcessor<Node, Node>> postProcessors() { return Arrays.asList( new RewardSameLabels(), new RewardSameLocation(), new PenalizeAgeDifference() ); } @Override protected List<BlacklistBuilder<Node, Node>> blacklistBuilders() { return Arrays.asList( new ExistingRelationshipBlacklistBuilder(FRIEND_OF, BOTH) ); } @Override protected List<Filter<Node, Node>> filters() { return Arrays.<Filter<Node, Node>>asList( new ExcludeSelf() ); }}

Page 45: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

?) Precompute

Page 46: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public final class FriendsComputingEngine extends Neo4jTopLevelDelegatingEngine { public FriendsComputingEngine() { super(new FriendsContextFactory()); } @Override protected List<RecommendationEngine<Node, Node>> engines() { return asList( new FriendsInCommon(), new RandomPeople() ); } @Override protected List<PostProcessor<Node, Node>> postProcessors() { return asList( new RewardSameLabels(), new RewardSameLocation(), new PenalizeAgeDifference() ); } @Override public ParticipationPolicy<Node, Node> participationPolicy(Context<Node, Node> context) { return ParticipationPolicy.IF_MORE_RESULTS_NEEDED; }}

Page 47: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public final class FriendsRecommendationEngine extends Neo4jTopLevelDelegatingEngine { @Override protected List<RecommendationEngine<Node, Node>> engines() { return Arrays.<RecommendationEngine<Node, Node>>asList( new Neo4jPrecomputedEngine(), new FriendsComputingEngine() ); } @Override protected List<BlacklistBuilder<Node, Node>> blacklistBuilders() { return Arrays.asList( new ExistingRelationshipBlacklistBuilder(FRIEND_OF, BOTH) ); } @Override protected List<Filter<Node, Node>> filters() { return Arrays.<Filter<Node, Node>>asList( new ExcludeSelf() ); }}

Page 48: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

6) Log

Page 49: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

public final class FriendsRecommendationEngine extends Neo4jTopLevelDelegatingEngine { @Override protected List<RecommendationEngine<Node, Node>> engines() { return Arrays.<RecommendationEngine<Node, Node>>asList( new Neo4jPrecomputedEngine(), new FriendsComputingEngine() ); } @Override protected List<BlacklistBuilder<Node, Node>> blacklistBuilders() { return Arrays.asList( new ExistingRelationshipBlacklistBuilder(FRIEND_OF, BOTH) ); } @Override protected List<Filter<Node, Node>> filters() { return Arrays.<Filter<Node, Node>>asList( new ExcludeSelf() ); } @Override protected List<Logger<Node, Node>> loggers() { return Arrays.asList( new Slf4jRecommendationLogger<Node, Node>(), new Slf4jStatisticsLogger<Node, Node>() ); }}

Page 50: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

7) Test

Page 51: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

List<Recommendation<Node>> reco = recommendationEngine.recommend(getPersonByName(“Adam"), 2);String expected = "(Vince {total:19.338144," + "ageDifference:-5.527864," + "friendsInCommon:14.866008," + "sameGender:10.0})," + "(Luanne {total:11.553411," + "ageDifference:-3.312597," + "friendsInCommon:14.866008})"; assertEquals(expected, toString(reco));

Page 52: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Trouver des “choses” à recommanderServir les recommandations les plus rélévantesMesurer la qualité des recommandationsTime to market / coût du développement

Challenges Business

Page 53: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Performance (real-time!)SimplicitéFlexibilité

Challenges Techniques

Page 54: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

A vous !<dependencies>

...

<dependency> <groupId>com.graphaware.neo4j</groupId> <artifactId>recommendation-engine</artifactId> <version>2.2.0.30.6</version> </dependency>

...

<dependencies>

Page 55: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Habilité intégrée pour pré-computer les recommandationsAutres classes intégréesTime-based ParticipationPolicy Raisons for RecommendationContribuez !https://github.com/graphaware/neo4j-reco

Il y a encore plus !

Page 56: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Algorithmes intégrésIntégration avec des compute enginesMachine learning

Futur

Page 57: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Le Framework GraphAware rend facile la conception, le test et le déploiement de fonctionnalités pour Neo4j, aussi bien génériques que spécifiques à un domaine.

GraphAware Framework

Page 58: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

GraphUnit& RestTest RelCount WarmUp Schema (wip) Recommendation

Engine

GraphAware Framework

ChangeFeed UUID TimeTree Algorithms NodeRank

Page 59: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

Open Source (GPL)ActifProduction ReadyGithub: github.com/graphawareWeb: graphaware.comMaven Central

GraphAware Framework

Page 60: Recommandations avec Neo4j et le GraphAware Recommendation Engine

GraphAwareTM

EssayezDonnez-nous du feedbackContribuezConcevez vos propres modulesContactez-nous pour du support/consultance

GraphAware Framework

Page 61: Recommandations avec Neo4j et le GraphAware Recommendation Engine

Questions ?

Page 62: Recommandations avec Neo4j et le GraphAware Recommendation Engine

Merci !

GraphAwareTM

Christophe Willemsen

Twitter: @ikwattro

Github: @ikwattro