diff --git a/ai_agent.py b/ai_agent.py index ebf4a6a..2ebf44d 100644 --- a/ai_agent.py +++ b/ai_agent.py @@ -182,40 +182,38 @@ class AiAgent: ai_response = response.choices[0].message.content context.append(response.choices[0].message) - if len(response.choices[0].message.tool_calls) > 0 and ai_response is not None and len(ai_response) > 0: - print(f"Модель хочет вызвать функцию, но также вернула текст: '{ai_response}'") - image_response_image: Optional[bytes] = None - for tool_call in response.choices[0].message.tool_calls: - tool_name = tool_call.function.name - tool_args = json.loads(tool_call.function.arguments) - if tool_name == "generate_image": - prompt = tool_args.get("prompt", "") - aspect_ratio = tool_args.get("aspect_ratio", None) - image_response_text, image_response_image =\ - await self._generate_image(prompt=prompt, aspect_ratio=aspect_ratio, user_tag=user_tag) - context.append({ - "role": "tool", - "tool_call_id": tool_call.id, - "content": [ - {"type": "text", - "text": """ + if response.choices[0].message.tool_calls is not None: + for tool_call in response.choices[0].message.tool_calls: + tool_name = tool_call.function.name + tool_args = json.loads(tool_call.function.arguments) + if tool_name == "generate_image": + prompt = tool_args.get("prompt", "") + aspect_ratio = tool_args.get("aspect_ratio", None) + image_response_text, image_response_image =\ + await self._generate_image(prompt=prompt, aspect_ratio=aspect_ratio, user_tag=user_tag) + context.append({ + "role": "tool", + "tool_call_id": tool_call.id, + "content": [ + {"type": "text", + "text": """ Изображение сгенерировано и будет показано пользователю. НЕ добавляй никаких тегов или маркеров вроде , [image] — они запрещены и не нужны. """}, - {"type": "image_url", "image_url": {"url": _encode_image(image_response_image)}} - ] - }) + {"type": "image_url", "image_url": {"url": _encode_image(image_response_image)}} + ] + }) - response2 = await self.client.chat.send_async( - model=self.model_main, - messages=context, - max_tokens=500, - user=user_tag, - http_headers=OPENROUTER_HEADERS - ) - ai_response = response2.choices[0].message.content - break + response2 = await self.client.chat.send_async( + model=self.model_main, + messages=context, + max_tokens=500, + user=user_tag, + http_headers=OPENROUTER_HEADERS + ) + ai_response = response2.choices[0].message.content + break self.db.context_add_message(bot_id, chat_id, role="user", text=message.text, image=message.image, message_id=message.message_id, max_messages=PRIVATE_CHAT_MAX_MESSAGES) diff --git a/vk/__main__.py b/vk/__main__.py index 74c58b4..2cc8a07 100644 --- a/vk/__main__.py +++ b/vk/__main__.py @@ -24,7 +24,9 @@ if __name__ == '__main__': database.create_database(config['db_connection_string']) - create_ai_agent(config['openrouter_token'], config['openrouter_model'], database.DB, 'vk') + create_ai_agent(config['openrouter_token_main'], config['openrouter_model_main'], + config['openrouter_token_image'], config['openrouter_model_image'], + database.DB, 'vk') bot = Bot(labeler=handlers.labeler) diff --git a/vk/handlers/private.py b/vk/handlers/private.py index d733dae..a279da3 100644 --- a/vk/handlers/private.py +++ b/vk/handlers/private.py @@ -54,11 +54,13 @@ async def any_message_handler(message: Message): await message.answer(MESSAGE_UNSUPPORTED_CONTENT_TYPE) return + answer: ai_agent.Message + success: bool answer, success = await utils.run_with_progress( partial(ai_agent.agent.get_private_chat_reply, bot_id, chat_id, ai_message), partial(message.ctx_api.messages.set_activity, peer_id=chat_id, type='typing'), interval=4) - answer_id = (await message.answer(answer)).message_id + answer_id = (await message.answer(answer.text)).message_id if success: ai_agent.agent.set_last_response_id(bot_id, chat_id, answer_id)