Improve the previously created Make LINE BOT and use the A3RT Talk API provided by Recruit to create a BOT for chatting. Had made.
Issue API KEY on A3RT site.
Check the structure of A3RT before modifying main.py created last time. Execute the following code to check the structure of A3RT and check if the AI returns the answer to "Good morning" properly. First, install pya3rt in a virtual environment,
pip install pya3rt
Try running the following in python mode in your terminal.
import pya3rt
apikey = "*******************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk("Good morning")
print(reply_message)
{'status': 0,'message': 'ok','results': [{'perplexity': 0.07743213382788067, 'reply': 'Good morning'}]}
Will be. I want to get "Good morning" in the reply, so I modified it as follows
import pya3rt
apikey = "****************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk("Good morning")
print(reply_message['results'][0]['reply'])
Then
Good morning
Then, he properly returned "Good morning" → "Good morning". Incorporate this into the previously created main.py.
First, the above description of pya3rt is made into a function as follows.
def talk_ai(word):
apikey = "****************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk(word)
return reply_message['results'][0]['reply']
Last time Modify the created main.py.
.py:main.py
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
import os
#add to
import pya3rt
app = Flask(__name__)
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
@app.route("/")
def hello_world():
return "hello world!"
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
#(Addition) talk_Pass the argument to the ai method and get the return value ai_Assign to message
ai_message = talk_ai(event.message.text)
line_bot_api.reply_message(
event.reply_token,
#TextSendMessage(text=event.message.txt))
#(Fixed) ai_Return message
TextSendMessage(text=ai_message))
#(Addition) Reply ai conversation with pya3rt
def talk_ai(word):
apikey = "****************************"
client = pya3rt.TalkClient(apikey)
reply_message = client.talk(word)
return reply_message['results'][0]['reply']
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
If you can fix it, update requirements.txt and
pip freeze > requirements.txt
After that, deploy it to Heroku and you're done.
Recommended Posts