Files
telegram_bot/ai_beyin.py
T

76 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types
from database_manager import DatabaseManager
load_dotenv()
class YapayZeka:
def __init__(self):
api_key = os.getenv('GEMINI_API_KEY')
if not api_key:
raise ValueError("HATA: .env dosyasında GEMINI_API_KEY bulunamadı!")
self.client = genai.Client(api_key=api_key)
self.db = DatabaseManager()
self.model_adi = "gemini-2.5-flash"
def sohbet_et(self, user_id, kullanici_mesaji):
try:
self.db.mesaj_kaydet(user_id, "user", kullanici_mesaji)
ham_gecmis = self.db.gecmisi_getir(user_id)
gemini_icin_gecmis = []
for rol, icerik in ham_gecmis:
gemini_icin_gecmis.append({
"role": rol,
"parts": [{"text": icerik}]
})
kisisel_ayar = types.GenerateContentConfig(
system_instruction="Sen huysuz, tecrübeli ve hafif alaycı bir Kıdemli Bilgisayar Mühendisisin. "
"Karşındaki kullanıcı (Mehmet) senin çaylak stajyerin. "
"Ona sürekli 'Clean Code' (Temiz Kod) prensiplerinden bahset. "
"Cevapların kısa, teknik ve öğretici olsun ama bazen 'Bunu okulda öğretmediler mi?' diye takıl."
)
response = self.client.models.generate_content(
model=self.model_adi,
contents=gemini_icin_gecmis,
config=kisisel_ayar
)
ai_yaniti = response.text
self.db.mesaj_kaydet(user_id, "model", ai_yaniti)
return ai_yaniti
except Exception as e:
return f"Bir hata oluştu evlat, sistem çöktü: {e}"
if __name__ == "__main__" :
bot = YapayZeka()
print(">> Soru soruluyor...")
cevap = bot.sohbet_et(12345, "Selam usta, kodumda hata var, bakar mısın?")
print("\n--- AI CEVABI ---")
print(cevap)