File size: 2,974 Bytes
f22a4d8 9b5b26a c19d193 f22a4d8 6aae614 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 9b5b26a f22a4d8 8c01ffb 6aae614 ae7a494 e121372 f22a4d8 13d500a 8c01ffb 9b5b26a 8c01ffb 861422e f22a4d8 9b5b26a 8c01ffb 8fe992b f22a4d8 8c01ffb 861422e 8fe992b 8c01ffb |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
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
# 1. функции-инструменты
@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)}"
# 2. остальные объекты
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() |