limit response lenght in gemini

This commit is contained in:
thedragonsinn
2024-03-01 12:25:24 +05:30
parent c1556d5c76
commit e4e364da0e

View File

@@ -11,9 +11,7 @@ MODEL = genai.GenerativeModel(
"gemini-pro", safety_settings={"HARASSMENT": "block_none"} "gemini-pro", safety_settings={"HARASSMENT": "block_none"}
) )
INSTRUCTIONS = ( INSTRUCTIONS = "Your response length must not exceed 4000 for all of my question(s):\n"
"your response length must not exceed 4000 for this following question:\n"
)
async def init_task(): async def init_task():
@@ -44,7 +42,8 @@ async def question(bot: BOT, message: Message):
""" """
if not (await basic_check(message)): # fmt:skip if not (await basic_check(message)): # fmt:skip
return return
response = await MODEL.generate_content_async(message.input) prompt = INSTRUCTIONS + message.input
response = await MODEL.generate_content_async(prompt)
response_text = get_response_text(response) response_text = get_response_text(response)
await message.reply( await message.reply(
text="**GEMINI AI**:\n\n" + response_text, parse_mode=ParseMode.MARKDOWN text="**GEMINI AI**:\n\n" + response_text, parse_mode=ParseMode.MARKDOWN
@@ -96,7 +95,7 @@ async def ai_chat(bot: BOT, message: Message):
await resp.edit("<i>History Loaded... Resuming chat</i>") await resp.edit("<i>History Loaded... Resuming chat</i>")
chat = MODEL.start_chat(history=history) chat = MODEL.start_chat(history=history)
try: try:
await do_convo(chat=chat, message=message) await do_convo(chat=chat, message=message, history=True)
except TimeoutError: except TimeoutError:
await export_history(chat, message) await export_history(chat, message)
@@ -105,8 +104,11 @@ def get_response_text(response):
return "\n".join([part.text for part in response.parts]) return "\n".join([part.text for part in response.parts])
async def do_convo(chat, message: Message): async def do_convo(chat, message: Message, history: bool = False):
prompt = message.input if not history:
prompt = INSTRUCTIONS + message.input
else:
prompt = message.input
reply_to_message_id = message.id reply_to_message_id = message.id
async with Convo( async with Convo(
client=bot, client=bot,