Spaces:
Running
Running
Update chatgpt.py
Browse files- chatgpt.py +26 -20
chatgpt.py
CHANGED
|
@@ -7,7 +7,7 @@ import requests
|
|
| 7 |
GROUP_ID = "534868"
|
| 8 |
CHANNEL_ID = "200605"
|
| 9 |
|
| 10 |
-
GET_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages
|
| 11 |
POST_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"
|
| 12 |
|
| 13 |
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
|
@@ -22,8 +22,9 @@ HEADERS = {
|
|
| 22 |
}
|
| 23 |
|
| 24 |
PARAMS = {
|
| 25 |
-
"sortOrder": "asc",
|
| 26 |
"limit": 50,
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
ASSISTANT_PERSON_ID = "595702"
|
|
@@ -37,10 +38,10 @@ def fetch_channel_messages():
|
|
| 37 |
res.raise_for_status()
|
| 38 |
return res.json().get("messages", [])
|
| 39 |
|
| 40 |
-
def build_chat_messages(messages):
|
| 41 |
chat_messages = []
|
| 42 |
|
| 43 |
-
for msg in messages:
|
| 44 |
text = msg.get("plainText")
|
| 45 |
person_id = msg.get("personId")
|
| 46 |
|
|
@@ -60,6 +61,7 @@ def call_chat_api(chat_messages):
|
|
| 60 |
payload = {
|
| 61 |
"model": "gpt-3.5-turbo",
|
| 62 |
"messages": chat_messages,
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
res = requests.post(
|
|
@@ -91,48 +93,52 @@ def send_to_channel(text):
|
|
| 91 |
|
| 92 |
# ===== Main =====
|
| 93 |
def main():
|
| 94 |
-
|
| 95 |
|
| 96 |
while True:
|
| 97 |
try:
|
| 98 |
messages = fetch_channel_messages()
|
| 99 |
-
|
| 100 |
if not messages:
|
| 101 |
-
time.sleep(
|
| 102 |
continue
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
latest =
|
| 106 |
-
|
| 107 |
latest_person_id = latest.get("personId")
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
if
|
| 111 |
-
time.sleep(
|
| 112 |
continue
|
| 113 |
|
| 114 |
-
#
|
| 115 |
if latest_person_id == ASSISTANT_PERSON_ID:
|
| 116 |
-
|
| 117 |
-
time.sleep(
|
| 118 |
continue
|
| 119 |
|
| 120 |
chat_messages = build_chat_messages(messages)
|
| 121 |
|
| 122 |
if not chat_messages:
|
| 123 |
-
|
| 124 |
continue
|
| 125 |
|
| 126 |
reply = call_chat_api(chat_messages)
|
| 127 |
send_to_channel(reply)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
print("エラー:", e)
|
| 134 |
|
| 135 |
-
time.sleep(
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
main()
|
|
|
|
| 7 |
GROUP_ID = "534868"
|
| 8 |
CHANNEL_ID = "200605"
|
| 9 |
|
| 10 |
+
GET_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"
|
| 11 |
POST_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"
|
| 12 |
|
| 13 |
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
PARAMS = {
|
| 25 |
+
"sortOrder": "asc", # 昇順で取得
|
| 26 |
"limit": 50,
|
| 27 |
+
"logFolded": "false"
|
| 28 |
}
|
| 29 |
|
| 30 |
ASSISTANT_PERSON_ID = "595702"
|
|
|
|
| 38 |
res.raise_for_status()
|
| 39 |
return res.json().get("messages", [])
|
| 40 |
|
| 41 |
+
def build_chat_messages(messages, limit=10):
|
| 42 |
chat_messages = []
|
| 43 |
|
| 44 |
+
for msg in messages[-limit:]:
|
| 45 |
text = msg.get("plainText")
|
| 46 |
person_id = msg.get("personId")
|
| 47 |
|
|
|
|
| 61 |
payload = {
|
| 62 |
"model": "gpt-3.5-turbo",
|
| 63 |
"messages": chat_messages,
|
| 64 |
+
"temperature": 0.7,
|
| 65 |
}
|
| 66 |
|
| 67 |
res = requests.post(
|
|
|
|
| 93 |
|
| 94 |
# ===== Main =====
|
| 95 |
def main():
|
| 96 |
+
processed_ids = set()
|
| 97 |
|
| 98 |
while True:
|
| 99 |
try:
|
| 100 |
messages = fetch_channel_messages()
|
|
|
|
| 101 |
if not messages:
|
| 102 |
+
time.sleep(2)
|
| 103 |
continue
|
| 104 |
|
| 105 |
+
# asc なので最後が最新
|
| 106 |
+
latest = messages[-1]
|
| 107 |
+
latest_id = latest.get("id")
|
| 108 |
latest_person_id = latest.get("personId")
|
| 109 |
|
| 110 |
+
# すでに処理済み
|
| 111 |
+
if latest_id in processed_ids:
|
| 112 |
+
time.sleep(2)
|
| 113 |
continue
|
| 114 |
|
| 115 |
+
# 自分の発言には反応しない
|
| 116 |
if latest_person_id == ASSISTANT_PERSON_ID:
|
| 117 |
+
processed_ids.add(latest_id)
|
| 118 |
+
time.sleep(2)
|
| 119 |
continue
|
| 120 |
|
| 121 |
chat_messages = build_chat_messages(messages)
|
| 122 |
|
| 123 |
if not chat_messages:
|
| 124 |
+
processed_ids.add(latest_id)
|
| 125 |
continue
|
| 126 |
|
| 127 |
reply = call_chat_api(chat_messages)
|
| 128 |
send_to_channel(reply)
|
| 129 |
|
| 130 |
+
processed_ids.add(latest_id)
|
| 131 |
+
|
| 132 |
+
# メモリ肥大防止
|
| 133 |
+
if len(processed_ids) > 100:
|
| 134 |
+
processed_ids = set(list(processed_ids)[-50:])
|
| 135 |
+
|
| 136 |
+
print("返信送信完了")
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
print("エラー:", e)
|
| 140 |
|
| 141 |
+
time.sleep(2)
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
main()
|