Question-Answering Chatbot via Transformer

Question-Answering Chatbot via Transformer - 图1

Susana @ Jina AI

Question-Answering Chatbot via Transformer - 图2June 15, 2021

We will use the hello world chatbot for this tutorial. You can find the complete code here and we will go step by step.

At the end of this tutorial, you will have your own chatbot. You will use text as an input and get a text result as output. For this example, we will use a covid dataset. You will understand how every part of this example works and how you can create new apps with different datasets on your own.

Define data and work directories

We can start by creating an empty folder, I’ll call mine tutorial and that’s the name you’ll see through the tutorial but feel free to use whatever you wish.

We will display our results in our browser, so download the static folder from here, and paste it into your tutorial folder. This is only the CSS and HTML files to render our results. We will use a dataset in a .csv format. We’ll use the COVID dataset from Kaggle.

Download it under your tutorial directory:

  1. wget https://static.jina.ai/chatbot/dataset.csv

Create Documents from a csv file

To create a Document in Jina, we do it like this:

  1. doc = Document(content='hello, world!')

In our case, the content of our Document needs to be the dataset we want to use:

  1. from jina.types.document.generators import from_csv
  2. with open('dataset.csv') as fp:
  3. docs = from_csv(fp, field_resolver={'question': 'text'})

So what happened there? We created a generator of Documents docs, and we used from_csv to load our dataset. We use field_resolver to map the text from our dataset to the Document attributes.

Finally, we can combine the 2 previous steps (loading the dataset into Documents and starting the context) and index like this:

  1. from jina.types.document.generators import from_csv
  2. with flow, open('dataset.csv') as fp:
  3. flow.index(from_csv(fp, field_resolver={'question': 'text'}))

See Also

from_csv is a function that belongs to the jina.types.document.generators module. Feel free to check it to find more generators.

Important

flow.index will send the data to the /index endpoint. However, both of the added Executors do not have an /index endpoint. In fact, MyTransformer and MyIndexer only expose endpoints /foo and /bar respectively:

  1. class MyTransformer(Executor):
  2. @requests(on='/foo')
  3. def foo(self, **kwargs):
  4. print(f'foo is doing cool stuff: {kwargs}')
  5. class MyIndexer(Executor):
  6. @requests(on='/bar')
  7. def bar(self, **kwargs):
  8. print(f'bar is doing cool stuff: {kwargs}')

This simply means that no endpoint will be triggered by flow.index. Besides, our Executors are dummy and still do not have logic to index data. Later, we will modify Executors so that calling flow.index does indeed store the dataset.

Create Flow

Let’s put the Executors and the Flow together and re-organize our code a little bit. First, we should import everything we need:

  1. import os
  2. import webbrowser
  3. from pathlib import Path
  4. from jina import Flow, Executor, requests
  5. from jina.logging.predefined import default_logger
  6. from jina.types.document.generators import from_csv

Then we should have our main and a tutorial function that contains all the code that we’ve done so far. tutorial accepts one parameter that we’ll need later: port_expose (the port used to expose our Flow)

  1. def tutorial(port_expose):
  2. class MyTransformer(Executor):
  3. @requests(on='/foo')
  4. def foo(self, **kwargs):
  5. print(f'foo is doing cool stuff: {kwargs}')
  6. class MyIndexer(Executor):
  7. @requests(on='/bar')
  8. def bar(self, **kwargs):
  9. print(f'bar is doing cool stuff: {kwargs}')
  10. flow = (
  11. Flow()
  12. .add(name='MyTransformer', uses=MyTransformer)
  13. .add(name='MyIndexer', uses=MyIndexer)
  14. )
  15. with flow, open('dataset.csv') as fp:
  16. flow.index(from_csv(fp, field_resolver={'question': 'text'}))
  17. if __name__ == '__main__':
  18. tutorial(8080)

If you run this, it should finish without errors. You won’t see much yet because we are not showing anything after we index.

To actually see something we need to specify how we will display it. For our tutorial we will do so in our browser. After indexing, we will open a web browser to serve the static html files. We also need to configure and serve our Flow on a specific port with the HTTP protocol so that the web browser can make requests to the Flow. So, we’ll use the parameter port_expose to configure the Flow and set the protocol to HTTP. Modify the function tutorial like so:

  1. def tutorial(port_expose):
  2. class MyTransformer(Executor):
  3. @requests(on='/foo')
  4. def foo(self, **kwargs):
  5. print(f'foo is doing cool stuff: {kwargs}')
  6. class MyIndexer(Executor):
  7. @requests(on='/bar')
  8. def bar(self, **kwargs):
  9. print(f'bar is doing cool stuff: {kwargs}')
  10. flow = (
  11. Flow(cors=True)
  12. .add(name='MyTransformer', uses=MyTransformer)
  13. .add(name='MyIndexer', uses=MyIndexer)
  14. )
  15. with flow, open('dataset.csv') as fp:
  16. flow.index(from_csv(fp, field_resolver={'question': 'text'}))
  17. # switches the serving protocol to HTTP at runtime
  18. flow.protocol = 'http'
  19. flow.port_expose = port_expose
  20. url_html_path = 'file://' + os.path.abspath(
  21. os.path.join(
  22. os.path.dirname(os.path.realpath(__file__)), 'static/index.html'
  23. )
  24. )
  25. try:
  26. webbrowser.open(url_html_path, new=2)
  27. except:
  28. pass # intentional pass, browser support isn't cross-platform
  29. finally:
  30. default_logger.success(
  31. f'You should see a demo page opened in your browser, '
  32. f'if not, you may open {url_html_path} manually'
  33. )
  34. flow.block()

