Closed7

Haystackチュートリアルをやってみる: Build Your First Question Answering System

kun432kun432

Colaboratoryで進める。

GPUを有効にする必要があるので、「ノートブックの設定」で"T4 GPU"を使用する。

GPUが有効になっているかを確認。

%%bash

nvidia-smi

ロギングを有効にする。

import logging

logging.basicConfig(format="%(levelname)s - %(name)s -  %(message)s", level=logging.WARNING)
logging.getLogger("haystack").setLevel(logging.INFO)
kun432kun432

インストール

%%bash

pip install --upgrade pip
pip install farm-haystack[colab,inference]

インストールオプションは色々ある様子。必要なオプションだけを有効化できるのは良い。

https://docs.haystack.deepset.ai/docs/installation

テレメトリーを有効化する。

from haystack.telemetry import tutorial_running

tutorial_running(1)

これはHaystackがチュートリアルの実施統計を取るための設定らしい。もちろん実際の環境では有効にする必要はない。

kun432kun432

DocumentStoreを初期化する。DocumentStoreはQAが参照するためのドキュメントを保存しておく場所。

InMemoryDocumentStoreはその名の通りメモリ上に保持する。use_bm25なのでキーワード検索ってことか。

from haystack.document_stores import InMemoryDocumentStore

document_store = InMemoryDocumentStore(use_bm25=True)

ドキュメントを用意する。サンプルとしてゲーム・オブ・スローンズのWikipediaのコンテンツが使用されている。なるほど、ダウンロード用のユーティリティみたいなものも用意されている様子。

from haystack.utils import fetch_archive_from_http

doc_dir = "data/build_your_first_question_answering_system"

fetch_archive_from_http(
    url="https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/wiki_gameofthrones_txt1.zip",
    output_dir=doc_dir,
)

ダウンロード・展開されたドキュメントはこんな感じ。エピソードごとのあらすじっぽい。

TextIndexingPipelineを使って、ドキュメントをHaystackのDocumentObjectに変換しつつDocumentStoreに読み込む。

import os
from haystack.pipelines.standard_pipelines import TextIndexingPipeline

files_to_index = [doc_dir + "/" + f for f in os.listdir(doc_dir)]
indexing_pipeline = TextIndexingPipeline(document_store)
indexing_pipeline.run_batch(file_paths=files_to_index)

出力を見てると、トークナイザーとしてNLTK PUNKTを使ってる様子。BM25でインデックスも作成してるのかな?

