File size: 1,935 Bytes
c44ba95 c19d193 6aae614 9b5b26a c44ba95 ae7a494 e121372 c44ba95 8c01ffb 861422e c44ba95 8c01ffb 8fe992b c44ba95 8c01ffb c44ba95 861422e c44ba95 9b5b26a c44ba95 |
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 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import re
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def get_distance_to_planet(planet: str) -> str:
"""A tool that fetches the distance (in kilometers) from Earth to a given planet using an online lookup.
Args:
planet: Name of the planet in the solar system.
"""
try:
# Build a query to search for the distance information.
query = f"distance from Earth to {planet} in km"
# Use the DuckDuckGoSearchTool to perform the search.
search_results = DuckDuckGoSearchTool.run(query=query)
# Use a regex to extract a number (which may include commas or decimals) followed by 'km'
match = re.search(r'([\d,\.]+)\s*km', search_results)
if match:
distance = match.group(1)
return f"The distance from Earth to {planet} is approximately {distance} km."
else:
return f"Unable to extract the distance for {planet} from the search results."
except Exception as e:
return f"Error fetching the distance to {planet}: {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,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, get_distance_to_planet],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="Planet_Distance_Agent",
description="Agent that provides the distance from Earth to any planet in the solar system using a web search.",
prompt_templates=prompt_templates
)
GradioUI(agent).launch()
|