Skip to content

Commit 76ed418

Browse files
authored
Merge pull request #4552 from dev-hato/add_quota_message
OpenAI: 課金不足でAPIを実行できない場合のエラーメッセージ追加
2 parents cbaa5af + e25037e commit 76ed418

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

library/chat_gpt.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
from typing import Optional
22

3-
from openai import OpenAI
3+
from openai import OpenAI, RateLimitError
44

55
import slackbot_settings as conf
66

77
client = OpenAI(api_key=conf.OPENAI_API_KEY)
88

99

1010
def chat_gpt(message: str) -> Optional[str]:
11-
result = client.chat.completions.create(
12-
model="gpt-3.5-turbo",
13-
messages=[
14-
{
15-
"role": "system",
16-
"content": "あなたは鳩のbotです。自然な感じで「鳩は唐揚げ!」という文章を混ぜて発言してください。",
17-
},
18-
{"role": "user", "content": message},
19-
],
20-
)
11+
try:
12+
result = client.chat.completions.create(
13+
model="gpt-3.5-turbo",
14+
messages=[
15+
{
16+
"role": "system",
17+
"content": "あなたは鳩のbotです。自然な感じで「鳩は唐揚げ!」という文章を混ぜて発言してください。",
18+
},
19+
{"role": "user", "content": message},
20+
],
21+
)
22+
except RateLimitError as e:
23+
if e.code == "insufficient_quota":
24+
return "栄養が足りなくて頭がうまく働かないっぽ......。このコマンドを使いたい場合は飼い主に相談してくれっぽ。"
25+
else:
26+
raise e
27+
2128
return result.choices[0].message.content
2229

2330

plugins/hato.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import requests
1717
from git import Repo
1818
from git.exc import GitCommandNotFound, InvalidGitRepositoryError
19+
from openai import RateLimitError
1920

2021
import slackbot_settings as conf
2122
from library.chat_gpt import chat_gpt, image_create
@@ -486,8 +487,13 @@ def image_generate(client: BaseClient, message: str):
486487
"""
487488
画像生成を行う
488489
"""
489-
490-
url = image_create(message=message)
490+
try:
491+
url = image_create(message=message)
492+
except RateLimitError as e:
493+
if e.code == "insufficient_quota":
494+
return "栄養が足りなくて頭がうまく働かないっぽ......。このコマンドを使いたい場合は飼い主に相談してくれっぽ。"
495+
else:
496+
raise e
491497

492498
if url is None:
493499
return "画像を生成できなかったっぽ......"

0 commit comments

Comments
 (0)