See Also

For more information on what the Flow is doing, and how to serve the Flow with f.block() and configure the protocol, check the Flow fundamentals section.

Important

Since we want to call our Flow from the browser, it’s important to enable Cross-Origin Resource Sharing with Flow(cors=True)

Ok, so it seems that we have plenty of work done already. If you run this you will see a new tab open in your browser, and there you will have a text box ready for you to input some text. However, if you try to enter anything you won’t get any results. This is because we are using dummy Executors. Our MyTransformer and MyIndexer aren’t actually doing anything. So far they only print a line when they are called. So we need real Executors.

Create Executors

We will be creating our Executors in a separate file: my_executors.py.

Sentence Transformer

First, let’s import the following:

  1. from typing import Dict
  2. from jina import Executor, DocumentArray, requests
  3. from jina.types.arrays.memmap import DocumentArrayMemmap
  4. from sentence_transformers import SentenceTransformer

Now, let’s implement MyTransformer:

  1. class MyTransformer(Executor):
  2. """Transformer executor class """
  3. def __init__(
  4. self,
  5. pretrained_model_name_or_path: str = 'paraphrase-mpnet-base-v2',
  6. device: str = 'cpu',
  7. *args,
  8. **kwargs,
  9. ):
  10. super().__init__(*args, **kwargs)
  11. self.model = SentenceTransformer(pretrained_model_name_or_path, device=device)
  12. self.model.to(device)
  13. @requests
  14. def encode(self, docs: 'DocumentArray', *args, **kwargs):
  15. import torch
  16. with torch.no_grad():
  17. texts = docs.get_attributes("text")
  18. embeddings = self.model.encode(texts, batch_size=32)
  19. for doc, embedding in zip(docs, embeddings):
  20. doc.embedding = embedding

MyTransformer exposes only one endpoint: encode. This will be called whenever we make a request to the Flow, either on query or index. The endpoint will create embeddings for the indexed or query Documents so that they can be used to get the closed matches.

Note

Encoding is a fundamental concept in neural search. It means representing the data in a vectorial form (embeddings).

Encoding is performed through a sentence-transformers model (paraphrase-mpnet-base-v2 by default). We get the text attributes of docs in batch and then compute embeddings. Later, we set the embedding attribute of each Document.

Simple Indexer

Now, let’s implement our indexer (MyIndexer):

  1. class MyIndexer(Executor):
  2. """Simple indexer class """
  3. def __init__(self, **kwargs):
  4. super().__init__(**kwargs)
  5. self._docs = DocumentArrayMemmap(self.workspace + '/indexer')
  6. @requests(on='/index')
  7. def index(self, docs: 'DocumentArray', **kwargs):
  8. self._docs.extend(docs)
  9. @requests(on='/search')
  10. def search(self, docs: 'DocumentArray', **kwargs):
  11. """Append best matches to each document in docs
  12. :param docs: documents that are searched
  13. :param kwargs: other keyword arguments
  14. """
  15. docs.match(self._docs, metric='cosine', normalization=(1, 0), limit=1)

MyIndexer exposes 2 endpoints: index and search. To perform indexing, we use DocumentArrayMemmap which is a Jina data type. Indexing is a simple as adding the Documents to the DocumentArrayMemmap.

See Also

Learn more about DocumentArrayMemmap.

To perform the search operation, we use the method match which will return the top match for the query Documents using the cosine similarity.

See Also

.match is a method of both DocumentArray and DocumentArrayMemmap. Learn more about it in this section.

To import the Executors, just add this to the imports:

  1. from my_executors import MyTransformer, MyIndexer

Put all together

Your app.py should now look like this:

  1. import os
  2. import webbrowser
  3. from pathlib import Path
  4. from jina import Flow, Executor
  5. from jina.logging.predefined import default_logger
  6. from jina.types.document.generators import from_csv
  7. from my_executors import MyTransformer, MyIndexer
  8. def tutorial(port_expose):
  9. flow = (
  10. Flow(cors=True)
  11. .add(name='MyTransformer', uses=MyTransformer)
  12. .add(name='MyIndexer', uses=MyIndexer)
  13. )
  14. with flow, open('dataset.csv') as fp:
  15. flow.index(from_csv(fp, field_resolver={'question': 'text'}))
  16. # switch to REST gateway at runtime
  17. flow.protocol = 'http'
  18. flow.port_expose = port_expose
  19. url_html_path = 'file://' + os.path.abspath(
  20. os.path.join(
  21. os.path.dirname(os.path.realpath(__file__)), 'static/index.html'
  22. )
  23. )
  24. try:
  25. webbrowser.open(url_html_path, new=2)
  26. except:
  27. pass # intentional pass, browser support isn't cross-platform
  28. finally:
  29. default_logger.success(
  30. f'You should see a demo page opened in your browser, '
  31. f'if not, you may open {url_html_path} manually'
  32. )
  33. flow.block()
  34. if __name__ == '__main__':
  35. tutorial(8080)

And your directory should be:

  1. .
  2. └── tutorial
  3. ├── app.py
  4. ├── my_executors.py
  5. ├── static/
  6. ├── our_flow.svg #This will be here if you used the .plot() function
  7. └── dataset.csv

And we are done! If you followed all the steps, now you should have something like this in your browser:

../../_images/results.png