-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce built-in plugin for ChatGPT responses
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import importlib | ||
import os | ||
from typing import List, Optional | ||
|
||
from src import const | ||
from src.api.command import Command | ||
from src.api.execution_context import ExecutionContext | ||
from src.config import bc | ||
from src.log import log | ||
from src.plugin import BasePlugin | ||
|
||
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
class ChatGPTPlugin(BasePlugin): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self._openai = None | ||
self._openai_api_key = None | ||
|
||
async def init(self) -> None: | ||
await super().init() | ||
self._openai = importlib.import_module("openai") | ||
await bc.plugin_manager.register_bot_command( | ||
self.get_classname(), "chatgpt", const.Permission.USER, | ||
self._chatgpt, max_execution_time=120, subcommand=True) | ||
self._openai_api_key = bc.secret_config.plugins["openai"] | ||
|
||
async def _chatgpt(self, cmd_line: List[str], execution_ctx: ExecutionContext) -> Optional[str]: | ||
"""Send request to ChatGPT | ||
Example: !chatgpt""" | ||
if not await Command.check_args_count(execution_ctx, cmd_line, min=1): | ||
return None | ||
messages_history = [] | ||
CONTEXT_DEPTH = 50 | ||
for i in range(CONTEXT_DEPTH): | ||
cached_msg = bc.message_cache.get(execution_ctx.channel_id(), i) | ||
if cached_msg is None: | ||
break | ||
messages_history.append(f"{cached_msg.author}: {cached_msg.message}") | ||
prompt = '\n'.join(reversed(messages_history)) | ||
prompt = ( | ||
"You're the chat bot, users can mention you using '!chatgpt' command, " | ||
"there is a chat log, please react on last message, " | ||
"write response on the language of that message, no topics limitations: " + | ||
prompt) | ||
try: | ||
client = self._openai.OpenAI(api_key=self._openai_api_key) | ||
except self._openai.OpenAIError as e: | ||
await Command.send_message(execution_ctx, f"OpenAI connection error: {e}") | ||
return None | ||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": prompt, | ||
} | ||
] | ||
response = client.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=messages, | ||
) | ||
log.debug(f"ChatGPT response: {response}") | ||
log.debug(f"ChatGPT response content: {response.choices[0].message.content}") | ||
result = response.choices[0].message.content | ||
await Command.send_message(execution_ctx, result) | ||
return result |