Исправлены ошибки в 44f9bb8c.
This commit is contained in:
parent
44f9bb8c04
commit
cb60b23ae7
3 changed files with 33 additions and 31 deletions
56
ai_agent.py
56
ai_agent.py
|
|
@ -182,40 +182,38 @@ class AiAgent:
|
||||||
ai_response = response.choices[0].message.content
|
ai_response = response.choices[0].message.content
|
||||||
context.append(response.choices[0].message)
|
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
|
image_response_image: Optional[bytes] = None
|
||||||
for tool_call in response.choices[0].message.tool_calls:
|
if response.choices[0].message.tool_calls is not None:
|
||||||
tool_name = tool_call.function.name
|
for tool_call in response.choices[0].message.tool_calls:
|
||||||
tool_args = json.loads(tool_call.function.arguments)
|
tool_name = tool_call.function.name
|
||||||
if tool_name == "generate_image":
|
tool_args = json.loads(tool_call.function.arguments)
|
||||||
prompt = tool_args.get("prompt", "")
|
if tool_name == "generate_image":
|
||||||
aspect_ratio = tool_args.get("aspect_ratio", None)
|
prompt = tool_args.get("prompt", "")
|
||||||
image_response_text, image_response_image =\
|
aspect_ratio = tool_args.get("aspect_ratio", None)
|
||||||
await self._generate_image(prompt=prompt, aspect_ratio=aspect_ratio, user_tag=user_tag)
|
image_response_text, image_response_image =\
|
||||||
context.append({
|
await self._generate_image(prompt=prompt, aspect_ratio=aspect_ratio, user_tag=user_tag)
|
||||||
"role": "tool",
|
context.append({
|
||||||
"tool_call_id": tool_call.id,
|
"role": "tool",
|
||||||
"content": [
|
"tool_call_id": tool_call.id,
|
||||||
{"type": "text",
|
"content": [
|
||||||
"text": """
|
{"type": "text",
|
||||||
|
"text": """
|
||||||
Изображение сгенерировано и будет показано пользователю.
|
Изображение сгенерировано и будет показано пользователю.
|
||||||
НЕ добавляй никаких тегов или маркеров вроде <image>, [image] — они запрещены и не нужны.
|
НЕ добавляй никаких тегов или маркеров вроде <image>, [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(
|
response2 = await self.client.chat.send_async(
|
||||||
model=self.model_main,
|
model=self.model_main,
|
||||||
messages=context,
|
messages=context,
|
||||||
max_tokens=500,
|
max_tokens=500,
|
||||||
user=user_tag,
|
user=user_tag,
|
||||||
http_headers=OPENROUTER_HEADERS
|
http_headers=OPENROUTER_HEADERS
|
||||||
)
|
)
|
||||||
ai_response = response2.choices[0].message.content
|
ai_response = response2.choices[0].message.content
|
||||||
break
|
break
|
||||||
|
|
||||||
self.db.context_add_message(bot_id, chat_id, role="user", text=message.text, image=message.image,
|
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)
|
message_id=message.message_id, max_messages=PRIVATE_CHAT_MAX_MESSAGES)
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,9 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
database.create_database(config['db_connection_string'])
|
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)
|
bot = Bot(labeler=handlers.labeler)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,11 +54,13 @@ async def any_message_handler(message: Message):
|
||||||
await message.answer(MESSAGE_UNSUPPORTED_CONTENT_TYPE)
|
await message.answer(MESSAGE_UNSUPPORTED_CONTENT_TYPE)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
answer: ai_agent.Message
|
||||||
|
success: bool
|
||||||
answer, success = await utils.run_with_progress(
|
answer, success = await utils.run_with_progress(
|
||||||
partial(ai_agent.agent.get_private_chat_reply, bot_id, chat_id, ai_message),
|
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'),
|
partial(message.ctx_api.messages.set_activity, peer_id=chat_id, type='typing'),
|
||||||
interval=4)
|
interval=4)
|
||||||
|
|
||||||
answer_id = (await message.answer(answer)).message_id
|
answer_id = (await message.answer(answer.text)).message_id
|
||||||
if success:
|
if success:
|
||||||
ai_agent.agent.set_last_response_id(bot_id, chat_id, answer_id)
|
ai_agent.agent.set_last_response_id(bot_id, chat_id, answer_id)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue