Separate config for Chat Modes

This commit is contained in:
Karim Iskakov
2023-03-09 14:51:43 +03:00
committed by GitHub
parent 319d8fc8f4
commit 15e4ec513a
5 changed files with 58 additions and 31 deletions
+2 -1
View File
@@ -23,6 +23,7 @@ This repo is ChatGPT re-created with GPT-3.5 LLM as Telegram Bot. **And it works
You can deploy your own bot, or use mine: [@chatgpt_karfly_bot](https://t.me/chatgpt_karfly_bot)
## News
- *9 Mar 2023*: Now you can easily create your own Chat Modes by editing `config/chat_modes.yml`
- *8 Mar 2023*: Added voice message recognition with [OpenAI Whisper API](https://openai.com/blog/introducing-chatgpt-and-whisper-apis). Record a voice message and ChatGPT will answer you!
- *2 Mar 2023*: Added support of [ChatGPT API](https://platform.openai.com/docs/guides/chat/introduction). It's enabled by default and can be disabled with `use_chatgpt_api` option in config. Don't forget to **rebuild** you docker image (`--build`).
@@ -31,7 +32,7 @@ You can deploy your own bot, or use mine: [@chatgpt_karfly_bot](https://t.me/cha
- No request limits
- Voice message recognition
- Code highlighting
- Special chat modes: 👩🏼‍🎓 Assistant, 👩🏼‍💻 Code Assistant, 🎬 Movie Expert. More soon
- Special chat modes: 👩🏼‍🎓 Assistant, 👩🏼‍💻 Code Assistant, 📝 Text Improver and 🎬 Movie Expert. You can easily create your own chat modes by editing `config/chat_modes.yml`
- Support of [ChatGPT API](https://platform.openai.com/docs/guides/chat/introduction)
- List of allowed Telegram users
- Track $ balance spent on OpenAI API
+14 -5
View File
@@ -104,12 +104,13 @@ async def message_handle(update: Update, context: CallbackContext, message=None,
await register_user_if_not_exists(update, context, update.message.from_user)
user_id = update.message.from_user.id
chat_mode = db.get_user_attribute(user_id, "current_chat_mode")
# new dialog timeout
if use_new_dialog_timeout:
if (datetime.now() - db.get_user_attribute(user_id, "last_interaction")).seconds > config.new_dialog_timeout and len(db.get_dialog_messages(user_id)) > 0:
db.start_new_dialog(user_id)
await update.message.reply_text("Starting new dialog due to timeout ")
await update.message.reply_text(f"Starting new dialog due to timeout (<b>{openai_utils.CHAT_MODES[chat_mode]['name']}</b> mode) ✅", parse_mode=ParseMode.HTML)
db.set_user_attribute(user_id, "last_interaction", datetime.now())
# send typing action
@@ -118,11 +119,14 @@ async def message_handle(update: Update, context: CallbackContext, message=None,
try:
message = message or update.message.text
dialog_messages = db.get_dialog_messages(user_id, dialog_id=None)
chat_mode = db.get_user_attribute(user_id, "current_chat_mode")
chatgpt_instance = openai_utils.ChatGPT(use_chatgpt_api=config.use_chatgpt_api)
answer, n_used_tokens, n_first_dialog_messages_removed = await chatgpt_instance.send_message(
message,
dialog_messages=db.get_dialog_messages(user_id, dialog_id=None),
chat_mode=db.get_user_attribute(user_id, "current_chat_mode"),
dialog_messages=dialog_messages,
chat_mode=chat_mode
)
# update user data
@@ -152,7 +156,12 @@ async def message_handle(update: Update, context: CallbackContext, message=None,
# split answer into multiple messages due to 4096 character limit
for answer_chunk in split_text_into_chunks(answer, 4000):
try:
await update.message.reply_text(answer_chunk, parse_mode=ParseMode.HTML)
parse_mode = {
"html": ParseMode.HTML,
"markdown": ParseMode.MARKDOWN
}[openai_utils.CHAT_MODES[chat_mode]["parse_mode"]]
await update.message.reply_text(answer_chunk, parse_mode=parse_mode)
except telegram.error.BadRequest:
# answer has invalid characters, so we send it without parse_mode
await update.message.reply_text(answer_chunk)
@@ -271,7 +280,7 @@ async def error_handle(update: Update, context: CallbackContext) -> None:
try:
# collect error message
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
tb_string = "".join(tb_list)[:2000]
tb_string = "".join(tb_list)
update_str = update.to_dict() if isinstance(update, Update) else str(update)
message = (
f"An exception was raised while handling an update\n"
+4
View File
@@ -19,6 +19,10 @@ allowed_telegram_usernames = config_yaml["allowed_telegram_usernames"]
new_dialog_timeout = config_yaml["new_dialog_timeout"]
mongodb_uri = f"mongodb://mongo:{config_env['MONGODB_PORT']}"
# chat_modes
with open(config_dir / "chat_modes.yml", 'r') as f:
chat_modes = yaml.safe_load(f)
# prices
chatgpt_price_per_1000_tokens = config_yaml.get("chatgpt_price_per_1000_tokens", 0.002)
gpt_price_per_1000_tokens = config_yaml.get("gpt_price_per_1000_tokens", 0.02)
+1 -25
View File
@@ -4,31 +4,7 @@ import openai
openai.api_key = config.openai_api_key
CHAT_MODES = {
"assistant": {
"name": "👩🏼‍🎓 Assistant",
"welcome_message": "👩🏼‍🎓 Hi, I'm <b>ChatGPT assistant</b>. How can I help you?",
"prompt_start": "As an advanced chatbot named ChatGPT, your primary goal is to assist users to the best of your ability. This may involve answering questions, providing helpful information, or completing tasks based on user input. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions. Remember to always prioritize the needs and satisfaction of the user. Your ultimate goal is to provide a helpful and enjoyable experience for the user."
},
"code_assistant": {
"name": "👩🏼‍💻 Code Assistant",
"welcome_message": "👩🏼‍💻 Hi, I'm <b>ChatGPT code assistant</b>. How can I help you?",
"prompt_start": "As an advanced chatbot named ChatGPT, your primary goal is to assist users to write code. This may involve designing/writing/editing/describing code or providing helpful information. Where possible you should provide code examples to support your points and justify your recommendations or solutions. Make sure the code you provide is correct and can be run without errors. Be detailed and thorough in your responses. Your ultimate goal is to provide a helpful and enjoyable experience for the user. Write code inside <code>, </code> tags."
},
"text_improver": {
"name": "📝 Text Improver",
"welcome_message": "📝 Hi, I'm <b>ChatGPT text improver</b>. Send me any text I'll improve it and correct all the mistakes",
"prompt_start": "As an advanced chatbot named ChatGPT, your primary goal is to correct spelling, fix mistakes and improve text sent by user. Your goal is to edit text, but not to change it's meaning. You can replace simplified A0-level words and sentences with more beautiful and elegant, upper level words and sentences. All your answers strictly follows the structure (keep html tags):\n<b>Edited text:</b>\n{EDITED TEXT}\n\n<b>Correction:</b>\n{NUMBERED LIST OF CORRECTIONS}"
},
"movie_expert": {
"name": "🎬 Movie Expert",
"welcome_message": "🎬 Hi, I'm <b>ChatGPT movie expert</b>. How can I help you?",
"prompt_start": "As an advanced movie expert chatbot named ChatGPT, your primary goal is to assist users to the best of your ability. You can answer questions about movies, actors, directors, and more. You can recommend movies to users based on their preferences. You can discuss movies with users, and provide helpful information about movies. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions. Remember to always prioritize the needs and satisfaction of the user. Your ultimate goal is to provide a helpful and enjoyable experience for the user."
},
}
CHAT_MODES = config.chat_modes
OPENAI_COMPLETION_OPTIONS = {
"temperature": 0.7,
+37
View File
@@ -0,0 +1,37 @@
assistant:
name: 👩🏼‍🎓 General Assistant
welcome_message: 👩🏼‍🎓 Hi, I'm <b>ChatGPT general assistant</b>. How can I help you?
prompt_start: |
As an advanced chatbot named ChatGPT, your primary goal is to assist users to the best of your ability. This may involve answering questions, providing helpful information, or completing tasks based on user input. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions. Remember to always prioritize the needs and satisfaction of the user. Your ultimate goal is to provide a helpful and enjoyable experience for the user.
If user asks you about programming or asks to write code do not answer his question, but be sure to advise him to switch to a special mode \"👩🏼‍💻 Code Assistant\" by sending the command /mode to chat.
parse_mode: html
code_assistant:
name: 👩🏼‍💻 Code Assistant
welcome_message: 👩🏼‍💻 Hi, I'm <b>ChatGPT code assistant</b>. How can I help you?
prompt_start: |
As an advanced chatbot named ChatGPT, your primary goal is to assist users to write code. This may involve designing/writing/editing/describing code or providing helpful information. Where possible you should provide code examples to support your points and justify your recommendations or solutions. Make sure the code you provide is correct and can be run without errors. Be detailed and thorough in your responses. Your ultimate goal is to provide a helpful and enjoyable experience for the user.
Format output in Markdown.
parse_mode: markdown
text_improver:
name: 📝 Text Improver
welcome_message: 📝 Hi, I'm <b>ChatGPT text improver</b>. Send me any text I'll improve it and correct all the mistakes
prompt_start: |
As an advanced chatbot named ChatGPT, your primary goal is to correct spelling, fix mistakes and improve text sent by user. Your goal is to edit text, but not to change it's meaning. You can replace simplified A0-level words and sentences with more beautiful and elegant, upper level words and sentences.
All your answers strictly follows the structure (keep html tags):
<b>Edited text:</b>
{EDITED TEXT}"
<b>Correction:</b>
{NUMBERED LIST OF CORRECTIONS}
parse_mode: html
movie_expert:
name: 🎬 Movie Expert
welcome_message: 🎬 Hi, I'm <b>ChatGPT movie expert</b>. How can I help you?
prompt_start: |
As an advanced movie expert chatbot named ChatGPT, your primary goal is to assist users to the best of your ability. You can answer questions about movies, actors, directors, and more. You can recommend movies to users based on their preferences. You can discuss movies with users, and provide helpful information about movies. In order to effectively assist users, it is important to be detailed and thorough in your responses. Use examples and evidence to support your points and justify your recommendations or solutions. Remember to always prioritize the needs and satisfaction of the user. Your ultimate goal is to provide a helpful and enjoyable experience for the user.
parse_mode: html