fix: !gpt !chat API endpoint and API key validation logic
This commit is contained in:
parent
7fe0ccea8e
commit
fe7cc753c4
1 changed files with 19 additions and 19 deletions
38
src/bot.py
38
src/bot.py
|
@ -222,7 +222,10 @@ class Bot:
|
||||||
content_body = re.sub("\r\n|\r|\n", " ", raw_user_message)
|
content_body = re.sub("\r\n|\r|\n", " ", raw_user_message)
|
||||||
|
|
||||||
# !gpt command
|
# !gpt command
|
||||||
if self.openai_api_key is not None:
|
if (
|
||||||
|
self.openai_api_key is not None
|
||||||
|
or self.gpt_api_endpoint != "https://api.openai.com/v1/chat/completions"
|
||||||
|
):
|
||||||
m = self.gpt_prog.match(content_body)
|
m = self.gpt_prog.match(content_body)
|
||||||
if m:
|
if m:
|
||||||
prompt = m.group(1)
|
prompt = m.group(1)
|
||||||
|
@ -239,29 +242,26 @@ class Bot:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
|
|
||||||
if self.gpt_api_endpoint is not None:
|
# !chat command
|
||||||
# chatgpt
|
if (
|
||||||
|
self.openai_api_key is not None
|
||||||
|
or self.gpt_api_endpoint != "https://api.openai.com/v1/chat/completions"
|
||||||
|
):
|
||||||
n = self.chat_prog.match(content_body)
|
n = self.chat_prog.match(content_body)
|
||||||
if n:
|
if n:
|
||||||
prompt = n.group(1)
|
prompt = n.group(1)
|
||||||
if self.openai_api_key is not None:
|
try:
|
||||||
try:
|
asyncio.create_task(
|
||||||
asyncio.create_task(
|
self.chat(
|
||||||
self.chat(
|
room_id,
|
||||||
room_id,
|
reply_to_event_id,
|
||||||
reply_to_event_id,
|
prompt,
|
||||||
prompt,
|
sender_id,
|
||||||
sender_id,
|
raw_user_message,
|
||||||
raw_user_message,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
except Exception as e:
|
|
||||||
logger.error(e, exc_info=True)
|
|
||||||
else:
|
|
||||||
logger.warning("No API_KEY provided")
|
|
||||||
await send_room_message(
|
|
||||||
self.client, room_id, reply_message="API_KEY not provided"
|
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e, exc_info=True)
|
||||||
|
|
||||||
# lc command
|
# lc command
|
||||||
if self.lc_admin is not None:
|
if self.lc_admin is not None:
|
||||||
|
|
Loading…
Reference in a new issue