[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.
INFO:haystack.pipelines.base:It seems that an indexing Pipeline is run, so using the nodes' run method instead of run_batch.
Converting files: 100%|██████████| 183/183 [00:03<00:00, 55.38it/s]
Preprocessing:   6%|▌         | 11/183 [00:00<00:01, 96.97docs/s]WARNING:haystack.nodes.preprocessor.preprocessor:We found one or more sentences whose word count is higher than the split length.
Preprocessing:  51%|█████     | 93/183 [00:01<00:00, 104.41docs/s]WARNING:haystack.nodes.preprocessor.preprocessor:Document dd048b8e5bcb7de1be5bd3937f15442f is 14232 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.
Preprocessing:  57%|█████▋    | 105/183 [00:01<00:00, 103.11docs/s]WARNING:haystack.nodes.preprocessor.preprocessor:Document b48fb0da693eb4d81b3566d0069868b3 is 10488 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.
Preprocessing:  86%|████████▌ | 157/183 [00:01<00:00, 137.11docs/s]WARNING:haystack.nodes.preprocessor.preprocessor:Document 4189b42892b3d941c035947d512b69dd is 12059 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.
Preprocessing: 100%|██████████| 183/183 [00:01<00:00, 110.02docs/s]
Updating BM25 representation...: 100%|██████████| 2359/2359 [00:00<00:00, 14216.04 docs/s]

DocumentObjectはこんな感じ。

{'documents': [<Document: {'content': "\n\n'''Jorah Mormont''' is a fictional character in the ''A Song of Ice and Fire'' series of fantasy novels by American author George R. R. Martin and its television adaptation ''Game of Thrones''.\n\nIntroduced in ''A Game of Thrones'' (1996), Jorah is a knight in exile, the disgraced former lord of Bear Island and the only son of Jeor Mormont, the honorable lord commander of the Night's Watch. Jorah subsequently appeared in Martin's ''A Clash of Kings'' (1998), ''A Storm of Swords'' (2000) and ''A Dance with Dragons'' (2011). After fleeing Westeros, Jorah pledges fealty to Daenerys Targaryen and over the course of both the novels and the television show becomes her closest and most loyal companion; Jorah's passionate yet unrequited love of Daenerys is central to the character's arc in both the novels and television show. He is portrayed as a skilled warrior whose knowledge of the peoples and customs of Essos proves invaluable to Daenerys' journeys.\n\nJorah is portrayed by the Scottish actor Iain Glen in the HBO television adaptation.\n\n", 'content_type': 'text', 'score': None, 'meta': {'_split_id': 0}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '89eee1cbee4aceaae32cba0f2b44a06f'}>,
  <Document: {'content': "==Character==\n===Background===\nSer Jorah Mormont is the only child of the Night's Watch's Lord Commander Jeor Mormont, who abdicated shortly before Robert's Rebellion to join the Night's Watch and let Jorah assume the lordship of Bear Island. At some point Jorah married a lady of House Glover, who died from miscarriage after ten years of marriage. Jorah fought in Greyjoy's Rebellion, distinguishing himself by being one of the first to enter the fray during the siege of Pyke and was knighted by King Robert Baratheon. At a tourney at Lannisport celebrating the Baratheon victory, Jorah fell in love with the beautiful Lynesse Hightower (aunt of Margaery Tyrell). He named her Queen of Love and Beauty after winning the tourney and asked her father for her hand in marriage, which he accepted. However, Lynesse found herself ill-suited to the rough life on Bear Island, having grown up as a member of the wealthy House Hightower. Jorah bankrupted himself trying to provide her with luxuries and eventually sold poachers found on his lands to a Tyroshi slaver to fund her lifestyle. His liege lord, Eddard Stark, condemned Jorah to death, but he and Lynesse fled to Lys. ", 'content_type': 'text', 'score': None, 'meta': {'_split_id': 1}, 'id_hash_keys': ['content'], 'embedding': None, 'id': 'e3cb79068ac2863eeaf1c039afd3fe09'}>,
...
kun432kun432

検索はRetriverで行う。Retrieverは関連するドキュメントを抽出する。まずRetrieverで初期化。

from haystack.nodes import BM25Retriever

retriever = BM25Retriever(document_store=document_store)

Readerは、Retrieverの検索結果を受けて、回答候補を抽出するもの、らしい。こちらも初期化。

from haystack.nodes import FARMReader

reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True)

おそらく一般的なRAGではこの部分をOpenAIとかのLLM使って「生成」することが多いと思うのだけど、ここで使われているのは「抽出」のためのモデルみたい。GPUを使うのも納得。

https://huggingface.co/roberta-base

https://huggingface.co/deepset/roberta-base-squad2

で、実行するとこんな感じでモデルのダウンロードが行われる。

INFO:haystack.modeling.utils:Using devices: CUDA:0 - Number of GPUs: 1
INFO:haystack.modeling.utils:Using devices: CUDA:0 - Number of GPUs: 1
Downloading (…)lve/main/config.json: 100%
571/571 [00:00<00:00, 39.0kB/s]
INFO:haystack.modeling.model.language_model: * LOADING MODEL: 'deepset/roberta-base-squad2' (Roberta)
Downloading model.safetensors: 100%
496M/496M [00:02<00:00, 244MB/s]
INFO:haystack.modeling.model.language_model:Auto-detected model language: english
INFO:haystack.modeling.model.language_model:Loaded 'deepset/roberta-base-squad2' (Roberta model) from model hub.
Downloading (…)okenizer_config.json: 100%
79.0/79.0 [00:00<00:00, 5.45kB/s]
Downloading (…)olve/main/vocab.json: 100%
899k/899k [00:00<00:00, 1.81MB/s]
Downloading (…)olve/main/merges.txt: 100%
456k/456k [00:00<00:00, 27.8MB/s]
Downloading (…)cial_tokens_map.json: 100%
772/772 [00:00<00:00, 43.3kB/s
kun432kun432

RetrieverとReaderをつなぐパイプラインを用意する。ビルトインですでに用意されているExtractiveQAPipelineを使う。

from haystack.pipelines import ExtractiveQAPipeline

pipe = ExtractiveQAPipeline(reader, retriever)

パイプラインを実行して質問してみる。

from pprint import pprint

prediction = pipe.run(
    query="Who is the father of Arya Stark?", params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}}
)

pprint(prediction)

結果。10個のドキュメントがRetrieverで検索されて、そこから5つの回答候補が抽出されてるのがわかる。

Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  2.82 Batches/s]
{'answers': [
             <Answer {'answer': 'Eddard', 'type': 'extractive', 'score': 0.993372917175293, 'context': "s Nymeria after a legendary warrior queen. She travels with her father, Eddard, to King's Landing when he is made Hand of the King. Before she leaves,", 'offsets_in_document': [{'start': 207, 'end': 213}], 'offsets_in_context': [{'start': 72, 'end': 78}], 'document_ids': ['9e3c863097d66aeed9992e0b6bf1f2f4'], 'meta': {'_split_id': 3}}>,
             <Answer {'answer': 'Ned', 'type': 'extractive', 'score': 0.9753613471984863, 'context': "k in the television series.\n\n====Season 1====\nArya accompanies her father Ned and her sister Sansa to King's Landing. Before their departure, Arya's h", 'offsets_in_document': [{'start': 630, 'end': 633}], 'offsets_in_context': [{'start': 74, 'end': 77}], 'document_ids': ['7d3360fa29130e69ea6b2ba5c5a8f9c8'], 'meta': {'_split_id': 10}}>,
             <Answer {'answer': 'Lord Eddard Stark', 'type': 'extractive', 'score': 0.9177325963973999, 'context': 'rk daughters.\n\nDuring the Tourney of the Hand to honour her father Lord Eddard Stark, Sansa Stark is enchanted by the knights performing in the event.', 'offsets_in_document': [{'start': 280, 'end': 297}], 'offsets_in_context': [{'start': 67, 'end': 84}], 'document_ids': ['5dbccad397381605eba063f71dd500a6'], 'meta': {'_split_id': 3}}>,
             <Answer {'answer': 'Ned', 'type': 'extractive', 'score': 0.8396497964859009, 'context': " girl disguised as a boy all along and is surprised to learn she is Arya, Ned Stark's daughter. After the Goldcloaks get help from Ser Amory Lorch and", 'offsets_in_document': [{'start': 848, 'end': 851}], 'offsets_in_context': [{'start': 74, 'end': 77}], 'document_ids': ['257088f56d2faba55e2ef2ebd19502dc'], 'meta': {'_split_id': 31}}>,
             <Answer {'answer': 'King Robert', 'type': 'extractive', 'score': 0.6922299861907959, 'context': "en refuses to yield Gendry, who is actually a bastard son of the late King Robert, to the Lannisters.  The Night's Watch convoy is overrun and massacr", 'offsets_in_document': [{'start': 579, 'end': 590}], 'offsets_in_context': [{'start': 70, 'end': 81}], 'document_ids': ['4d51b1876e8a7eac8132b97e2af04401'], 'meta': {'_split_id': 4}}>],
 'documents': [<Document: {'content': "She names her direwolf Lady; she is the smallest of the pack and the first to die, sentenced to death by Cersei after Arya's direwolf, Nymeria, bit a violent Joffrey.\n\n===Arya Stark===\nMaisie Williams\n\n'''Arya Stark''' portrayed by Maisie Williams. Arya Stark of House Stark is the younger daughter and third child of Lord Eddard and Catelyn Stark of Winterfell. Ever the tomboy, Arya would rather be training to use weapons than sewing with a needle. She names her direwolf Nymeria, after a legendary warrior queen.\n\n===Robb Stark===\nRichard Madden\n\n'''Robb Stark''' (seasons 1–3) portrayed by Richard Madden. Robb Stark of House Stark is the eldest son of Eddard and Catelyn Stark and the heir to Winterfell. His dire wolf is called Grey Wind. Robb becomes involved in the war against the Lannisters after his father, Ned Stark, is arrested for treason. Robb summons his bannermen for war against House Lannister and marches to the Riverlands. Eventually, crossing the river at the Twins becomes strategically necessary. To win permission to cross, Robb agrees to marry a daughter of Walder Frey, Lord of the Twins. Robb leads the war effort against the Lannisters and successfully captures Jaime. ", 'content_type': 'text', 'score': 0.8972880480457244, 'meta': {'_split_id': 15}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '26af9634a6cb6995e9dc2f06ad097764'}>,
               <Document: {'content': 'Theon Greyjoy is a prisoner of the Boltons, mutilated and driven nearly insane by torture at the hands of Roose\'s sadistic son Ramsay, who renames him "Reek". To cement his rule over the North, Roose Bolton has Ramsay apparently married to Arya Stark; his bride is actually Jeyne Poole, a friend of Arya\'s sister Sansa forced to impersonate Arya. Ramsay abuses Jeyne physically and sexually. Mance Rayder arrives at Winterfell in disguise and enlists Theon to help him free the false Arya. Mance and his spearwives are left behind while Theon and Jeyne escape, leaping from the castle wall into the snow to be captured by Stannis\'s forces.\n\n====The South====\n\nJaime Lannister, the uncle (and, secretly, father) of the young king Tommen Baratheon, negotiates the surrender of the last of the late Robb Stark\'s allies, nominally putting an end to the Stark–Lannister war in the Riverlands. Brienne of Tarth, whom Jaime had sent to search for Sansa Stark (and who unbeknownst to Jaime was captured by Sansa\'s undead, vengeful mother Catelyn), finds Jaime and tells him Sansa is in danger; he follows after her.\n\n', 'content_type': 'text', 'score': 0.8944335671589794, 'meta': {'_split_id': 7}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '34ba43cba62916a0192f5d9095fa7e67'}>,
               <Document: {'content': 'Sansa lies to King Robert about the circumstances of the fight in order to protect both Joffrey and her sister Arya.  Since Arya ran off with her wolf to save it, Sansa\'s wolf is killed instead, estranging the Stark daughters.\n\nDuring the Tourney of the Hand to honour her father Lord Eddard Stark, Sansa Stark is enchanted by the knights performing in the event.  At the request of his mother, Queen Cersei Lannister, Joffrey spends a portion of the tourney with Sansa, but near the end he commands his guard Sandor Clegane, better known as The Hound, to take her back to her quarters. Sandor explains how his older brother, Gregor, aka "Mountain that Rides" pushed his face into a brazier of hot coals, for playing with one of his wooden toys.\n\nAfter Eddard discovers the truth of Joffrey\'s paternity, he tells Sansa that they will be heading back to Winterfell. Sansa is devastated and wishes to stay in King\'s Landing, so she runs off to inform Queen Cersei of her father\'s plans, unwittingly providing Cersei with the information needed to arrest her father. ', 'content_type': 'text', 'score': 0.8944116483150227, 'meta': {'_split_id': 3}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '5dbccad397381605eba063f71dd500a6'}>,
               <Document: {'content': '== Storylines ==\n=== Novels ===\n==== \'\'A Game of Thrones\'\' ====\nCoat of arms of House Stark\n\nArya adopts a direwolf cub, which she names Nymeria after a legendary warrior queen. She travels with her father, Eddard, to King\'s Landing when he is made Hand of the King. Before she leaves, her half-brother Jon Snow has a smallsword made for her as a parting gift, which she names "Needle" after her least favorite ladylike activity.\n\nWhile taking a walk together, Prince Joffrey and her sister Sansa happen upon Arya and her friend, the low-born butcher apprentice Mycah, sparring in the woods with broomsticks.  Arya defends Mycah from Joffrey\'s torments and her direwolf Nymeria helps Arya fight off Joffrey, wounding his arm in the process.  Knowing that Nymeria will likely be killed in retribution, Arya chases her wolf away; but Sansa\'s direwolf Lady is killed in Nymeria\'s stead and Mycah is hunted down and killed by Sandor Clegane, Joffrey\'s bodyguard.\n\nIn King\'s Landing, her father discovers Arya\'s possession of Needle, but instead of confiscating it he arranges for fencing lessons under the Braavosi swordmaster Syrio Forel, who teaches her the style of fighting known as "water dancing".  ', 'content_type': 'text', 'score': 0.8911998637494184, 'meta': {'_split_id': 3}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '9e3c863097d66aeed9992e0b6bf1f2f4'}>,
               <Document: {'content': 'When Rafford begs for a healer, Arya cuts his throat in the same fashion as he had killed Lommy and throws his corpse into a canal.  She heads back to perform the play, knowing this murder will most likely ruin her Mercedene identity.\n\n=== Family tree of House Stark ===\n\n=== Television series ===\n\nArya Stark is portrayed by English actress Maisie Williams in the television adaption of the book series, this being Williams\' first role as an actress. Williams was chosen from among 300 actresses across England.\nMaisie Williams plays the role of Arya Stark in the television series.\n\n====Season 1====\nArya accompanies her father Ned and her sister Sansa to King\'s Landing. Before their departure, Arya\'s half-brother Jon Snow gifts Arya a sword which she dubs "Needle". On the Kingsroad, Arya is sparring with a butcher\'s boy, Mycah, when Sansa\'s betrothed Prince Joffrey Baratheon attacks Mycah, prompting Arya\'s direwolf Nymeria to bite Joffrey. Arya shoos Nymeria away so she is not killed, but is furious when Sansa later refuses to support her version of events. Mycah is later killed by Joffrey\'s bodyguard Sandor "The Hound" Clegane, earning him Arya\'s hatred. ', 'content_type': 'text', 'score': 0.8887660099616093, 'meta': {'_split_id': 10}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '7d3360fa29130e69ea6b2ba5c5a8f9c8'}>,
               <Document: {'content': "After her father's arrest, Syrio is killed protecting her and Arya narrowly escapes capture.  She later witnesses the public execution of her father before falling under the protection of the Night's Watch recruiter Yoren.\n\n==== ''A Clash of Kings'' ====\nArya escapes King's Landing with Yoren and his party of recruits; and on the road, she clashes with the other Night's Watch child recruits Lommy, Gendry, and Hot Pie but eventually befriends them. On the way, the party is attacked by Amory Lorch when Yoren refuses to yield Gendry, who is actually a bastard son of the late King Robert, to the Lannisters.  The Night's Watch convoy is overrun and massacred, but Arya and the other children escape through a tunnel.  Before escaping, she rescues three prisoners locked in a wagon cage, among them a mysterious man named Jaqen H'ghar.\n\nArya and her friends are later captured by Ser Gregor Clegane and taken to Harrenhal as slave laborers.  After witnessing the torture death of her fellow prisoners, Arya begins nightly reciting the names of the people upon whom she wishes to exact revenge.  ", 'content_type': 'text', 'score': 0.8883622030478101, 'meta': {'_split_id': 4}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '4d51b1876e8a7eac8132b97e2af04401'}>,
               <Document: {'content': "Gendry refuses, to the shame of the master smith. After Ned's arrest and eventual execution, arrangements are made for Yoren of the Night's Watch to take Gendry to the Wall with him.\n\n===Season 2===\nGendry travels North with Yoren and other Night's Watch recruits, including Arya Stark (disguised as an orphan boy named 'Arry), Lommy Greenhands, Hot Pie and Jaqen H'ghar. During their journey, they are stopped by the Goldcloaks of the City Watch, who demand that Yoren hand Gendry over to them - King Joffrey has ordered that all of his father Robert's bastards be killed, but Yoren turns the Goldcloaks away. Later, Gendry forces Arya to reveal her true identity, and is surprised to learn she is in fact Ned Stark's daughter. After the Goldcloaks get help from Ser Amory Lorch and his men, they ambush the travelling party. In the chaos, Yoren is killed. Gendry's life is then saved by Arya, who convinces the Goldcloaks that Lommy, who was killed during the attack, was in fact Gendry. Gendry and the rest of the recruits are then escorted to Harrenhal, the ruined castle-turned-prison. ", 'content_type': 'text', 'score': 0.88822099471314, 'meta': {'_split_id': 6}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '685a2d32df2b2312acf9c6c19062fe2'}>,
               <Document: {'content': "In ''A Feast for Crows'', Jon Snow sends him to the Citadel by sea, but Maester Aemon dies on the voyage between Braavos and Oldtown.\n\nIn the HBO television adaptation, he is portrayed by Peter Vaughan.\n\n=== Yoren ===\nYoren is a recruiter of the Night's Watch. In ''A Game of Thrones'' Yoren travels with Tyrion Lannister from the Wall to King's Landing, and is present when Tyrion is arrested by Catelyn Stark. He then races to King's Landing to inform Eddard Stark. During Lord Eddard's execution, he finds Arya Stark and shields her from seeing her father's death. In ''A Clash of Kings'' he disguises Arya as a boy recruit to smuggle her to Winterfell, but is killed by Lannister soldiers.\n\nIn the HBO television adaptation, he is portrayed by Francis Magee.\n\n=== Samwell Tarly ===\n\nSamwell Tarly, called 'Sam', is the elder son of Lord Randyll Tarly of Horn Hill. Ten chapters throughout ''A Storm of Swords'' and ''A Feast for Crows'' are told from his point of view. In spite of his self-professed cowardice, Sam is highly intelligent, resourceful, and loyal. ", 'content_type': 'text', 'score': 0.883679782276301, 'meta': {'_split_id': 76}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '10e153b615f441b825f454e8c3909778'}>,
               <Document: {'content': "Gendry is an apprentice blacksmith in King's Landing and an unacknowledged bastard of King Robert. Gendry shows promise as a smith and makes a helmet in the shape of a bull's head; Eddard compliments the helmet, offering to purchase it. Gendry refuses, to the shame of the master smith. After Eddard Stark's fall and eventual execution, arrangements are made for Yoren of the Night's Watch to take Gendry to the Wall with him; he travels North with Yoren and other recruits, including Arry, Lommy Greenhands, Hot Pie and Jaqen H'ghar. During their journey, they are stopped by the Goldcloaks, who demand that Yoren give up Gendry as King Joffrey wants all of his father's bastards killed but are forced to leave by Yoren. Later, Gendry reveals to Arry that he knows she is a girl disguised as a boy all along and is surprised to learn she is Arya, Ned Stark's daughter. After the Goldcloaks get help from Ser Amory Lorch and his men who kill Yoren, Gendry's life is saved by Arya when she lies to the Goldcloaks that Lommy, who was killed during the attack, was Gendry. ", 'content_type': 'text', 'score': 0.8834753753880283, 'meta': {'_split_id': 31}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '257088f56d2faba55e2ef2ebd19502dc'}>,
               <Document: {'content': "Yara accuses him of killing their father; to her surprise, Euron freely admits to it, declaring that Balon was leading the Ironborn to ruin. He promises to conquer Westeros by marrying Daenerys and offering her the Iron Fleet. He is subsequently chosen as King. Theon and Yara, realising Euron will have them put to death, flee with the best ships of the Iron Fleet. Undaunted, Euron orders the Ironborn to begin construction of a new fleet.\n\n===In Braavos===\nJaqen offers Arya the assignment of killing an actress named Lady Crane, who is playing Cersei in a play recounting the deaths of Robert Baratheon and Ned Stark. While observing the play, Arya is noticeably distressed by the play's portrayal of Ned.\n\n===In Meereen===\nTyrion and Varys note that peace has fallen over Meereen since they forged their pact with the Masters. In order to preserve this, Tyrion summons the red priestess Kinvara, who agrees to preach to the people that Daenerys is the chosen one of the Lord of Light. She also claims to know what originally happened to Varys and why, unnerving the eunuch.\n\n", 'content_type': 'text', 'score': 0.8807893650320407, 'meta': {'_split_id': 2}, 'id_hash_keys': ['content'], 'embedding': None, 'id': 'f419fcbf20cc435a12445877cd2983d3'}>],
 'no_ans_gap': 11.405388832092285,
 'node_id': 'Reader',
 'params': {'Reader': {'top_k': 5}, 'Retriever': {'top_k': 10}},
 'query': 'Who is the father of Arya Stark?',
 'root_node': 'Query'}

