cypher-direction-competition ã§åªåð¥
2023-10-07
9æ17æ¥ã«çµäºãã cypher-direction-competition ãšããè¶
å°èŠæš¡ã³ã³ãã§åªåã㊠â¬1,500 ãç²åŸããã
ãããŠåªåããã³ãŒãã langchain ã«çµã¿èŸŒãŸããã
æ¬æ°ãªã¢ã«ãŽãªãºã ãªã©ã¯äœ¿ã£ãŠããããæè¡çã«äœãåãããšãããããã§ã¯ãªãããæèããããšã¯ããã€ãããã®ã§ãã®èŸºãã«ã€ããŠæžããããšã
ã³ã³ãã®å
容ã«é¢ãã詳现ã¯ãäž»å¬è
ã® Tomaz Bratanic ããã® Medium ã«èšèŒãããŠããã®ã§ãæ°ã«ãªãæ¹ã¯çŽæ¥ãã¡ãã確èªãããšè¯ãã
ã³ã³ãã®èæ¯ãšæŠèŠ
æšä» LLM ãçšãã RAGïŒRetrieval Augmented Generation, æ€çŽ¢æ¡åŒµçæïŒã泚ç®ãããŠããã
retrieve ããå€éšç¥èã®èç©å Žæã®äžã€ãšã㊠Neo4j å
ã«æ§ç¯ããããã¬ããžã°ã©ããæãããããã質åæãå
¥åãšã㊠Neo4j ããçããåŸãã«ã¯ãèªç¶èšèªãã cypher ã¯ãšãªãçæïŒ= text2cypherïŒããå¿
èŠãããã
Neo4j å
ã«å®çŸ©ãã relation type ã node labelïŒ= ã¹ããŒãïŒãæ·»ãã質åæã cypher ã¯ãšãªã«å€æãããããã³ããã ChatGPT ã«æããã°è¯ããšæãã ããããæ¡å€ããäžæãã¯ãããªãã
äœãäžæããããªãããšãããšãæ³å®ãããŠããªããªã¬ãŒã·ã§ã³ã® typeã»åã㧠cypher ã¯ãšãªãçæãããŠããŸãã±ãŒã¹ãå²ãšå€ãçºçããã
ããã§æ¬²ãããªã£ãŠããã®ããçæããã cypher ã¯ãšãªãã¹ããŒãã«é©åããããã« post-processing ã§ä¿®æ£ãããããªã¢ã«ãŽãªãºã ã
cypher-direction-competition ã§ã¯ãã®ã¢ã«ãŽãªãºã ã®éçºããé¡ã ã£ãã
ã³ã³ãã®ã«ãŒã«
詳现ã¯ã³ã³ãã®ãªããžããªã«èšèŒãããŠããã®ã§ãæ¬æçš¿ã§ã¯èŠç¹ã®ã¿èšèŒããã
æé
2023 幎 8 æ 11 æ¥ã 2023 幎 9æ 17æ¥
å 容
以äžã®å
¥åãåºåã䌎ãã¢ã«ãŽãªãºã ã®å®è£
ã
å
¥å: cypher ã¯ãšãªãšã¹ããŒã
åºå: ã¹ããŒãã«é©åããããã«ä¿®æ£ãã cypher ã¯ãšãª
èŠã¯ãcypher ã¯ãšãªå
ã«èšè¿°ãããŠãããã¹ã®äžã«æ³å®ãããŠããªã labelãtype åã³äž¡è
ã®çµã¿åããããªããã確èªããä¿®æ£ã§ããã®ã§ããã°ã¯ãšãªã®å
容ãä¿®æ£ãããšããããšã
äŸãã°ã以äžã®ãããªã¹ããŒããæ³å®ãããŠããã
MATCH (movie:Movie)-[:ACTED_IN]->(person:Person) RETURN movie
ãšããã¯ãšãªãå
¥åããããšããã
ã¹ããŒãã«ããã°ã
:Movie
ã :Person
ã« :ACTED_IN
ããŠããã±ãŒã¹ã¯æ³å®ãããŠãããããã®ã¯ãšãªã¯èª€ãã§ãããšå€æã§ããããããéã« :Person
ã :ACTED_IN
ããŠãã :Movie
ã¯ã¹ããŒãã«ååšããããããã£ãŠãrelation ã®åããéã«ãã以äžã®ã¯ãšãªã«ä¿®æ£ã§ããã°æ£è§£ã«ãªããMATCH (movie:Movie)<-[:ACTED_IN]-(person:Person) RETURN movie
ã«ãŒã«ããŒã¹ã§å®¹æã«å¯Ÿå¿å¯èœãªã¿ã¹ã¯ã§ããã
è©äŸ¡æ¹æ³
以äžã®4é
ç®ã®ç·åè©äŸ¡ã
è©äŸ¡é
ç®
- 粟床
- ã·ã³ãã«ã
- å¯èªæ§
- ããã©ãŒãã³ã¹
åé
ç®ãå
·äœçã«ã©ãè©äŸ¡ããããã©ãã ãéèŠãããšãã£ãæ¹éã¯ç¹ã«èšèŒããªãã£ããïŒKaggle ã®ã³ã³ãã«æ
£ããŠããèªåãšããŠã¯ãå®éææšãååšããªãã®ã¯ã©ããªãã ãšããæ°æã¡ã¯ãã£ããïŒ
å¶éäºé
- ã§ããã° Python ã䜿çšãäžå¿ä»ã®èšèªã§ãå¯ã
- å€éšã©ã€ãã©ãªãããŒã«ã®å©çšçŠæ¢ãèšèªã«æšæºçµã¿èŸŒã¿ãããã©ã€ãã©ãªã®ã¿å©çšå¯èœãå¯äžã®äŸå€ãšã㊠Cypher ã® AST parser ã¯å©çšå¯ã
åãçµã¿æ¹
æåã«ãã©ã®è©äŸ¡é
ç®ã§ä»ã®åå è
ãšå·®ãã€ããããããèããã
ã¿ã¹ã¯ã®å
容ããå
šåå è
ã粟床 100% ã§æåºããããšã¯ç¢ºå®ã ãšæã£ãŠããã®ã§ã粟床ã§å·®ã¯ä»ããªããšèããã
äžæ¹ããã©ãŒãã³ã¹ã«ã€ããŠã¯åœç¶å·®ãä»ããšèããããããæèšã¯ãããŠããªããã®ã®ãããã©ãŒãã³ã¹ã¯äž»å¬è
ããããŸã§éèŠããŠããªã芳ç¹ã§ã¯ãªãããšæšå¯ãããäœæ
ãªããããã©ãŒãã³ã¹ãéèŠããã®ã§ããã°ããããã Python ãæšå¥šããªããšèããããããã飜ããŸã§ãåŠçã«æéãæããéããå Žåã«è©äŸ¡ãäžããããçšåºŠã®æ¶æ¥µçèŠä»¶ã§ãããšä»®å®ããã
åŸã£ãŠãæ®ãã·ã³ãã«ããšå¯èªæ§ã§å·®ãä»ããã¹ãã ãšå€æããã
ã·ã³ãã«ã
å¯èªæ§ã«ãå
±éããããšã ãããªãŒããã«ã³ãŒãã«èšèŒãããŠãããããªç¹ããã æèããã ãã
äŸãã°ã
- äžé¢æ°ã«äžã€ã®ããžãã¹ããžãã¯ãèšè¿°ããã
- éå°ãªã¢ãžã¥ãŒã«åå²ãã¯ã©ã¹èšèšã¯è¡ããªãã ããšãæèããã
1 ç¹ç®ã¯èšèŒã®éããäžã€ããèšãããŠããããšã ãšæãã®ã§ããã§ã¯ã¹ã«ãŒã
2 ç¹ç®ã§å
·äœçã«è¡ã£ãããšã®äžã€ã«ã¯ namedtuple ã®äœ¿çšãæãããããç¥ã£ãŠãã人ããããã°ç¹ã«ç®æ°ãããã®ã§ã¯ãªãããåšãã«äœ¿ã£ãŠãã人ãããŸãããªãã®ã§ç°¡æœã«çŽ¹ä»ãããšãnamedtuple ã¯ãtuple ã®åãã£ãŒã«ãã«ååãä»ããããä»çµã¿ã
äŸãã°ä»¥äžã®ããã«
:Person
ãšãã namedtuple ãå®çŸ©ããã°ãage
ãšãããã£ãŒã«ãåãæå®ããŠå€ãååŸã§ããããã«ãªããfrom collections import namedtuple Person = namedtuple('Person', ['name', 'age']) person = Person('Jack', 24) person.age
24
å¿è«
:Person
ã® class ãå®çŸ©ããŠããperson.age
ã®ãããªåœ¢ã§ãã£ãŒã«ãã«ã¢ã¯ã»ã¹ã§ããããã«ã¯ã§ããããæããã«ã³ãŒãéãå¢ããã
æ§é åããŠæŽçããæ¹ãåãããããæŠå¿µïŒåèšã®äŸã ãš :Person
ïŒã«ã€ããŠããã®æŠå¿µãæã€ã¹ãé¢æ°ïŒäŸãã°ã賌å
¥ãæå³ãã buy
é¢æ°ãªã©ïŒãå®çŸ©ããå¿
èŠããªãããŸããã®æŠå¿µãæã€ãã£ãŒã«ãã®å€ïŒage
ïŒãæžãå€ããããšãæ³å®ãããªãã®ã§ããã°ãnamedtuple ã¯æãã³ãŒãéãæãã€ã€å¯èªæ§ãæ
ä¿ã§ããæ段ã ãšæããå¯èªæ§
ã³ãŒãã®å¯èªæ§ãããããšãªãããã³ãŒããèªãåã«ã¢ã«ãŽãªãºã ã®æŠèŠãç°¡æœã«èšèŒããããšã§ãèªã¿æãã³ãŒããèªã¿å§ããããããããã«å¿æããã
å
·äœçã«ã¯ä»¥äžã® 2 ç¹ãè¡ã£ãã
- ã³ãŒããèªãŸããåã« README ã§ã¢ã«ãŽãªãºã ã®æŠèŠãèšèŒ
- ã¢ã«ãŽãªãºã ã®äœ¿ãæ¹ã瀺ã notebook ã®äœæ èªåèªèº«ãæ®æ®µå©çšãããã©ã€ãã©ãªãªã©ãããã°ãREADME ãèªãã äžã§å©çšãå§ããç¿æ £ãããã®ã§ãç¹ã« README ã«ã¯äžå¯§ãªèª¬æãèšèŒãããïŒæåã¯ã¢ã«ãŽãªãºã ã®å³è§£ãªã©ãçšæãããããšæã£ããããããªã«é£è§£ãªä»çµã¿ã§ããªããããã£ãŠäœèšããªããšæããçµå±å³è§£ã¯ããããïŒ
ãŸããå®éã«ã©ã®ããã«ã¢ã«ãŽãªãºã ãå©çšã§ããããã©ã®çšåºŠã®ç²ŸåºŠããã©ã®çšåºŠã®åŠçæéãæããã®ãã«ã€ããŠããå
·äœäŸãããã°åãããããã ãããšæãããã¢çšã® notebook ãäœæããã
æ£çŽãã®ãããã¯åå è
ã®ã»ãšãã©ãããã ãããšæã£ãŠããããæ¡å€ããã§ããªãã£ãã確èªããéãã§ã¯äžçªããã¥ã¡ã³ãé¡ãå
å®ãããŠãæ°ãããã®ã§ãããããããããã§å·®ãã€ããã®ããïŒ
å®è£ ããã¢ã«ãŽãªãºã
åé ã«èšèŒããéããã¢ã«ãŽãªãºã ã®å
容èªäœã«ç¹çãã¹ãç¹ã¯ãªãããææç©ã®äžå¿ãšãªããã®ãªã®ã§äžå¿è§Šããã
ã¢ã«ãŽãªãºã ã®åŠçã®æµãã¯æŠã以äžã® 4 ã¹ãããã
- 宣èšãããå€æ°ãš node label, relation type ã®å¯Ÿå¿ãæ€åº
- ãã¹ã®æ€åº
- ãã¹ãã node - relation - node ãšãã圢ã®éšåãã¹ãæ€åº
- æ€åºããéšåãã¹ããšã«ã¹ããŒããšã®é©åæ§ãè©äŸ¡ã»ä¿®æ£
以äžã§ã¯åæ²ã®ã¹ããŒãäŸãšä»¥äžã®ãµã³ãã«ã¯ãšãªãçšããŠå€§ãŸããªã¢ã«ãŽãªãºã ã®æåãèšèŒããã
MATCH (p1:Person)-[:DIRECTED]->(m:Movie)<-[:WROTE]-(p1) MATCH (p2:Person)<-[:ACTED_IN]-(m) RETURN p2
ã¡ãªã¿ã«ãã®ã¯ãšãªã¯ããã人ãèæ¬ãšãã£ã¬ã¯ã¿ãŒãåæã«åããæ ç»ãåºæŒãã人ç©ã¯ïŒããšãã質åã«å¯ŸããçããåŸããæã®ã¯ãšãªã
ïŒæ®éã«èããŠãããããããïŒæ ç»ã人ç©ã«åºæŒãããšããã®ã¯ã¹ããŒãã«ç
§ãããŠèª€ãã
ã¢ã«ãŽãªãºã ã¯ãããèªåã§ä¿®æ£ããã
ãªããæ€åºãšãæœåºãšãè¡šçŸããŠããéšåã¯ãã¹ãŠæ£èŠè¡šçŸèžã§å®çŸãããã䜿çšãããã¿ãŒã³ã«ã€ããŠè§£èª¬ããã®ã¯ã€ãŸããªãã®ã§å²æããã以äžã§ã¯ã飜ããŸã§ãã¢ã«ãŽãªãºã ã®æµãã«ã€ããŠã®ã¿èšèŒããã
1. å€æ°ãšå¯Ÿå¿ãã label, type
ã¯ãšãªå
ã§å®£èšãããŠããå€æ°ãæ€åºãããã®å€æ°ã«å¯ŸããŠæå®ãããŠãã label, type ãäœã§ããããèŸæžåããã
key | value |
---|---|
p1 | [Person] |
m | [Movie] |
p2 | [Person] |
2. ãã¹ã®æ€åº
ã¯ãšãªå
ã«èšè¿°ãããŠãããã¹ããã¹ãŠæ€åºããã
äŸã§ã¯ä»¥äžã® 2 ã€ã®ãã¹ãæ€åºãããã
(p1:Person)-[:DIRECTED]->(m: Movie)<-[:WROTE]-(p1)
(p2:Person)<-[:ACTED_IN]-(m)
3. éšåãã¹ã®æ€åº
æ€åºãããã¹ã node-relation-node ã®éšåãã¹ã«ããã«å解ããããã®æã1 ããŒããã€ããããŠéšåãã¹ãçæããŠãããåŸã£ãŠãn åç®ã®éšåãã¹ã®å³ç«¯ã®ããŒããš n+1 åç®ã®å·Šç«¯ã®ããŒãã¯åäžã«ãªã
(p1:Person)-[:DIRECTED]->(m: Movie)<-[:WROTE]-(p1)
ãã 2 ã€ã®éšåãã¹ãæœåº(p1:Person)-[:DIRECTED]->(m: Movie)
(m:Movie)<-[:WROTE]-(p1)
(p2:Person)<-[:ACTED_IN]-(m)
ãã 1 ã€ã®éšåãã¹ãæœåº(p2:Person)<-[:ACTED_IN]-(m)
4. ã¹ããŒããšã®æ¯èŒ
æœåºããéšåãã¹ããšã«ã¹ããŒããžã®é©åæ§ãè©äŸ¡ããã
éšåãã¹å
ã§å€æ°ã䜿çšãããŠããå Žåã¯ã1. ãã§æ§ç¯ããèŸæžãã label, type ãç¹å®ããŠè©äŸ¡ããã
(p1:Person)-[:DIRECTED]->(m:Movie)
â:Person
ã:Movie
ã:DIRECTED
ããã¹ããŒãã¯ååšãããããã¯æ£ããéšåãã¹ã(m:Movie)<-[:WROTE]-(p1)
â èŸæžã«ããã°ãp1
ã¯:Person
labelã â:Person
ãMovie
ã:WROTE
ããã¹ããŒãã¯ååšãããããã¯æ£ããéšåãã¹ã