| from smolagents import CodeAgent, HfApiModel, tool |
| import requests |
| import yaml |
|
|
| |
| @tool |
| def search_cape_town_weather() -> str: |
| """Searches for the latest weather in Cape Town using EXA search API.""" |
| api_key = "fe5ee653-7f0d-493c-99ae-8e63d46133d9" |
| query = "current weather in Cape Town site:bbc.com OR site:cnn.com OR site:news24.com" |
| url = f"https://api.exa.ai/v1/search?q={query}&api_key={api_key}" |
|
|
| try: |
| response = requests.get(url) |
| data = response.json() |
|
|
| if "results" in data and len(data["results"]) > 0: |
| top_result = data["results"][0] |
| title = top_result["title"] |
| snippet = top_result["snippet"] |
| link = top_result["url"] |
| return f"Latest Cape Town weather report:\n{title}\n{snippet}\nMore info: {link}" |
| else: |
| return "No weather updates found. Try again later." |
| except Exception as e: |
| return f"Error fetching data: {str(e)}" |
|
|
| |
| model = HfApiModel( |
| model_id='mistralai/Mistral-7B-Instruct', |
| max_tokens=200, |
| temperature=0.5 |
| ) |
|
|
| |
| with open("prompts.yaml", "r") as stream: |
| prompt_templates = yaml.safe_load(stream) |
|
|
| |
| agent = CodeAgent( |
| model=model, |
| tools=[search_cape_town_weather], |
| max_steps=3, |
| verbosity_level=1, |
| prompt_templates=prompt_templates |
| ) |
|
|
| |
| if __name__ == "__main__": |
| print(agent.run("Find the latest weather update for Cape Town.")) |
|
|