haystack/tutorials/Tutorial1_Basic_QA_Pipeline.ipynb

200 lines
5.3 KiB
Plaintext
Raw Normal View History

2019-11-14 11:42:51 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
2019-11-14 11:42:51 +01:00
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
2019-11-14 11:42:51 +01:00
"source": [
"from farm_haystack.reader.adaptive_model import FARMReader\n",
"from farm_haystack.retriever.tfidf import TfidfRetriever\n",
"from farm_haystack import Finder\n",
"from farm_haystack.indexing.io import write_documents_to_db, fetch_archive_from_http\n",
"from farm_haystack.indexing.cleaning import clean_wiki_text\n",
"from farm_haystack.utils import print_answers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task: Question Answering for Game of Thrones\n",
"\n",
"<img style=\"float: right;\" src=\"https://upload.wikimedia.org/wikipedia/en/d/d8/Game_of_Thrones_title_card.jpg\">\n",
"\n",
"Question Answering can be used in a variety of use cases. A very common one: Using it to navigate through complex knowledge bases or long documents (\"search setting\").\n",
"\n",
"A \"knowledge base\" could for example be your website, an internal wiki or a collection of financial reports. \n",
"In this tutorial we will work on a slightly different domain: \"Game of Thrones\". \n",
"\n",
"Let's see how we can use a bunch of wikipedia articles to answer a variety of questions about the \n",
"marvellous seven kingdoms... \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Indexing & cleaning documents"
]
},
2019-11-14 11:42:51 +01:00
{
"cell_type": "code",
"execution_count": null,
2019-11-14 11:42:51 +01:00
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
2019-11-14 11:42:51 +01:00
"source": [
"# Init a database (default: sqllite)\n",
"from farm_haystack.database import db\n",
"db.create_all()\n",
"\n",
"# Let's first get some documents that we want to query\n",
"# Here: 517 Wikipedia articles for Game of Thrones\n",
2019-11-14 11:42:51 +01:00
"doc_dir = \"data/article_txt_got\"\n",
"s3_url = \"https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/wiki_gameofthrones_txt.zip\"\n",
"fetch_archive_from_http(url=s3_url, output_dir=doc_dir)\n",
"\n",
"# Now, let's write the docs to our DB. \n",
"# You can supply a cleaning/transformation function that is applied to each doc (e.g. to remove headers)\n",
"# It must take a str as input, and return a str.\n",
"# Our 517 documents also get splitted into 2068 smaller paragraphs here. \n",
2019-11-14 11:42:51 +01:00
"write_documents_to_db(document_dir=doc_dir, clean_func=clean_wiki_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initalize Reader, Retriever & Finder"
]
},
2019-11-14 11:42:51 +01:00
{
"cell_type": "code",
"execution_count": null,
2019-11-14 11:42:51 +01:00
"metadata": {
"pycharm": {
"is_executing": false,
"name": "#%%\n"
}
},
"outputs": [],
2019-11-14 11:42:51 +01:00
"source": [
"# A retriever identifies the k most promising chunks of text that might contain the answer for our question\n",
"# Retrievers use some simple but fast algorithm, here: TF-IDF\n",
"retriever = TfidfRetriever()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# A reader scans the text chunks in detail and extracts the k best answers\n",
"# Reader use more powerful but slower deep learning models, here: a BERT QA model trained via FARM on Squad 2.0\n",
"reader = FARMReader(model_dir=\"../FARM/saved_models/bert-english-qa-large\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# The Finder sticks together retriever and retriever in a pipeline to answer our actual questions \n",
"finder = Finder(reader, retriever)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Voilá! Ask a question!"
]
},
2019-11-14 11:42:51 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"prediction = finder.get_answers(question=\"Who is the father of Arya Stark?\", top_k_reader=10, top_k_retriever=5)"
2019-11-14 11:42:51 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
2019-11-14 11:42:51 +01:00
"outputs": [],
"source": [
"#prediction = finder.get_answers(question=\"Who created the Dothraki vocabulary?\", top_k_reader=5)\n",
"#prediction = finder.get_answers(question=\"Who is the sister of Sansa?\", top_k_reader=5)"
2019-11-14 11:42:51 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": false,
2019-11-14 11:42:51 +01:00
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"print_answers(prediction, details=\"minimal\")"
2019-11-14 11:42:51 +01:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
2019-11-14 11:42:51 +01:00
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}