LoginSignup
29
33

More than 5 years have passed since last update.

Python でユーザーローカルの人工知能 API を遊んでみた話

Last updated at Posted at 2016-08-17

User Local が提供している人工知能 API の事前応募に応募していて、6 月くらいには API キーをもらっていたのですが、なかなか手を出せていませんでした。僕は Web API に慣れ親しんでいないので、簡単なところからの投稿になります。

とりあえず、今回は Python から API を叩いて、どんな感じか確かめてみます。

Python で人工知能 API を叩く

GET リクエストをすればいいらしいので、以下のように Python コードを書いてやりました。apikey_pram はご自身のものを指定してください。実は、API Key は "sample" にすることで 1 時間に 10 回までは遊ぶことができます。

#! /usr/bin/env python -O
# -*- encode: utf-8 -*-
import sys
import json
import urllib.request
import urllib.parse

message_param = sys.argv[1]
apikey_param = "plz set your api key"
url = "https://chatbot-api.userlocal.jp/api/chat?"

params = {
    "message": message_param,
    "key": apikey_param
}

url += urllib.parse.urlencode(params)
if __debug__:
    print("access to {0}".format(url))

with urllib.request.urlopen(url) as res:
    html = res.read().decode("utf-8")
    root = json.loads(html)
    print("AI: {0}".format(root["result"]))

これを sample_userlocal.py という名前で保存しました。

結果

人工知能と名付けられているので、ちょっと意地悪ですが、こんな風に聞いてみました。

~ $ ./sample_userlocal.py 人工知能って何?
AI: わたしのこと?笑
~ $ ./sample_userlocal.py まじで?
AI: まじ
~ $ ./sample_userlocal.py うおー、すげー!
AI: ぶぎー((´^ω^))

ぶぎーーーー((´^ω^))!!!「なんかいい感じじゃねーか笑」 という気分に。もう少し遊んでみます。

~ $ ./sample_userlocal.py おなかへった
AI: おかえりーですー!
~ $ ./sample_userlocal.py ただいま
AI: おつかれ
~ $ ./sample_userlocal.py ありがとう
AI: うむ

うーん、「おなかへった」で「おかえり」っていうのはなくはないけど... これだけだと不自然な気がしますが、そのまま無理やり会話してみるとなんとなくの会話はできます。最後に、「人工知能って何」をもう一回聞いてみて終わりにしました。

~ $ ./sample_userlocal.py 人工知能って何?
AI: 人工知能というより人工無脳かな

えええええええええ笑 まじかよ。(君が言っちゃダメな奴)

まとめ

公式も言っていますが、学習中の API なため、ちょこちょこおかしなことを言ってきます。まあ結構面白いので、次回はこやつを研究室 Slack の bot にしてやろうと思います(小並感)

簡単ですが、備忘録までに。

29
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
33