This article is participating in Python Theme Month. See the link to the event for more details

Everybody is good! I am Python xiaxiabai, Tanabata and double 叒 Yi, here to take you to super fast AI ChatBot tutorial,

Real hand to hand teach you to do a chat robot with AI write love words,

(Created by Paddlehub + Python Wechaty)

The construction process will encounter many, many deep pits! But they waded through it for everyone!

See next chapter for more details

Words don’t say first on the effect: open whole

1. Environmental support

Here run under Win7 environment

1. Python version: v>=3.8

PIP version: V21.1.1

3. The docker service

Second, specific steps

1. Apply for freetoken

1.1 Padlocal_token

  1. Wechaty 官网

  2. Python-wechaty-for-padlocal-puppet-service

  3. TOKEN Application method: > Padlocal: 👉🏻 Get Free Padlocal TOKEN 👈🏻

1.2 wechaty_token

Wechaty_token is a UUID to avoid reapplying for an address: wechaty_token

2. Set up the PadLocal Token Gateway service environment

  1. vimCreate a script
Vi wechaty_test.sh command I -- Enter the following script contentCopy the code
  1. Enter the following script

    Add a line at the beginning: #! /bin/bash

    1. WECHATY_PUPPET_PADLOCAL_TOKEN is the one applied for abovePadlocal_token
    2. SERVER_PORT External access port:9999
    3. WECHATY_TOKEN for the above applicationwechaty_token
#! /bin/bash

export WECHATY_LOG="verbose"
export WECHATY_PUPPET="wechaty-puppet-padlocal"
export WECHATY_PUPPET_PADLOCAL_TOKEN="puppet_padlocal_XXXXXX"

export WECHATY_PUPPET_SERVER_PORT="9999"
export WECHATY_TOKEN="1fe5f846-3cfb-401d-b20c-XXXXX"

docker run -ti \
 --name wechaty_puppet_service_token_gateway \
 --rm \
 -e WECHATY_LOG \
 -e WECHATY_PUPPET \
 -e WECHATY_PUPPET_PADLOCAL_TOKEN \
 -e WECHATY_PUPPET_SERVER_PORT \
 -e WECHATY_TOKEN \
 -p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT"\ wechaty/wechaty: 0.56Copy the code

Then set the sh command permission for the created script wechaty_test.sh, enter the following command & and press Enter

# Where +x is the letter x
chmod +x ./wechaty_test.sh
Copy the code

3. StartDocker

In the docker installation directory: E:\ codeTools \Docker Toolbox\start.sh, start the Docker service, successfully as shown below:

If the Docker service is not started, starting our wechaty_test.sh service will fail, as shown in the figure below:

4. Runwechaty_test.sh, the connectionwechatyservice

sh ./wechaty_test.sh
Copy the code

The following heart chart shows that we have been withwechatyThe remote server is connected,

If you have run the robot project and logged in wechat, this is the message received by wechat here we can see!

send-test-message

The sh command line displays this message:

The next step is to clone the python-wechaty-getting-started project code

3. Python Robot project

1. Clone projectspython-wechaty-getting-started

After downloading the clone project, open it with PyCharm and do a simple configuration

git clone https://github.com/wj-Mcat/python-wechaty-getting-started

cd python-wechaty-getting-started
Copy the code

2. The configurationexamples/ding-dong-bot.py

SERVICE_TOKEN Corresponds to the wechaty_token applied in the preceding section

SERVICE_ENDPOINT Corresponds to the started Docker service

# examples/ding-dong-bot.py

## Import OS package to set environment variables
import os

os.environ['WECHATY_PUPPET'] = "wechaty-puppet-service"
os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = "bb137430-27ae-xxxx-xxx-xxxxxxxx"
os.environ['WECHATY_PUPPET_SERVICE_ENDPOINT'] = "192.xxx.xx.100:9999"
Copy the code

3. Start

# Start project
python examples/ding-dong-bot.py
Copy the code

After success, there is a TWO-DIMENSIONAL code can use wechat scan code login. Login can not login several times..

Then test it with your friends: sending a test message will automatically reply dong + an IMG

To this, congratulations you into the pit.

The above is the preliminary project groundwork!

The following is the key logic that can continue to write love words robot!

Four, to the robot blessed AI write love words

1. Install paddlepaddle

Install PaddlePaddle using Python’s package manager PIP

pip install paddlepaddle
Copy the code

2. Installpaddlehub

Paddlehub, a tool component in the application scenario of Baidu Flying Pulp deep learning platform, is used here

This article uses the application scenario PaddleHub >> intelligent text generation >> love words generation model

Install paddleHub into the user directory

pip install paddlehub --user
Copy the code

At this point PyCharm reintroduces related dependencies

3. Write robot logic

Using the examples in the documentation, it’s super simple, it’s great,

# Introduce dependencies
import paddlehub as hub

# Load model
module = hub.Module(name="ernie_gen_lover_words")
Copy the code

Modifying the logic in the on_message function is described in detail

async def on_message(msg: Message) :
    """ Message Handler for the Bot """
    If you send a message by yourself, you will not return a reply
    if msg.is_self():
        return

    ## production_text = [' valentine's Day ', 'home ',' programmer Valentine's Day ']

    # generate message
    # In this case, the argument texts=[MSG. Text ()] generates a two-dimensional array of received messages as training text
    # beam_width Number of return statements set here to 1
    results = module.generate(texts=[msg.text()], use_gpu=True, beam_width=1)
    # return directly without generating the corresponding content
    if results is None:
        return
    Send message: the first item of a two-dimensional array
    await msg.say(results[0] [0])
Copy the code

And that’s it. Ha, ha, ha

4. Run

Start running: run failure can be run more times! Please be patient while this process downloads the Paddle model file package

python examples/ding-dong-bot.py
Copy the code

Effect: Come and try it with your friends,

More paddleHub [application scenarios] can also be tried by yourself!

How, is it a little cool!!

Hit the pit

If there is insufficient, doubt, welcome not stingy give instruction, criticism pointed out!

If there is some help for you, please also move the small hand to make a fortune with a key three! A little like, comment collection! Thank you. Thank you

We have waded through all kinds of potholes for you, and we will list them in detail in a future article. Stay tuned.