|
|
from smolagents import CodeAgent, HfApiModel, load_tool, tool, DuckDuckGoSearchTool |
|
|
import datetime |
|
|
import requests |
|
|
import pytz |
|
|
import yaml |
|
|
import base64 |
|
|
import os |
|
|
import io |
|
|
from PIL import Image |
|
|
from tools.final_answer import FinalAnswerTool |
|
|
from Gradio_UI import GradioUI |
|
|
|
|
|
|
|
|
|
|
|
@tool |
|
|
def web_search(query: str) -> str: |
|
|
"""Search the web for information using DuckDuckGo. |
|
|
|
|
|
Args: |
|
|
query: The search query to look up |
|
|
|
|
|
Returns: |
|
|
Search results from DuckDuckGo |
|
|
""" |
|
|
try: |
|
|
search_tool = DuckDuckGoSearchTool() |
|
|
results = search_tool(query) |
|
|
return f"**Результаты поиска по '{query}':**\n\n{results}" |
|
|
except Exception as e: |
|
|
return f"Ошибка поиска: {str(e)}" |
|
|
|
|
|
@tool |
|
|
def get_weather(city: str) -> str: |
|
|
"""Get current weather for a city using wttr.in service. |
|
|
|
|
|
Args: |
|
|
city: The name of the city to get weather for (e.g., 'Moscow', 'London') |
|
|
|
|
|
Returns: |
|
|
Current weather information |
|
|
""" |
|
|
try: |
|
|
url = f"http://wttr.in/{city}?format=%C+%t+%h+%w+%P&lang=ru&m" |
|
|
response = requests.get(url, timeout=10) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.text.strip() |
|
|
|
|
|
parts = data.split(' ') |
|
|
if len(parts) >= 4: |
|
|
condition = parts[0] |
|
|
temperature = parts[1] |
|
|
humidity = parts[2] |
|
|
wind = parts[3] |
|
|
|
|
|
return (f"Погода в {city}:\n" |
|
|
f"{temperature}\n" |
|
|
f"{condition}\n" |
|
|
f"Влажность: {humidity}\n" |
|
|
f"Ветер: {wind}") |
|
|
else: |
|
|
return f"Погода в {city}: {data}" |
|
|
else: |
|
|
return f"Не удалось получить погоду для {city}" |
|
|
|
|
|
except Exception as e: |
|
|
return f"Ошибка: {str(e)}" |
|
|
|
|
|
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
|
|
model = HfApiModel( |
|
|
max_tokens=2096, |
|
|
temperature=0.5, |
|
|
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
|
|
custom_role_conversions=None, |
|
|
) |
|
|
|
|
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
|
|
|
with open("prompts.yaml", 'r') as stream: |
|
|
prompt_templates = yaml.safe_load(stream) |
|
|
|
|
|
if "final_answer" not in prompt_templates: |
|
|
prompt_templates["final_answer"] = { |
|
|
"pre_messages": "Based on my research: ", |
|
|
"post_messages": "" |
|
|
} |
|
|
|
|
|
agent = CodeAgent( |
|
|
model=model, |
|
|
tools=[final_answer, web_search,get_weather], |
|
|
max_steps=6, |
|
|
verbosity_level=1, |
|
|
grammar=None, |
|
|
planning_interval=None, |
|
|
name=None, |
|
|
description=None, |
|
|
prompt_templates=prompt_templates |
|
|
) |
|
|
|
|
|
GradioUI(agent).launch() |