mirror of
https://github.com/father-bot/chatgpt_telegram_bot.git
synced 2026-06-13 03:54:57 +03:00
85 lines
4.5 KiB
Python
85 lines
4.5 KiB
Python
import config
|
|
|
|
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."
|
|
},
|
|
|
|
"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."
|
|
},
|
|
}
|
|
|
|
|
|
class ChatGPT:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def send_message(self, message, dialog_messages=[], chat_mode="assistant"):
|
|
if chat_mode not in CHAT_MODES.keys():
|
|
raise ValueError(f"Chat mode {chat_mode} is not supported")
|
|
|
|
n_dialog_messages_before = len(dialog_messages)
|
|
answer = None
|
|
while answer is None:
|
|
prompt = self._generate_prompt(message, dialog_messages, chat_mode)
|
|
try:
|
|
r = openai.Completion.create(
|
|
engine="text-davinci-003",
|
|
prompt=prompt,
|
|
temperature=0.7,
|
|
max_tokens=1000,
|
|
top_p=1,
|
|
frequency_penalty=0,
|
|
presence_penalty=0,
|
|
)
|
|
answer = r.choices[0].text
|
|
answer = self._postprocess_answer(answer)
|
|
|
|
n_used_tokens = r.usage.total_tokens
|
|
|
|
except openai.error.InvalidRequestError as e: # too many tokens
|
|
if len(dialog_messages) == 0:
|
|
raise ValueError("Dialog messages is reduced to zero, but still has too many tokens to make completion") from e
|
|
|
|
# forget first message in dialog_messages
|
|
dialog_messages = dialog_messages[1:]
|
|
|
|
n_first_dialog_messages_removed = n_dialog_messages_before - len(dialog_messages)
|
|
|
|
return answer, prompt, n_used_tokens, n_first_dialog_messages_removed
|
|
|
|
def _generate_prompt(self, message, dialog_messages, chat_mode):
|
|
prompt = CHAT_MODES[chat_mode]["prompt_start"]
|
|
prompt += "\n\n"
|
|
|
|
# add chat context
|
|
if len(dialog_messages) > 0:
|
|
prompt += "Chat:\n"
|
|
for dialog_message in dialog_messages:
|
|
prompt += f"User: {dialog_message['user']}\n"
|
|
prompt += f"ChatGPT: {dialog_message['bot']}\n"
|
|
|
|
# current message
|
|
prompt += f"User: {message}\n"
|
|
prompt += "ChatGPT: "
|
|
|
|
return prompt
|
|
|
|
def _postprocess_answer(self, answer):
|
|
answer = answer.strip()
|
|
return answer |