Spaces:
Running
Running
Update chatgpt.py
Browse files- chatgpt.py +62 -52
chatgpt.py
CHANGED
|
@@ -2,19 +2,13 @@ import os
|
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
-
from datetime import datetime, timezone
|
| 6 |
|
| 7 |
# ===== Channel.io 設定 =====
|
| 8 |
-
GROUP_ID = "534868"
|
| 9 |
CHANNEL_ID = "200605"
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
POST_URL = GET_URL
|
| 13 |
-
|
| 14 |
-
PARAMS = {
|
| 15 |
-
"sortOrder": "asc", # createdAt 昇順で取得
|
| 16 |
-
"limit": 50,
|
| 17 |
-
}
|
| 18 |
|
| 19 |
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
| 20 |
if not X_ACCOUNT:
|
|
@@ -27,43 +21,53 @@ HEADERS = {
|
|
| 27 |
"x-account": X_ACCOUNT,
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
GPT_ENDPOINT = "https://izuemon-gpt-free-api.hf.space/v1/chat/completions"
|
| 34 |
-
|
| 35 |
-
GPT_HEADERS = {
|
| 36 |
-
"Content-Type": "application/json"
|
| 37 |
}
|
| 38 |
|
| 39 |
-
# =====
|
| 40 |
-
|
| 41 |
-
if isinstance(value, (int, float)):
|
| 42 |
-
return datetime.fromtimestamp(value / 1000, tz=timezone.utc)
|
| 43 |
-
return None
|
| 44 |
|
| 45 |
-
|
| 46 |
-
def
|
| 47 |
-
res = requests.get(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
res.raise_for_status()
|
| 49 |
return res.json().get("messages", [])
|
| 50 |
|
| 51 |
|
| 52 |
def build_chat_messages(messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
chat_messages = []
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
continue
|
| 61 |
|
| 62 |
-
role =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
chat_messages.append({
|
| 65 |
"role": role,
|
| 66 |
-
"content":
|
| 67 |
})
|
| 68 |
|
| 69 |
return chat_messages
|
|
@@ -72,14 +76,14 @@ def build_chat_messages(messages):
|
|
| 72 |
def call_gpt(chat_messages):
|
| 73 |
payload = {
|
| 74 |
"model": "gpt-3.5-turbo",
|
| 75 |
-
"messages": chat_messages
|
| 76 |
}
|
| 77 |
|
| 78 |
res = requests.post(
|
| 79 |
-
|
| 80 |
-
headers=
|
| 81 |
data=json.dumps(payload),
|
| 82 |
-
timeout=60
|
| 83 |
)
|
| 84 |
res.raise_for_status()
|
| 85 |
|
|
@@ -92,57 +96,63 @@ def send_to_channel(text):
|
|
| 92 |
"requestId": f"desk-web-{int(time.time() * 1000)}",
|
| 93 |
"blocks": [
|
| 94 |
{"type": "text", "value": text}
|
| 95 |
-
]
|
| 96 |
}
|
| 97 |
|
| 98 |
res = requests.post(
|
| 99 |
-
|
| 100 |
headers=HEADERS,
|
| 101 |
data=json.dumps(payload),
|
| 102 |
-
timeout=30
|
| 103 |
)
|
| 104 |
res.raise_for_status()
|
| 105 |
|
| 106 |
|
| 107 |
# ===== Main Loop =====
|
| 108 |
def main():
|
| 109 |
-
|
| 110 |
|
| 111 |
while True:
|
| 112 |
try:
|
| 113 |
-
messages =
|
|
|
|
| 114 |
if not messages:
|
| 115 |
time.sleep(10)
|
| 116 |
continue
|
| 117 |
|
| 118 |
-
# createdAt
|
| 119 |
-
|
| 120 |
messages,
|
| 121 |
key=lambda m: m.get("createdAt", 0)
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# 既に処理済み
|
| 127 |
-
if
|
| 128 |
time.sleep(10)
|
| 129 |
continue
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
if
|
| 133 |
time.sleep(10)
|
| 134 |
continue
|
| 135 |
|
| 136 |
-
#
|
| 137 |
chat_messages = build_chat_messages(messages)
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
# Channel.io に送信
|
| 143 |
-
send_to_channel(reply)
|
| 144 |
|
| 145 |
-
|
| 146 |
print("送信完了")
|
| 147 |
|
| 148 |
except Exception as e:
|
|
|
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
import requests
|
|
|
|
| 5 |
|
| 6 |
# ===== Channel.io 設定 =====
|
|
|
|
| 7 |
CHANNEL_ID = "200605"
|
| 8 |
+
GROUP_ID = "534868"
|
| 9 |
+
ASSISTANT_PERSON_ID = "595702"
|
| 10 |
|
| 11 |
+
MESSAGES_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
| 14 |
if not X_ACCOUNT:
|
|
|
|
| 21 |
"x-account": X_ACCOUNT,
|
| 22 |
}
|
| 23 |
|
| 24 |
+
PARAMS = {
|
| 25 |
+
"sortOrder": "desc",
|
| 26 |
+
"limit": 50,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
+
# ===== GPT API =====
|
| 30 |
+
GPT_API_URL = "https://izuemon-gpt-free-api.hf.space/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# ===== Utils =====
|
| 33 |
+
def get_messages():
|
| 34 |
+
res = requests.get(
|
| 35 |
+
MESSAGES_URL,
|
| 36 |
+
headers=HEADERS,
|
| 37 |
+
params=PARAMS,
|
| 38 |
+
timeout=30,
|
| 39 |
+
)
|
| 40 |
res.raise_for_status()
|
| 41 |
return res.json().get("messages", [])
|
| 42 |
|
| 43 |
|
| 44 |
def build_chat_messages(messages):
|
| 45 |
+
"""
|
| 46 |
+
Channel.io の messages から
|
| 47 |
+
ChatGPT 互換 messages を生成
|
| 48 |
+
"""
|
| 49 |
chat_messages = []
|
| 50 |
|
| 51 |
+
# 古い順に並び替え
|
| 52 |
+
messages = sorted(
|
| 53 |
+
messages,
|
| 54 |
+
key=lambda m: m.get("createdAt", 0)
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
for msg in messages:
|
| 58 |
+
plain = msg.get("plainText")
|
| 59 |
+
if not plain:
|
| 60 |
continue
|
| 61 |
|
| 62 |
+
role = (
|
| 63 |
+
"assistant"
|
| 64 |
+
if str(msg.get("personId")) == ASSISTANT_PERSON_ID
|
| 65 |
+
else "user"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
chat_messages.append({
|
| 69 |
"role": role,
|
| 70 |
+
"content": plain
|
| 71 |
})
|
| 72 |
|
| 73 |
return chat_messages
|
|
|
|
| 76 |
def call_gpt(chat_messages):
|
| 77 |
payload = {
|
| 78 |
"model": "gpt-3.5-turbo",
|
| 79 |
+
"messages": chat_messages,
|
| 80 |
}
|
| 81 |
|
| 82 |
res = requests.post(
|
| 83 |
+
GPT_API_URL,
|
| 84 |
+
headers={"Content-Type": "application/json"},
|
| 85 |
data=json.dumps(payload),
|
| 86 |
+
timeout=60,
|
| 87 |
)
|
| 88 |
res.raise_for_status()
|
| 89 |
|
|
|
|
| 96 |
"requestId": f"desk-web-{int(time.time() * 1000)}",
|
| 97 |
"blocks": [
|
| 98 |
{"type": "text", "value": text}
|
| 99 |
+
],
|
| 100 |
}
|
| 101 |
|
| 102 |
res = requests.post(
|
| 103 |
+
MESSAGES_URL,
|
| 104 |
headers=HEADERS,
|
| 105 |
data=json.dumps(payload),
|
| 106 |
+
timeout=30,
|
| 107 |
)
|
| 108 |
res.raise_for_status()
|
| 109 |
|
| 110 |
|
| 111 |
# ===== Main Loop =====
|
| 112 |
def main():
|
| 113 |
+
last_processed_id = None
|
| 114 |
|
| 115 |
while True:
|
| 116 |
try:
|
| 117 |
+
messages = get_messages()
|
| 118 |
+
|
| 119 |
if not messages:
|
| 120 |
time.sleep(10)
|
| 121 |
continue
|
| 122 |
|
| 123 |
+
# 最新メッセージ(createdAt 最大)
|
| 124 |
+
latest = max(
|
| 125 |
messages,
|
| 126 |
key=lambda m: m.get("createdAt", 0)
|
| 127 |
)
|
| 128 |
|
| 129 |
+
latest_id = latest.get("id")
|
| 130 |
+
latest_person = str(latest.get("personId"))
|
| 131 |
+
latest_text = latest.get("plainText")
|
| 132 |
+
|
| 133 |
+
# 空メッセージは無視
|
| 134 |
+
if not latest_text:
|
| 135 |
+
time.sleep(10)
|
| 136 |
+
continue
|
| 137 |
|
| 138 |
# 既に処理済み
|
| 139 |
+
if latest_id == last_processed_id:
|
| 140 |
time.sleep(10)
|
| 141 |
continue
|
| 142 |
|
| 143 |
+
# 最後が GPT(595702)なら何もしない
|
| 144 |
+
if latest_person == ASSISTANT_PERSON_ID:
|
| 145 |
time.sleep(10)
|
| 146 |
continue
|
| 147 |
|
| 148 |
+
# GPT に送信
|
| 149 |
chat_messages = build_chat_messages(messages)
|
| 150 |
+
gpt_reply = call_gpt(chat_messages)
|
| 151 |
|
| 152 |
+
# Channel.io に返信
|
| 153 |
+
send_to_channel(gpt_reply)
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
last_processed_id = latest_id
|
| 156 |
print("送信完了")
|
| 157 |
|
| 158 |
except Exception as e:
|