ChatworkのルームにAIを住まわせる

2024.05.28
Chatwork APIを使って、AIチャットボットを書いてみた。
必要な物をインストール
$ sudo apt install curl; curl -fsSL https://ollama.com/install.sh | sh
$ pip install ollama
スクリプトはこちら
chatwork_ai.py:
#!/bin/python3
import requests
import time
import ollama
url = "https://api.chatwork.com/v2/rooms/ルームID/messages"
headers = {
"accept": "application/json",
"x-chatworktoken": "APIトークン"
}
'''
Chatworkからメッセージを取得
*APIの仕様により自動的に差分が取得される
'''
def chatwork_get_messages():
response = requests.get(url, headers=headers)
if response.status_code == 200:
messages = response.json()
# "##"から始まるメッセージをフィルタリング
request_messages = [msg for msg in messages if msg['body'].startswith("##")]
return request_messages
else:
print(f"待機中: {response.status_code}")
return []
'''
AIに推論させる
'''
def ai_thinking(prompt):
response = ollama.chat(
model="hawkclaws/datapilot-arrowpro-7b-kujira",
#model="codellama",
messages=[
{
"role": "user",
"content": f"{prompt} ##出力:",
},
],
)
return response["message"]["content"]
'''
Chatworkへメッセージを送る
'''
def chatwork_send_message(send_message_txt):
data = {'body': send_message_txt}
response = requests.post(url, headers=headers, data=data)
return response.status_code
'''
繰り返し処理
'''
while True:
chatwork_messages = chatwork_get_messages()
for msg in chatwork_messages:
#受信メッセージ確認
account_id = msg['account']['account_id']
body = msg['body']
print(f"Account ID: {account_id}, Message: {body}")
#AI推論
ai_message = ai_thinking(body)
# Chatworkにメッセージを送信
message_body = f"[To:{account_id}]{ai_message}"
send_message = chatwork_send_message(message_body)
print(message_body)
print(f"送信結果:{send_message}")
# 2秒間待機 ChatworkAPIは5分間に300リクエストまで
time.sleep(2)
実行している様子
$ chmod +x chatwork_ai.py
$ ./chatwork_ai.py
ターミナル画面:

Chatwork画面:

やっていることは次の通り:
- Chatworkのルームのメッセージを2秒毎に取得
- 「##」から始まるメッセージがあった場合プロンプトとして拾う
- 拾ったプロンプトを一つずつOllama×KUJIRAで推論をしていく
- 結果を投稿主にTo:で返信する
複数のユーザーからプロンプトが投げられた場合、キューに追加され、順番に推論していく。
ちゃんとプロンプトを送った人にTo:するので混乱しづらい。
めちゃくちゃ単純だけどちゃんと機能します。Chatworkにログインできる端末なら何でも使えるし、複数人で同じローカルLLMが使える。
RAGを実装すればめっちゃ強いAIバイトくんができそうです。
この記事のコメント一覧(4)
yttrium chloride 2025-02-09 06:09:38
BBI Bis(benzene sulphonyl)imidePOPDH 3-Prop-2-ynoxypropane-1,2-diol
nano titanium dioxide dispersion
BCES (Hydroxypropyl) Butyne Diether Disulfonate, Sodium Salt
europium chloride
copper metal powder
BMP Butynediol Propoxylate
reps lucy sneaker shoes LY-adidas Handball Spezial "IMCF" sneakers
ATP S-Carboxyethylisothiuronium Chloride
www.fines.co.jp
rep lucysneaker LY-adidas Adizero Prime X 2.0 sneakers
reps lucy sneaker shoes LY-adidas AE1 "Coral" sneakers
rep lucysneaker LY-adidas NMD R1 "Triple Black" sneakers
cheap Lucy sneaker LY-adidas x Teenage Mutant Ninja Turtles Superstar sneakers
zirconia blocks dental lab
yttrium chloride
Graphite Gasket Reinforced with Metal mesh 2025-02-06 15:00:20
24/7 Fresh Pizza Vending Machinerep tbkicks08 TBkick-Jordan 4 Retro Toro Bravo 308497-603
www.bilu.com.pl
reps tbkickru TBkick-Air Jordan 4 Retro Fire Red (2020) DC7770-160
Graphite Gasket Reinforced with Flat metal
Pizza Kiosk Machine
Kammprofile gasket with integral outer ring
Electric Pizza Machine
Basic style kammprofile gaskets
CG spiral wound gasket for thermal exchange
Outside Street Vending Machine
reps tbkickru TBkick-Jordan 4 Retro SB DR5415-150
cheap tbkicks TBkick-Air Jordan 4 OG White Cement 840606-192 (nike)
rep tbkicks08 TBkick-Air Jordan 4 Retro Lightning CT8527-700
Rotary Knife Vegetable Cutter
Graphite Gasket Reinforced with Metal mesh
drover sointeru 2025-01-02 10:57:43
I like this site very much, Its a really nice place to read and get info . "God cannot alter the past, but historians can." by Samuel Butler.Marcella I 2024-08-10 19:15:19
Very interesting info!Perfect just what I was searching for!Money from blog