Writing the message queue chatbot

The chatbot server we want to create is essentially a combination of the three previous examples. Open up Chapter_4_5.py and follow the next exercise:

  1. The complete server code as follows:
import pika
from deeppavlov.skills.pattern_matching_skill import PatternMatchingSkill
from deeppavlov.agents.default_agent.default_agent import DefaultAgent
from deeppavlov.agents.processors.highest_confidence_selector import HighestConfidenceSelector

hello = PatternMatchingSkill(responses=['Hello world!'], patterns=["hi", "hello", "good day"])
bye = PatternMatchingSkill(['Goodbye world!', 'See you around'], patterns=["bye", "chao", "see you"])
fallback = PatternMatchingSkill(["I don't understand, sorry", 'I can say "Hello world!"'])

HelloBot = DefaultAgent([hello, bye, fallback], skills_selector=HighestConfidenceSelector())

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channelin = connection.channel()
channelin.exchange_declare(exchange='chat', exchange_type='direct', durable=True)
channelin.queue_bind(exchange='chat', queue='chatin')

channelout = connection.channel()
channelout.exchange_declare(exchange='chat', durable=True)

def callback(ch, method, properties, body):
global HelloBot, channelout
response = HelloBot([str(body)])[0].encode()
print(body,response)
channelout.basic_publish(exchange='chat',
routing_key='chatout',
body=response)
print(" [x] Sent response %r" % response)

channelin.basic_consume(callback,
queue='chatin',
no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channelin.start_consuming()

  1. We essentially have a complete working Hello World chatbot server in fewer than 25 lines of code. Of course, the functionality is still limited, but by now you can certainly understand how to add other pattern-matching skills to the bot.
    The important thing to note here is that we are consuming from a queue called chatin and publishing to a queue called chatout. These queues are now wrapped in an exchange called chat. You can think of an exchange as a routing service. Exchanges provide for additional functionality around queues, and the great thing is that they are optional. For use, though, we want to use exchanges, because they provide us with better global control of our services. There are four types of exchanges used in RabbitMQ and they are summarized here:
    • Direct: Messages are sent directly to the queue marked in the message transmission.
    • Fanout: Duplicate the message to all queues wrapped by the exchange. This is great when you want to add logging or historical archiving.
    • Topic: This allows you to send messages to queues identified by matching the message queue. For instance, you could send a message to the queue chat and any queue wrapped in the same exchange containing the word chat receives the message. The topic exchange allows you to group like messages.
    • Headers: This works similar to the topic exchange but instead filters based on the headers in the message itself. This is a great exchange to use for dynamic routing of messages with the appropriate headers.
  2. Run the Chapter_4_5.py server example and keep it running.
  3. Next, open the Chapter_4_6.py file and look at the code shown:
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channelin = connection.channel()

channelin.exchange_declare(exchange='chat')

chat = 'boo'

channelin.basic_publish(exchange='chat',
routing_key='chatin',
body=chat)
print(" [x] Sent '{0}'".format(chat))
connection.close()
  1. The preceding code is just a sample client we can use to test the chatbot server. Note how the message variable chat is set to 'boo'. When you run the code, check the output window of the chatbot server; this is the Chapter_4_5.py file we ran earlier. You should see a response message logged in the window that is appropriate to the chat message we just sent.

At this point, you could write a full chat client that could communicate with our chatbot in Python. However, we want to connect our bot up to Unity and see how we can use our bot as a microservice in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset