kopia lustrzana https://codeberg.org/pluja/openai-telegram-bot
make use of user setting temperature
rodzic
bc6801fa06
commit
28921d6b6d
12
main.py
12
main.py
|
@ -60,14 +60,14 @@ def restricted(func):
|
|||
return
|
||||
else:
|
||||
if not f"{update.effective_chat.id}" in users:
|
||||
users[f"{update.effective_chat.id}"] = {"context": [], "usage": {"chatgpt": 0,"whisper": 0,"dalle": 0,}, "options": {"whisper-to-chat": WHISPER_TO_CHAT, "temperature": 0.9, "max-context": 5}}
|
||||
users[f"{update.effective_chat.id}"] = {"context": [], "usage": {"chatgpt": 0,"whisper": 0,"dalle": 0,}, "options": {"whisper-to-chat": WHISPER_TO_CHAT, "temperature": float(TEMPERATURE), "max-context": MAX_USER_CONTEXT}}
|
||||
return await func(update, context, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
async def messageGPT(text: str, chat_id: str):
|
||||
# Initialize user if not present
|
||||
if chat_id not in users:
|
||||
users[chat_id] = {"context": [], "usage": {"chatgpt": 0, "whisper": 0, "dalle": 0}}
|
||||
users[chat_id] = {"context": [], "usage": {"chatgpt": 0,"whisper": 0,"dalle": 0,}, "options": {"whisper-to-chat": WHISPER_TO_CHAT, "temperature": float(TEMPERATURE), "max-context": MAX_USER_CONTEXT}}
|
||||
|
||||
# Update context
|
||||
user_context = users[chat_id]["context"]
|
||||
|
@ -81,7 +81,7 @@ async def messageGPT(text: str, chat_id: str):
|
|||
response = openai.ChatCompletion.create(
|
||||
model=MODEL,
|
||||
messages=[{"role": "system", "content": SYSTEM_PROMPT}] + user_context,
|
||||
temperature=float(TEMPERATURE)
|
||||
temperature=users[chat_id]["options"]["temperature"],
|
||||
)
|
||||
except:
|
||||
return "There was a problem with OpenAI, so I can't answer you."
|
||||
|
@ -106,7 +106,7 @@ async def messageGPT(text: str, chat_id: str):
|
|||
@restricted
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not f"{update.effective_chat.id}" in users:
|
||||
users[f"{update.effective_chat.id}"] = {"context": [], "usage": {"chatgpt": 0,"whisper": 0,"dalle": 0,}, "options": {"whisper-to-chat": WHISPER_TO_CHAT, "temperature": 0.9, "max-context": 5}}
|
||||
users[f"{update.effective_chat.id}"] = {"context": [], "usage": {"chatgpt": 0,"whisper": 0,"dalle": 0,}, "options": {"whisper-to-chat": WHISPER_TO_CHAT, "temperature": float(TEMPERATURE), "max-context": MAX_USER_CONTEXT}}
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
|
||||
|
||||
@restricted
|
||||
|
@ -182,7 +182,7 @@ async def attachment(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
|
||||
if audioMessage and users[f"{chat_id}"]["options"]["whisper-to-chat"]:
|
||||
chatGPT_response = await messageGPT(transcript['text'], str(chat_id))
|
||||
transcript['text'] = "> " + transcript['text'] + "\n\n " + chatGPT_response
|
||||
transcript['text'] = "> " + transcript['text'] + "\n\n" + chatGPT_response
|
||||
|
||||
# Check if the transcript length is longer than 4095 characters
|
||||
if len(transcript['text']) > 4095:
|
||||
|
@ -214,7 +214,7 @@ async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
|
||||
# Check if replying and add context
|
||||
if hasattr(update.message.reply_to_message, "text"):
|
||||
user_prompt = f"In reply to: '{update.message.reply_to_message.text}' \n---\n {update.message.text}"
|
||||
user_prompt = f"In reply to: '{update.message.reply_to_message.text}' \n---\n{update.message.text}"
|
||||
else:
|
||||
user_prompt = update.message.text
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue