-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
72 lines (60 loc) · 2.2 KB
/
Copy pathbot.py
File metadata and controls
72 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart
from aiogram.types import Message
import logging
import os
from langchain_ollama import ChatOllama
from langchain_core.messages.ai import AIMessage
from mcp_use import MCPAgent, MCPClient
import os
import re
from dotenv import load_dotenv
from asyncio import run
logger = logging.getLogger(__name__)
load_dotenv()
BOT_TOKEN = os.getenv("BOT_TOKEN")
OLLAMA_URL = os.getenv("OLLAMA_URL")
CONFIG = {
"mcpServers": {
"Appointment Booking Assistant": {
"command": "mcp",
"args": [
"run",
"mcp_server.py"
]
}
}
}
SYSTEM_PROMPT = """
Ты администратор медицинского центра. Тебе доступны инструменты MCP. Отвечай только на вопросы, связанные с медицинским центром и записями к специалистам.
"""
client = MCPClient.from_dict(CONFIG)
llm = ChatOllama(model="qwen3:8b", base_url=OLLAMA_URL)
agent = MCPAgent(llm=llm, client=client, max_steps=30, system_prompt=SYSTEM_PROMPT)
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
def parse_result(result: str) -> str:
pattern = r"<think>.*?</think>"
cleaned_text = re.sub(pattern, "", result, flags=re.DOTALL)
return cleaned_text.strip()
async def ask(message: Message) -> str:
identifier = f"Пользователь с айди: {message.from_user.id} спрашивает у тебя:"
message_text = identifier + "\n" + message.text
content = await agent.run(message_text)
result = AIMessage(content)
return parse_result(result.content)
@dp.message(CommandStart())
async def start_command(message: Message):
await message.answer(f"Hello, {message.from_user.first_name}! I'm your bot.")
@dp.message()
async def ask_llm(message: Message):
message_text = message.text
await bot.send_chat_action(message.chat.id, "typing")
print(message_text)
response = await ask(message)
await message.answer(response, parse_mode="HTML")
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
logger.info('Running')
run(main())