import datetime import requests import pytz import yaml # smolagents ile ilgili kütüphaneler from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool # "Final Answer" oluşturmak için özel araç from tools.final_answer import FinalAnswerTool # Gradio arayüzü from Gradio_UI import GradioUI # # 1) Geliştirilmiş Örnek Araç (metin özetleme benzeri) # @tool def my_custom_tool(arg1: str) -> str: """ A tool that simulates a simple text summarizer. Args: arg1: the text to be summarized """ words = arg1.split() summary = " ".join(words[:10]) + "..." return f"Summary: {summary}" # # 2) Saat dilimi aracı (Belirli bir saat dilimindeki saati döndürüyor) # @tool def get_current_time_in_timezone(timezone: str) -> str: """ A tool that fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: tz = pytz.timezone(timezone) local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"The current local time in {timezone} is: {local_time}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" # # 3) final_answer aracı (Ajanın nihai cevabını oluşturur) # final_answer = FinalAnswerTool() # # 4) Model parametrelerini güncelliyoruz: # - max_tokens: 1500 (daha uzun yanıtlar) # - temperature: 0.65 (daha dengeli) # model = HfApiModel( max_tokens=1500, temperature=0.65, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Gerekirse endpoint'i burada değiştirebilirsin custom_role_conversions=None, ) # # 5) Metinden resim oluşturma aracı (Hub'dan yükleniyor) # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) # # 6) DuckDuckGoSearchTool (İnternet araması) # duck_tool = DuckDuckGoSearchTool() # # 7) prompts.yaml dosyasındaki prompt şablonlarını yükle # with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) # Ufak bir ek: "system" prompt'una ek not if "system" in prompt_templates: prompt_templates["system"] += "\n\nYou are a helpful AI assistant. Provide clear, concise, and detailed answers where appropriate.\n" # # 8) CodeAgent oluştur # tools listesine eklediğimiz tüm araçları buraya ekliyoruz. # agent = CodeAgent( model=model, tools=[ final_answer, my_custom_tool, get_current_time_in_timezone, image_generation_tool, duck_tool ], max_steps=6, # 6 adımlık Thought→Action→Observation döngüsü verbosity_level=1, grammar=None, planning_interval=None, name="MyBetterAgent", description="An agent that can summarize text, generate images, check times, and do web search.", prompt_templates=prompt_templates ) # # 9) Gradio üzerinden web arayüzünü başlat # GradioUI(agent).launch()