Llama 3.2とNeo4jでローカルGraphRAG環境を構築
を参考にしてみる
ソースが
https://github.com/satken2/local-llama-graphrag
にあるので
Git clone してみる
この中の
graph_generation.py
で作成しているので
コードを調べることにするのと
graph_retrieval.py
でリレーションをしているので
おそらく分割が必要と思われる
有料プランでならGPTとか使えばできるけど
毎回トークンを使用するのは無理
ってそもそもllama3.2なので課金は存在しない
むしろ試すならcolabで行けるはず
まずはgit clone を Mac でやってモデルをelzaにして動かしてみる
解説は
https://qiita.com/satken2/items/ea3ddc5f273d43cd63d4?utm_source=chatgpt.com
もみながら調べていく
Graph RAGではナレッジグラフを活用することで、データ間の関係性や全体の文脈を考慮したコンテキストの取得をすることを試みる技術
これまでのRAGではベクトルデータベースの中からプロンプトに合致しそうなデータを局所的に取得してきて、
それをコンテキストとして言語モデルに与えていました。
ですがこの方法は元となるデータ全体の文脈をあまり考慮できておらず、
結果としてあまり一致度の高くないデータを取得してしまって回答精度の低下に繋がる場合がありました。
* CPU: Core i5 12400F
* メモリ: DDR4 32GB
* GPU : RTX 3070 (8GB)
* OS: Windows 11
* Python: 3.11.0
が参考スペック
とりあえず M1MacbookAir 16GBで試してみる
グラフデータベースとしてNeo4jをDockerで起動
メインとなる言語モデルはオープンの言語モデルであるLlama3.2の3BモデルをOllamaで動作させ、
LangChainでグラフデータとの連携とかプロンプトの管理をしています。
* Llama 3.2 (3B)
* Ollama
* Neo4j
* langchain
とりあえず実験終了したらOllamaを日本語対応可能な
elyza:jp8b
にしてみる
Neo4jのCypherクエリ言語を拡張するライブラリapoc-coreを使用は
既にセットアップしたのでそのまま使う
docker compose up -d
で起動
次に
Wikipediaのハリーポッター(人物)の記事をRAGで検索したい
これはリポジトリに含まれているので
harrypotter.txt
の内容をそのまま使う
ちなみに内容は
Harry James Potter is a fictional character in the Harry Potter series of novels by J. K. Rowling. The plot of the seven-book series chronicles seven years in the life of the orphan Harry, who, on his eleventh birthday, learns he is a wizard. He attends Hogwarts, a school of magic, where he receives guidance from the headmaster Albus Dumbledore and becomes friends with Ron Weasley and Hermione Granger. Harry learns that during his infancy, the Dark wizard Lord Voldemort murdered his parents but was unable to kill him as well. The plot of the series revolves around Harry's struggle to adapt to the wizarding world and defeat Voldemort.
Harry is regarded as a fictional icon and has been described by many critics, readers, and audiences as one of the greatest literary and film characters of all time. He is portrayed by Daniel Radcliffe in all eight Harry Potter films. Harry also appears in the play Harry Potter and the Cursed Child, which has been produced on stage since 2016.
According to Rowling, the idea for Harry Potter and the novel series came to her while she was waiting for a train in 1990. She first conceived of Harry as a "scrawny, black-haired, bespectacled boy" who is unaware that he is a wizard.[2] While developing ideas for the first book, she decided to make Harry an orphan who attends a boarding school called Hogwarts. She explained in a 1999 interview with The Guardian: "Harry had to be an orphan—so that he's a free agent, with no fear of letting down his parents, disappointing them ... Then there's the security. Having a child of my own reinforces my belief that children above all want security, and that's what Hogwarts offers Harry."[3]
The death of Rowling's mother in December 1990 inspired her to write Harry as a boy longing for his dead parents. She explained that his anguish became "much deeper" and "much more real" than in earlier drafts because she related to it herself.[2] In a 2000 interview, Rowling said that the character Wart in T. H. White's novel The Once and Future King is Harry's "spiritual ancestor".[4] Although Rowling gave Harry her own birth date of 31 July, she maintained that he is not directly based on any real-life person. She explained that he came "out of a part of me".[5]
Rowling has claimed that Harry is a suitable real-life role model for children. She said, "The advantage of a fictional hero or heroine is that you can know them better than you can know a living hero, many of whom you would never meet ... if people like Harry and identify with him, I am pleased, because I think he is very likeable."[6]
Novels
Harry Potter and the Philosopher's Stone
Harry is first introduced in Harry Potter and the Philosopher's Stone (1997) as an orphan living with his abusive aunt and uncle, Vernon and Petunia Dursley, and their bullying son, Dudley. On his eleventh birthday, Harry discovers he is a wizard when Rubeus Hagrid brings him an acceptance letter from Hogwarts School of Witchcraft and Wizardry. Harry learns that his parents, James and Lily Potter, were murdered by a powerful Dark wizard, Lord Voldemort, who also tried to kill Harry. However, Voldemort's killing curse bounced back and apparently destroyed him, leaving Harry with a lightning-bolt-shaped scar on his forehead. Due to his miraculous survival, Harry became famous among wizards.
Hagrid takes Harry to Diagon Alley to shop for school supplies. Harry buys a wand, and the proprietor tells him that his wand and Voldemort's wand contain feathers from the same phoenix. At the end of the summer, Harry boards the Hogwarts Express, where he befriends Ron Weasley and meets Hermione Granger. When he arrives at Hogwarts, he is assigned to Gryffindor House by the Sorting Hat. During the school year, he receives guidance from the Transfiguration professor Minerva McGonagall and the headmaster Albus Dumbledore. He develops animosity towards a fellow first-year named Draco Malfoy, and becomes increasingly wary of the Potions professor, Severus Snape. Harry and Ron become friends with Hermione after they save her from a troll, and Harry becomes the youngest Seeker on the Gryffindor Quidditch team in a century.
During Christmas holidays, Harry receives an anonymous gift – his father's invisibility cloak. Aided by the cloak, he is able to explore the school freely. He, Ron and Hermione decide to enter a trapdoor they discovered one night, which is guarded by an enormous three-headed dog. They believe a valuable magical object, the Philosopher's Stone, lies beyond the door. After they navigate a series of obstacles, Harry faces Professor Quirrell, whose body has been possessed by Voldemort. When Harry obtains the Stone, Quirrell tries to kill him. However, Quirrell's flesh burns upon contact with Harry's skin, and Harry passes out.
Harry awakens in the Hogwarts infirmary, where Dumbledore explains that Harry was protected by his mother's love, which also killed Quirrell. Voldemort survived, and the Philosopher's Stone was destroyed. During the school's end-of-year feast, Gryffindor is awarded the House Cup thanks to the many points Harry and his friends obtained from their adventure.
Harry Potter and the Chamber of Secrets
In Harry Potter and the Chamber of Secrets (1998), Muggle-born students are being Petrified in the Hogwarts castle. Some students suspect that Harry is behind the attacks, which alienates him from his peers. Harry learns that he shares Voldemort's ability to communicate with snakes, and he begins to question whether he is worthy of Gryffindor House. After Ron's sister Ginny disappears, Harry enters the Chamber of Secrets to rescue her. He battles Tom Riddle, a version of Voldemort that lives in a diary which had possessed Ginny. In addition to Riddle, Harry encounters the basilisk which has been attacking students. To defeat it, Harry summons the Sword of Gryffindor from the Sorting Hat. After Harry triumphs over the basilisk and saves Ginny, Dumbledore reassures him that he is worthy of Gryffindor.
Harry Potter and the Prisoner of Azkaban
Near the beginning of Harry Potter and the Prisoner of Azkaban (1999), Harry becomes angry after his Aunt Marge disparages his parents. He accidentally inflates her with magic, then runs away from home. As he re-enters the wizarding community, he learns about Sirius Black, a notorious killer who escaped from the wizard prison Azkaban. On the train to Hogwarts, Harry is assaulted by Dementors, which cause him to relive his worst memories. During a Care of Magical Creatures lesson with Hagrid, Draco is injured after provoking a hippogriff named Buckbeak. As a result, the Ministry of Magic sentences Buckbeak to death. After Harry has more encounters with Dementors, he asks the new Defence Against the Dark Arts professor, Remus Lupin, to help him ward them off. Lupin teaches Harry the Patronus charm, which Harry struggles to master.
While in the village of Hogsmeade, Harry learns that his parents were betrayed by Sirius, their friend and Harry's godfather, and that Sirius also killed Peter Pettigrew. After Ron is taken to the Shrieking Shack by a wild dog, Harry and Hermione give chase and realize the dog is Sirius in Animagus form. Lupin arrives, and he and Sirius explain that Sirius did not kill Pettigrew. They also reveal that Ron's pet rat, Scabbers, is Pettigrew. Lupin then transforms into a werewolf and injures Sirius, and Pettigrew escapes amid the chaos. A swarm of Dementors appear, but they are driven off by a Patronus cast by a mysterious figure.
Awakening in the school infirmary, Harry and Hermione learn that Sirius is being held captive in Hogwarts and is awaiting the Dementor's Kiss. At Dumbledore's advice, they use a Time-Turner to travel back in time to the moment before Buckbeak's execution. After they save the hippogriff, Harry notices the past versions of himself, Hermione, and Sirius being attacked by Dementors. He conjures a strong Patronus to fight them off, then realizes that he was the mysterious figure who appeared earlier. Harry and Hermione free Sirius, who flies away on Buckbeak, still a wanted fugitive. Harry and Hermione then return to their original timeline.
Harry Potter and the Goblet of Fire
In Harry Potter and the Goblet of Fire (2000), Harry is mysteriously chosen by the Goblet of Fire to compete in the dangerous Triwizard Tournament, even though another Hogwarts champion, Cedric Diggory, has already been selected. During the Tournament's final challenge, Harry and Cedric are transported to a graveyard, where Cedric is killed by Peter Pettigrew. Voldemort, aided by Pettigrew, uses Harry's blood to resurrect his body. Harry then duels Voldemort, and their wands connect. Spirit echoes of Voldemort's victims, including Harry's parents, emerge from his wand. The spirits protect Harry as he escapes to Hogwarts with Cedric's body.
Rowling said it was necessary to depict violence and death in this novel because it allows Harry's bravery and compassion to show. She said that Harry's effort to prevent Cedric's body from falling into Voldemort's hands was based on the scene in Homer's Iliad where Achilles retrieves the corpse of his friend Patroclus from his enemy Hector.[7] She asserted that Goblet of Fire marks a turning point in Harry's life. She said Harry has been "very protected until now" but in this novel he is no longer protected.[8]
Harry Potter and the Order of the Phoenix
In Harry Potter and the Order of the Phoenix (2003), the Ministry of Magic wages a smear campaign against Harry and Dumbledore, disputing their claims that Voldemort has returned. The Ministry appoints Dolores Umbridge as the new Defence Against the Dark Arts teacher. Because the Ministry suspects that Dumbledore is building a student army, Umbridge refuses to teach students practical defensive magic. Harry, Hermione and Ron subsequently form a secret group called Dumbledore's Army to teach students defensive spells. The group is disbanded, however, when one student informs Umbridge about it. To shield his students from punishment, Dumbledore falsely claims that Dumbledore's Army was his idea. As a result, he is ousted from his position as headmaster and replaced with Umbridge.
During this time, Harry's increasingly angry and erratic behaviour nearly estranges him from Ron and Hermione. He suffers another emotional blow when his godfather Sirius is killed by the Death Eater Bellatrix Lestrange during a battle at the Department of Mysteries. Also present is Voldemort, who tries to possess Harry so Dumbledore will kill him. This ruse fails, and Harry and Dumbledore thwart Voldemort's plan to steal an important prophecy. Later, Dumbledore explains to Harry that Voldemort chose Harry as his equal, and that either he or Voldemort must eventually kill the other. A subplot of the novel involves Harry's romance with Cho Chang, which quickly unravels.[9]
Rowling said she put Harry through extreme emotional stress in Order of the Phoenix to show his emotional vulnerability and humanity, which contrast with Voldemort's inhumanity.[10]
Harry Potter and the Half-Blood Prince
In Harry Potter and the Half-Blood Prince (2005), Dumbledore brings Harry along as he attempts to persuade Horace Slughorn to rejoin the Hogwarts faculty as the Potions professor. Harry excels in Slughorn's class after he begins using an old textbook that once belonged to someone known as "The Half-Blood Prince". Outside of class, Dumbledore uses the Pensieve to teach Harry about Voldemort's life and his rise to power. Harry and Dumbledore learn that Voldemort created six Horcruxes to gain immortality, and that they must be destroyed before Voldemort can be killed. Harry repeatedly warns Dumbledore that Draco Malfoy is an agent of Voldemort, but Dumbledore refuses to take action against Draco. He tells Harry that he already knows more about what is happening than Harry does. During the course of the novel, Harry becomes romantically involved with Ginny.
Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he is attacked by Inferi. They try to drown Harry, but Dumbledore rescues him. Dumbledore and Harry then return to the Hogwarts Astronomy Tower with the Horcrux. Dumbledore asks Harry not to interfere in the events that are about to take place, and puts him in a body-binding curse. Immobilized, Harry is unable to intervene as Snape arrives and kills Dumbledore. Later, Harry ends his relationship with Ginny to protect her from Voldemort.
Rowling said that Harry's tumultuous puberty in the novel is based on her own difficult teenage years and those of her sister.[11][12] She said that after Half-Blood Prince Harry becomes "battle-hardened" and is ready to fight and take revenge against Voldemort and Snape.[13]
Harry Potter and the Deathly Hallows
Harry, Ron, and Hermione leave Hogwarts in Harry Potter and the Deathly Hallows (2007) to search for and demolish Voldemort's four remaining Horcruxes. They must be destroyed with basilisk venom, the Sword of Gryffindor, or some other powerful means. Ron, Hermione, Vincent Crabbe and Neville Longbottom each destroy one of these Horcruxes. After a battle at Hogwarts, Harry goes into the Forbidden Forest to meet Voldemort, who uses the Elder Wand to cast the Killing Curse on Harry. The curse stuns Harry into a deathlike state, and he finds himself in an ethereal realm with Dumbledore. The deceased headmaster tells Harry that he is a Horcrux that Voldemort unknowingly created. There was a part of Voldemort's soul inside Harry, but it was destroyed by the curse Voldemort cast moments ago. Harry subsequently returns to the living world and duels with Voldemort. The Dark Lord uses the Killing Curse again, but it rebounds and kills him. Harry, not Voldemort, was the true master of the Elder Wand, and the wand did not wish to harm its master. Harry decides to leave the Elder Wand in Dumbledore's tomb and the Resurrection Stone hidden in the forest, but he keeps the Invisibility Cloak.
In the epilogue, set nineteen years after Voldemort's death, Harry and Ginny are a married couple and have three children: James Sirius Potter, Albus Severus Potter and Lily Luna Potter. Albus is departing for his first year at Hogwarts, and is worried that he will be sorted into Slytherin. Harry explains to him that not all Slytherins are bad.
Harry Potter and the Cursed Child
Harry Potter and the Cursed Child is a play written by Jack Thorne from a story by Thorne, J. K. Rowling and John Tiffany. It takes place nineteen years after the events of Deathly Hallows. In the play, Harry is Head of the Department of Magical Law Enforcement at the Ministry of Magic. The story focuses on Harry's son, Albus Severus Potter.
Film adaptations
Harry is portrayed by Daniel Radcliffe in all eight Harry Potter film adaptations. Radcliffe was asked to audition for the role in 2000 by David Heyman, who was producing the first film in the series. Radcliffe met Heyman while attending a London play called Stones in His Pockets.[14][15]
In a 2007 interview with MTV, Radcliffe described the development of his character during the course of the series: "That's what the films are about for me: a loss of innocence, going from being a young kid in awe of the world around him, to someone who is more battle-hardened by the end of it."[16] Radcliffe said that after the death of Cedric Diggory in Goblet of Fire, Harry experiences survivor's guilt and feels immense loneliness. Because of this, Radcliffe talked to a bereavement counsellor to help him prepare for the role.[16] Radcliffe stated that the most repeated question he has been asked is how Harry Potter has influenced his own life, to which he regularly answers it has been "fine", and that he did not feel pigeonholed by the role, but rather sees it as a huge privilege to portray Harry.[17]
In 2011, Empire magazine ranked Radcliffe's Harry Potter as the 36th greatest film character of all time. The magazine published another character ranking in 2018, this time placing Harry at 67th.[18]
Outward appearance
A stylized image of Harry and the two most instantly recognizable features of his appearance: his lightning bolt scar and his circular glasses.
In the novels, Harry is described as having his father's perpetually untidy black hair, his mother's bright green eyes, and a lightning bolt-shaped scar on his forehead. He is short and skinny for his age, with a thin face and "knobbly" knees, and he wears Windsor glasses. When asked about the meaning behind Harry's scar, Rowling said she wanted him to be "physically marked by what he has been through". She said the scar is "an outward expression of what he has been through inside".[19] Rowling also said that Harry's glasses are the clue to his vulnerability.[20] Harry eventually grows taller, and by the final book is almost the height of his father, and is described as tall by other characters.[21]
Personality
According to Rowling, Harry is strongly guided by his own conscience, and has a keen feeling of what is right and wrong. Having "very limited access to truly caring adults", Rowling said, Harry "is forced, for such a young person, to make his own choices".[22] He "does make mistakes", she conceded, but in the end, he does what his conscience tells him to do. According to Rowling, one of Harry's pivotal scenes came in the fourth book when he protects his dead schoolmate Cedric Diggory's body from Voldemort, because it shows he is brave and selfless.[7]
Rowling has stated that Harry's character flaws include anger and impulsiveness; however, Harry is also innately honourable.[23][24] "He's not a cruel boy. He's competitive, and he's a fighter. He doesn't just lie down and take abuse. But he does have native integrity, which makes him a hero to me. He's a normal boy but with those qualities most of us really admire."[25] For the most part, Harry shows humility and modesty, often downplaying his achievements; though he uses a litany of his adventures as examples of his maturity early in the fifth book. However, these very same accomplishments are later employed to explain why he should lead Dumbledore's Army, at which point he asserts them as having just been luck, and denies that they make him worthy of authority. After the seventh book, Rowling commented that Harry has the ultimate character strength, which not even Voldemort possesses: the acceptance of the inevitability of death.
Magical abilities and skills
Throughout the series, Harry is described as a gifted wizard. He has a particular talent for flying, which manifests itself in Harry Potter and the Philosopher's Stone the first time he tries it, and gets him a place on a Quidditch team one year before the normal minimum joining age. He captains it in his sixth year. In his fourth year, Harry is able to confront a dragon on his broomstick.
Harry is also gifted in Defence Against the Dark Arts, in which he becomes proficient due to his repeated encounters with Voldemort and various monsters. In his third year, Harry becomes able to cast the very advanced Patronus Charm, and by his fifth year he has become so talented at the subject that he is able to teach his fellow students in Dumbledore's Army, some even older than him how to defend themselves against Dark Magic. At the end of that year, he achieves an 'Outstanding' Defence Against the Dark Arts O.W.L., something that not even Hermione achieved.
Harry also has the unusual ability to speak and understand "Parseltongue", a language associated with Dark Magic. This, it transpires, is because he harbours a piece of Voldemort's soul. He loses this ability after the part of Voldemort's soul inside him is destroyed at the end of The Deathly Hallows. However, in the events of Harry Potter and the Cursed Child, it was revealed that he had not lost the ability to recognise or speak the language when he encountered Delphini, Voldemort's daughter, who was trying to use his son and Draco Malfoy's son Scorpius to fulfill a prophecy that could guarantee the return of Voldemort by changing time.
Possessions
Harry's parents left him a large pile of gold in the wizarding bank, Gringotts. After Sirius' death later in the series, all of his remaining possessions are also passed along to Harry, including Number Twelve, Grimmauld Place, and Sirius's vast amount of gold were transferred into Harry's account at Gringotts.
Among the school items Harry purchases in Diagon Alley after discovering his gold inheritance is his first wand, an 11-inch-long holly and phoenix feather model that he learns is the twin of Voldemort's wand, as the feathers that both wands contain as their cores both comes from Fawkes, the phoenix that Dumbledore keeps as a pet in his office until his death in Half-Blood Prince.[26] Harry's wand is broken in Deathly Hallows. For a time, he borrows Hermione's wand, and later steals Draco's. With his defeat of Voldemort at the end of the series, he comes into the possession of the Elder Wand, but uses it only to repair his holly wand, before returning it to Dumbledore's tomb, from which Voldemort had stolen it. In the film version of Deathly Hallows Part 2, Harry destroys the Elder Wand.
Harry also inherits indirectly two of his father's prized possessions. One is the Marauder's Map, given to him by interim owners Fred and George Weasley, which endows Harry with comprehensive knowledge of Hogwarts' facilities, grounds, and occupants. The other is his father's invisibility cloak, given to him by Dumbledore, which eventually proves Harry's descent from the Peverell family. Harry uses these tools both to aid in excursions at school and to protect those he cares about; the Invisibility Cloak, in particular, can hide two full-grown people. If three fully-grown people hide under the cloak their feet will be visible.
Throughout the majority of the books, Harry also has a pet owl named Hedwig, used to deliver and receive messages and packages. Hedwig is killed in the seventh book, about which Rowling says: "The loss of Hedwig represented a loss of innocence and security. She has been almost like a cuddly toy to Harry at times. I know that death upset a lot of people!"[23] As a Quidditch player, Harry has owned two high-quality brooms. The first, a Nimbus Two Thousand, was procured for him by Professor Minerva McGonagall when Harry was added to Gryffindor's Quidditch team despite being a first-year student. This broom was destroyed by the Whomping Willow during a match in Harry's third year. It was replaced by a Firebolt, an even faster broom, purchased for Harry by Sirius; however, as Sirius was believed to be trying to murder Harry at the time, the broom was subjected to stringent security inspections before Harry was allowed to ride it. Harry used it throughout his Hogwarts career until it, along with Hedwig, was lost during the July escape from Privet Drive in the final book.
Harry also owns a mokeskin pouch, or small 'bag' that is used for storing items, which no one but the owner can get out. Harry uses the pouch throughout the course of Deathly Hallows to keep several objects such as the Marauder's Map, a shard of the magical mirror given to him by his god-father Sirius, the fake Horcrux locket that had belonged to Sirius's brother R.A.B. (Regulus Arcturus Black), the Snitch bequeathed to him by Dumbledore, containing the Resurrection Stone that had previously been set into Voldemort's grandfather Marvolo Gaunt's signet ring, which Harry discovers is actually the second Hallow, a letter from his mother to Sirius with part of a photo (of him and his father, James), and eventually, his own broken wand (which Harry later repairs with the Elder Wand).
Family
In the novels and films, Harry is the only child of James and Lily Potter, orphaned as an infant. Rowling made him an orphan because she felt an orphan would be interesting to write about.[3] However, after her mother's death, Rowling wrote Harry as a child longing to see his dead parents again, incorporating her own anguish into him. Harry is categorised as a "half-blood" wizard in the series, because although both his parents were magical, Lily was "Muggle-born", and James was a pure-blood.
Harry's aunt and uncle kept the truth about his parents' deaths from Harry, telling him that they had died in a car crash.[2] James Potter is a descendant of Ignotus Peverell, the third of the three original owners of the Deathly Hallows, and thus so is Harry, a realisation he makes during the course of the final book. The lineage continues at the end of the saga through his three children with Ginny: James Sirius Potter, Albus Severus Potter and Lily Luna Potter.
Reception
In 2002, Book magazine ranked Harry Potter No. 85 among the "100 Best Fictional Characters since 1900".[27] Entertainment Weekly ranked Harry Potter number two on its 2010 "100 Greatest Characters of the Last 20 Years" list, saying "Long after we've turned the last page and watched the last end credit, Harry still feels like someone we know. And that's the most magical thing about him."[28] UGO Networks listed Harry as one of their best heroes of all time, who said that "Harry is a hero to the often oppressed and downtrodden young fan boys and girls out there, who finally have an icon that is respected and revered by those who might otherwise look down on robe-wearing and wand waving as dork fodder".[29] Harry Potter was also ranked number thirty-six on Empire's 2008 list of "100 Greatest Movie Characters of All Time".[30] IGN said that Harry Potter was their favourite Harry Potter character, calling him a "sympathetic figure" and saying in response to his fights against Voldemort that "everybody loves an underdog story of good vs. evil".[31]
On the other hand, he has received criticism. In 2010, he was voted the 35th "Worst Briton" in Channel 4's "100 Worst Britons We Love to Hate" programme.[32] In The Irish Times, Ed Power called Harry an "anointed cherub" who is "told he is special from the very outset" before he has any "winning attributes". Power said Harry is "celebrated as an overachiever before he's achieved anything".[33] Lannah Marshall called Harry "a bland character". She further described him as "an audience surrogate, or what I call a 'puppet protagonist'. A puppet protagonist is a main character with dull, limited personality, enabling the audience to step inside the role and use their imagination to fill in the rest.[34]
Despite blowback from some Christian fundamentalists critical of Rowling's usage of witchcraft and magic in the series,[35] other Christian critics, including Rev. John Killinger, have argued that Potter is a Christ figure in the series. Killinger opined in 2002 that "J.K. Rowling has written the Christ story of the 21st century, and it's wonderful that she has attained such a magnificent following worldwide." He noted several allusions to Jesus in Potter's character arcs in Philosopher's Stone and Chamber of Secrets.[36] Rowling herself later admitted that the Gospel story inspired that of Harry Potter, especially with his ultimate sacrifice in Deathly Hallows and apparent death before returning to defeat Voldemort once and for all.[37]
既にOllamaのセットもしてあるので
これも飛ばす
次にRAGの構築のため
グラフデータベースを構築
Neo4jの初期化とLlama3.2の読み込み
No4jとOllamaはどちらもlangchainを使う
from langchain_ollama import ChatOllama
from langchain_community.graphs import Neo4jGraph
# Initialize LLM model
llm = ChatOllama(model="llama3.2:1b", temperature=0, format="json")
# Initialize the Neo4j graph
graph = Neo4jGraph()
このモデルの部分を
llama3.2:1b
から別のモデルにすれば行けるはず
elyza:jp8b
にしてみる
RAGの構築用プログラムgraph_generator.pyを作成していく
テキストデータの読み込みと分割
最初に準備したWikipediaの記事 harrypotter.txtを読み込んで、
RecursiveCharacterTextSplitterを使ってチャンクに分割する
しかし
このテキスト分割は文脈などを考慮せず機械的に切っているだけなので、重要な情報が途中でぶった切られる
対策として
重要な情報が途中でぶった切られるのを防ぐため、
chunk_overlapを設定して
チャンクと次のチャンクの範囲をある程度重複させて分割
# Load documents from a text file
loader = TextLoader(file_path="harrypotter.txt", encoding = 'UTF-8')
docs = loader.load()
# Split documents into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=24)
documents = text_splitter.split_documents(documents=docs)
このチャンクの範囲の設定については適切な量はGPTで調べてみる
## 1. **チャンクサイズ (`chunk_size`) の決定**
– **短すぎると**:単語や文章が分断され、情報の流れが失われる。
– **長すぎると**:分割する意味が薄れ、メモリ消費が大きくなる。
– **一般的な目安**:
– **250~500文字**:短め(検索用、要約用)
– **500~1000文字**:バランス型(QAシステム、ベクトル検索)
– **1000~2000文字**:長め(LLMのコンテキスト処理)
例:
– 段落単位で検索するなら **500~1000文字**
– 文脈を広く持ちたいなら **1000~2000文字**
## 2. **チャンクの重複 (`chunk_overlap`) の決定**
`chunk_overlap` を設定することで、情報の途切れを防ぐことができます。適切な値は以下のように決めます。
– **目安**:
– `chunk_size` の **10~20%** くらいが一般的
– 例えば `chunk_size=500` なら `chunk_overlap=50~100`
– 重要な文脈を保持したいなら `chunk_overlap=20~30%`
### **例**
– `chunk_size=250`, `chunk_overlap=24` (10%程度) → 短めのコンテンツ
– `chunk_size=500`, `chunk_overlap=100` (20%程度) → 一般的な設定
– `chunk_size=1000`, `chunk_overlap=200` (20%程度) → 長めのコンテンツ
– 一般的には **`chunk_size=500`, `chunk_overlap=100` (20%)** くらいが良い
– **内容が切れやすいなら `chunk_overlap` を増やす**
– **より自然な分割には `separators` を活用する**
– **実際に試して調整するのが最も確実**
とのこと
この辺りは別の時に試すことにする
チャンクからグラフデータを作成
LLMGraphTransformerを使ってチャンクに分割したテキストをグラフドキュメントに変換する
これは
LLMがテキストからエンティティやその間の関係性を抽出し、構造化されたグラフデータを生成
その後、それをNeo4jに保存
という流れになる
# Convert documents to graph format
llm_transformer = LLMGraphTransformer(llm=llm, ignore_tool_usage=False)
graph_documents = llm_transformer.convert_to_graph_documents(documents)
# Add documents to the graph
graph.add_graph_documents(
graph_documents,
baseEntityLabel=True,
include_source=True
)
この辺りのコードは変更なさそう
次にベクトルインデックスの作成
グラフデータだけでなく
テキストから作成した埋め込み行列(ベクトルインデックス)も計算し、
これもNeo4jに格納
ベクトルインデックスの計算にはmxbai-embed-largeという別のモデルを使用
Neo4jVector.from_existing_graphを使用することで、
embeddingsに指定したモデルを使用してテキストの埋め込み行列が作成され、
グラフデータベースに保存される
このコードも変わらないと思う
# Initialize embeddings
embeddings = OllamaEmbeddings(
model="mxbai-embed-large",
)
# Create a vector index from the existing graph
Neo4jVector.from_existing_graph(
embeddings,
search_type="hybrid",
node_label="Document",
text_node_properties=["text"],
embedding_node_property="embedding"
)
次に
フルテキストインデックスの作成
最後に通常の全文検索に使用するインデックス
(ベクトルインデックスとは異なります)を作成
これは単にNeo4jに接続してCREATE FULLTEXT INDEXを使えばOK
これは
# Connect to the Neo4j database and create a full-text index
driver = GraphDatabase.driver(
uri=os.environ["NEO4J_URI"],
auth=(os.environ["NEO4J_USERNAME"], os.environ["NEO4J_PASSWORD"])
)
def create_fulltext_index(tx):
query = '''
CREATE FULLTEXT INDEX `fulltext_entity_id`
FOR (n:__Entity__)
ON EACH [n.id];
'''
tx.run(query)
try:
with driver.session() as session:
session.execute_write(create_fulltext_index)
print("Fulltext index created successfully.")
except Exception as e:
print(f"Failed to create index: {e}")
driver.close()
でコードもそのままで行けるはず
上記のコードが実行できたら、http://localhost:7474にアクセスしてグラフの状況を確認
次に
RAGを使った質問の回答
このグラフRAGでは質問に回答するときに以下の3段階でデータを取ってます。
1. 質問文に含まれる人名や組織名を抽出し、それに関連するグラフノードを取得する
2. 取得したグラフノードでベクトル検索を行い、コンテキストを作る
3. 元の質問文に作成したコンテキストを添えて言語モデルに投げ、最終的な回答を得る
“What does Harry Potter looks like?”という質問をした場合
1.質問に関連するグラフデータの抽出
質問文から”Harry Potter”という人名が抽出され、”Harry Potter”に言及するグラフノードが取得されます
2.ベクトル検索による関連情報の抽出
さらに、これはハリーの見た目に関する質問だということを言語モデルが解釈して、1で取得したグラフノードに含まれる文章に対してベクトル検索を行い、見た目に関する言及を抽出してコンテキストとします
3.最終的な回答の生成
最後に、context_builderが作ったコンテキストと元の質問である”What does Harry Potter looks like?”をつなげてLlamaに投げています
ここからが検索用プログラム
質問文からの人名抽出
まず、ユーザーの質問文からエンティティを抽出するための処理を書きます。
Entitiesクラスを定義し、descriptionに「テキストに出てくる全ての人名・組織名」と定義することで言語モデルに人名と組織名を抽出させるように指定
そして、そのEntitiesクラスをllm.with_structured_outputで言語モデルに渡してinvokeすることで質問から人名・組織名を抽出
コードは
class Entities(BaseModel):
"""Identifying information about entities."""
names: list[str] = Field(
...,
description="All the person, organization, or business entities that "
"appear in the text",
)
# Create a prompt for extracting entities
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are extracting organization and person entities from the text."),
("human", "Use the given format to extract information from the following input: {question}"),
]
)
dict_schema = convert_to_openai_function(Entities)
entity_chain = prompt | llm.with_structured_output(dict_schema)
entities = entity_chain.invoke({"question": question})
entity_names = eval(entities['names'])
この中の英文
フィールドの指定で
descriptionで
All the person, organization, or business entities that ”
“appear in the text”
は
「すべての個人、組織、または事業体」
「本文中に登場します」
プロンプトで
You are extracting organization and person entities from the text.
テキストから組織と個人のエンティティを抽出しています。
Use the given format to extract information from the following input: {question}
指定された形式を使用して、次の入力から情報を抽出します: {question}
わかりにくいのでGPTでこの部分を調べた
system と human の関係
* system メッセージ → AI の振る舞いやタスクの定義
* human メッセージ → 実際の入力データと実行すべき処理の具体的な指示
この二つを組み合わせることで、AI に 「何をするべきか(system)」 と 「何を元に処理するか(human)」を伝える
このため応用するなら
プロンプトとフィールドの部分を変えれば
他の用途、例えば場所や商品の検索にも使えるらしい
次にグラフデータ取得
ここは単なるNeo4jの操作
先ほどリスト形式で取得したエンティティをループさせてNeo4jのクエリを複数回投げる
これもそのまま使える
# Retrival of related nodes
related_nodes = []
for entity in entity_names:
response = graph.query(
"""
MATCH (n)-[:MENTIONS]->(m)
WHERE m.id = $entity
RETURN n.text AS text, n.embedding AS embedding
""",
{"entity": entity},
)
for record in response:
text = record["text"]
embedding = np.array(record["embedding"])
related_nodes.append((entity, text, embedding))
次に取得したグラフデータに対するベクトル類似度計算
上の質問で質問に関連する人物に関するグラフノードを取得しましたが、
元のデータの量によっては膨大な量のデータが取れるかもしれません
そのため、取得したグラフデータに対して質問文との類似度を計算し、
上位いくつかのデータだけをコンテキストとして採用する
まずは質問文の埋め込みベクトルを計算し、cosine_similarityで各グラフデータとの類似度を計算
上位いくつかのデータを並べて、#Documentというタグをつけておく
これもそのままで行けるはず
# Create embeddings from question
question_embedding = embeddings.embed_query(question)
# Calculation and sorting of cosine similarity
def cosine_similarity(vec1, vec2):
return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
related_nodes.sort(
key=lambda x: cosine_similarity(question_embedding, x[2]),
reverse=True
)
top_related_texts = [node[0] for node in related_nodes[:top_n]]
graph_context = ""
for node in related_nodes[:top_n]:
graph_context += f"#Document\nExplanation about '{node[0]}' - '{node[1]}'\n\n"
次に通常のベクトル検索も行う
グラフデータの検索だけでなく通常のRAG検索であるベクトル検索も行います
Neo4jVector.from_existing_indexを使用してグラフデータを取得するためのリトリーバーを取得してから、
それを使って検索を実行
これも#Documentというタグをつけてテキストに整形
ここもコードはそのままでOK
# Retrieve the vector retriever
vector_index = Neo4jVector.from_existing_index(
embeddings,
search_type="hybrid",
index_name="Document_embedding",
keyword_index_name="Document_keyword",
node_label="Document",
text_node_property="text",
embedding_node_property="embedding"
)
vector_retriever = vector_index.as_retriever()
# Execute vector search
vector_data = [el.page_content for el in vector_retriever.invoke(question)]
vector_context = ""
for text in vector_data[:top_n]:
vector_context += f"#Document\nAdditional information - '{text}'\n\n"
コンテキストの作成と言語モデルの実行
コンテキストができたので、あとはこれをLlamaに渡すだけ
このコード例ではgraph_retrieverとvector_retriverが上記のコンテキスト作成プログラムになっています
def context_builder(question: str) -> str:
graph_data = graph_retriever(question)
vector_data = vector_retriver(question)
return f"""
Context from graph data:
{graph_data}
Context from vector data:
{vector_data}
"""
# Create a prompt template for the final answer
template = """Answer the question based only on the following context:
{context}
Question: {question}
Use natural language and be concise.
Answer:"""
prompt = ChatPromptTemplate.from_template(template)
# Set up the final chain
chain = (
{
"context": context_builder,
"question": lambda x: x,
}
| prompt
| llm
| StrOutputParser()
)
input_question = input("Question: ")
result = chain.invoke(question)
print("Answer:", result)
プロンプトやchainの作成などはLangChainの基本的な使い方の話なのでここでは割愛
とあるのでGPTで解説
このコードは、質問 (question) に対して グラフデータとベクトルデータ を取得し、
それらの情報を元に回答を生成する処理を行っています。
LangChain を用いた パイプライン処理 を実装しており、関数型プログラミングのスタイルでデータの流れを明確にしています。
def context_builder(question: str) -> str:
graph_data = graph_retriever(question)
vector_data = vector_retriver(question)
return f"""
Context from graph data:
{graph_data}
Context from vector data:
{vector_data}
"""
で
graph_retriever(question) を用いて、質問に関連する グラフデータ を取得
vector_retriver(question) を用いて、質問に関連する ベクトルデータ を取得
それらを フォーマット済みのコンテキスト にまとめて返す
ポイント
* グラフデータ:ノードとエッジの関係を利用した情報(例:知識グラフ)
* ベクトルデータ:埋め込みモデルを使った検索結果(例:類似度検索)
* 統合したコンテキストを作成し、後続の LLM へ渡す
template = """Answer the question based only on the following context:
{context}
Question: {question}
Use natural language and be concise.
Answer:"""
prompt = ChatPromptTemplate.from_template(template)
でプロンプトの作成
役割
* LLM に与える プロンプトのテンプレート を定義。
* {context} の部分に context_builder() で作成したコンテキストを挿入。
* {question} に元の質問を挿入。
* 「与えられたコンテキストだけを使って回答せよ」 という指示を明示。
例
例えば、質問 “What is the capital of France?” に対して、context_builder() が以下のようなデータを返したとする:
Context from graph data:
– France -> Capital -> Paris
Context from vector data:
– The capital of France is Paris.
この場合、最終的なプロンプトは:
Answer the question based only on the following context:
Context from graph data:
– France -> Capital -> Paris
Context from vector data:
– The capital of France is Paris.
Question: What is the capital of France?
Use natural language and be concise.
Answer:
このプロンプトを LLM に渡して、回答を生成す
chain(処理の流れを定義)
chain = (
{
"context": context_builder,
"question": lambda x: x,
}
| prompt
| llm
| StrOutputParser()
)
データの流れを順番に処理するためのパイプライン を構築
{
"context": context_builder,
"question": lambda x: x,
}
context_builder(question) で コンテキストを生成。
lambda x: x で 質問をそのまま渡す
| prompt
ChatPromptTemplate を適用し、コンテキストと質問を埋め込んだプロンプトを作成。
| llm
prompt で作成したプロンプトを LLM に入力し、回答を得る
| StrOutputParser()
LLM の出力を文字列としてパース し、最終的な回答として返す
ユーザーの質問を処理
input_question = input("Question: ")
result = chain.invoke(question)
print("Answer:", result)
* input() で ユーザーの質問を受け取る。
* chain.invoke(question) を実行し、質問に基づいて LLM に回答を生成させる。
* print() で 最終的な回答を出力。
ポイント
✅ グラフ検索 (graph_retriever()) と ベクトル検索 (vector_retriver()) を組み合わせて、幅広い情報を取得
✅ プロンプトテンプレート (ChatPromptTemplate) を使用して、質問とコンテキストを LLM に適切に渡す
✅ データ処理の流れをパイプライン (| 演算子) で簡潔に記述
✅ LLM への入力と出力を一貫して処理できるフレームワーク
改良のアイデア
1. スコア付きの情報を使用
* graph_data や vector_data にスコアを追加し、信頼性の高い情報を優先。
2. 回答に信頼性スコアを付与
* LLM の回答に「出典(graph / vector)」を示す。
3. エンティティ拡張
* question からエンティティを抽出し、graph_retriever() の検索クエリを改善。
とのこと
ここまで完成したら、以下のようなプロンプトを投げてみます。
Where did Dumbledore and Harry entered in search of a Horcrux?
ダンブルドアとハリーは分霊箱を探してどこに入りましたか?
graph_dataをログに出力してみると、HarryとDumbledoreが人名として解釈され、2人に言及するグラフデータが取得されてコンテキストとして整形されています
結果ログは
Context from graph data:
#Document
Explanation about 'Dumbledore' - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Explanation about 'Harry' - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Explanation about 'Dumbledore' - 'Harry awakens in the Hogwarts infirmary, where Dumbledore explains that Harry was protected by his mother's love, which also killed Quirrell. Voldemort survived, and the Philosopher's Stone was destroyed. During the school's end-of-year feast,'
#Document
Explanation about 'Harry' - 'Hagrid takes Harry to Diagon Alley to shop for school supplies. Harry buys a wand, and the proprietor tells him that his wand and Voldemort's wand contain feathers from the same phoenix. At the end of the summer, Harry boards the Hogwarts Express,'
#Document
Explanation about 'Dumbledore' - 'that both wands contain as their cores both comes from Fawkes, the phoenix that Dumbledore keeps as a pet in his office until his death in Half-Blood Prince.[26] Harry's wand is broken in Deathly Hallows. For a time, he borrows Hermione's wand, and'
graph_dataをログに出力してみると、HarryとDumbledoreが人名として解釈され、
2人に言及するグラフデータが取得されてコンテキストとして整形されていま
ベクトル検索データの確認
ベクトル検索から得られたコンテキストも確認します。
だいたい同じようなデータが取得されています
Context from vector data:
#Document
Additional information - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Additional information - 'Dumbledore to drink, he is attacked by Inferi. They try to drown Harry, but Dumbledore rescues him. Dumbledore and Harry then return to the Hogwarts Astronomy Tower with the Horcrux. Dumbledore asks Harry not to interfere in the events that are'
#Document
Additional information - 'Harry, Ron, and Hermione leave Hogwarts in Harry Potter and the Deathly Hallows (2007) to search for and demolish Voldemort's four remaining Horcruxes. They must be destroyed with basilisk venom, the Sword of Gryffindor, or some other powerful'
#Document
Additional information - 'or some other powerful means. Ron, Hermione, Vincent Crabbe and Neville Longbottom each destroy one of these Horcruxes. After a battle at Hogwarts, Harry goes into the Forbidden Forest to meet Voldemort, who uses the Elder Wand to cast the Killing'
最終的な質問文
ChatPromptTemplateに渡しているテンプレートの内容と合わさって、最終的にLlamaには以下のような質問が投げられます
Answer the question based only on the following context:
Context from graph data:
#Document
Explanation about 'Dumbledore' - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Explanation about 'Harry' - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Explanation about 'Dumbledore' - 'Harry awakens in the Hogwarts infirmary, where Dumbledore explains that Harry was protected by his mother's love, which also killed Quirrell. Voldemort survived, and the Philosopher's Stone was destroyed. During the school's end-of-year feast,'
#Document
Explanation about 'Harry' - 'Hagrid takes Harry to Diagon Alley to shop for school supplies. Harry buys a wand, and the proprietor tells him that his wand and Voldemort's wand contain feathers from the same phoenix. At the end of the summer, Harry boards the Hogwarts Express,'
#Document
Explanation about 'Dumbledore' - 'that both wands contain as their cores both comes from Fawkes, the phoenix that Dumbledore keeps as a pet in his office until his death in Half-Blood Prince.[26] Harry's wand is broken in Deathly Hallows. For a time, he borrows Hermione's wand, and'
Context from vector data:
#Document
Additional information - 'Near the end of the book, Dumbledore and Harry enter a cave in search of a Horcrux. Dumbledore drinks a potion inside the Horcrux's container and begins to scream in agony. When Harry attempts to retrieve some lake water for Dumbledore to drink, he'
#Document
Additional information - 'Dumbledore to drink, he is attacked by Inferi. They try to drown Harry, but Dumbledore rescues him. Dumbledore and Harry then return to the Hogwarts Astronomy Tower with the Horcrux. Dumbledore asks Harry not to interfere in the events that are'
#Document
Additional information - 'Harry, Ron, and Hermione leave Hogwarts in Harry Potter and the Deathly Hallows (2007) to search for and demolish Voldemort's four remaining Horcruxes. They must be destroyed with basilisk venom, the Sword of Gryffindor, or some other powerful'
#Document
Additional information - 'or some other powerful means. Ron, Hermione, Vincent Crabbe and Neville Longbottom each destroy one of these Horcruxes. After a battle at Hogwarts, Harry goes into the Forbidden Forest to meet Voldemort, who uses the Elder Wand to cast the Killing'
Question: Where did Dumbledore and Harry entered in search of a Horcrux?
Use natural language and be concise.
Answer:
これだけ事前情報が与えられれば、パラメータ数の少ないLlama3.2 3Bでも正確な回答ができます。回答は以下の
A cave.
となる