シンプルに回答だけがほしければ、そういうユーティリティが用意されている。

from haystack.utils import print_answers

print_answers(prediction, details="minimum")  ## Choose from `minimum`, `medium`, and `all`

結果

Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  3.15 Batches/s]'Query: Who is the father of Arya Stark?'
'Answers:'
[   {   'answer': 'Eddard',
        'context': 's Nymeria after a legendary warrior queen. She travels '
                   "with her father, Eddard, to King's Landing when he is made "
                   'Hand of the King. Before she leaves,'},
    {   'answer': 'Ned',
        'context': 'k in the television series.\n'
                   '\n'
                   '====Season 1====\n'
                   'Arya accompanies her father Ned and her sister Sansa to '
                   "King's Landing. Before their departure, Arya's h"},
    {   'answer': 'Lord Eddard Stark',
        'context': 'rk daughters.\n'
                   '\n'
                   'During the Tourney of the Hand to honour her father Lord '
                   'Eddard Stark, Sansa Stark is enchanted by the knights '
                   'performing in the event.'},
    {   'answer': 'Ned',
        'context': ' girl disguised as a boy all along and is surprised to '
                   "learn she is Arya, Ned Stark's daughter. After the "
                   'Goldcloaks get help from Ser Amory Lorch and'},
    {   'answer': 'King Robert',
        'context': 'en refuses to yield Gendry, who is actually a bastard son '
                   "of the late King Robert, to the Lannisters.  The Night's "
                   'Watch convoy is overrun and massacr'}]
このスクラップは2023/10/03にクローズされました