Datasets:
Update runs/python/telegram_gemini2.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455561234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import asyncio
import aiohttp # برای درخواستهای async
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes, filters
import logging
# تنظیمات
BOT_TOKEN = "7490823724:AAEcskSIKg9t63nBME3Igkxw_QE4dl2Ql_U"
LLAMA_API_URL = "http://127.0.0.1:8080/completion"
# لاگگیری
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# گرفتن پاسخ از llama.cpp به صورت async
async def get_llama_response(prompt: str) -> str:
system_prompt = f"User: {prompt}\nAssistant:"
payload = {
"prompt": system_prompt,
"max_tokens": 64,
"temperature": 0.7,
"stop": ["</s>", "User:"]
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(LLAMA_API_URL, json=payload, timeout=60) as resp:
if resp.status == 200:
data = await resp.json()
return data.get("content", "").strip()
else:
logging.error(f"LLaMA API Error: {resp.status}")
return "❌ خطا در دریافت پاسخ از مدل زبان."
except asyncio.TimeoutError:
return "⏱️ مدل دیر پاسخ داد. لطفاً دوباره تلاش کنید."
except Exception as e:
logging.exception("خطا در ارتباط با مدل:")
return "⚠️ خطا در پردازش درخواست شما."
# هندل کردن پیامهای دستوری
async def handle_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.message
if message and message.text:
user_input = message.text.lstrip('/')
await message.chat.send_action("typing")
response = await get_llama_response(user_input)
await message.reply_text(response)
# راهاندازی ربات
def main():
app = ApplicationBuilder().token(BOT_TOKEN).build()
app.add_handler(MessageHandler(filters.COMMAND, handle_command))
app.run_polling()
if __name__ == "__main__":
main()import asyncioimport aiohttp # برای درخواستهای asyncfrom telegram import Updatefrom telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes, filtersimport logging
# تنظیماتBOT_TOKEN = ""LLAMA_API_URL = "http://127.0.0.1:8080/completion"
# لاگگیریlogging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
# گرفتن پاسخ از llama.cpp به صورت asyncasync def get_llama_response(prompt: str) -> str: system_prompt = f"User: {prompt}\nAssistant:" payload = { "prompt": system_prompt, "max_tokens": 64, "temperature": 0.7, "stop": ["</s>", "User:"] } try: async with aiohttp.ClientSession() as session: async with session.post(LLAMA_API_URL, json=payload, timeout=60) as resp: if resp.status == 200: data = await resp.json() return data.get("content", "").strip() else: logging.error(f"LLaMA API Error: {resp.status}") return "❌ خطا در دریافت پاسخ از مدل زبان." except asyncio.TimeoutError: return "⏱️ مدل دیر پاسخ داد. لطفاً دوباره تلاش کنید." except Exception as e: logging.exception("خطا در ارتباط با مدل:") return "⚠️ خطا در پردازش درخواست شما."
# هندل کردن پیامهای دستوریasync def handle_command(update: Update, context: ContextTypes.DEFAULT_TYPE): message = update.message if message and message.text: user_input = message.text.lstrip('/') await message.chat.send_action("typing") response = await get_llama_response(user_input) await message.reply_text(response)
# راهاندازی رباتdef main(): app = ApplicationBuilder().token(BOT_TOKEN).build() app.add_handler(MessageHandler(filters.COMMAND, handle_command)) app.run_polling()
if __name__ == "__main__": main()runs/python/telegram_gemini2.py+56-56