File size: 1,691 Bytes
27ecee0
9b5b26a
c19d193
8fe992b
27ecee0
9b5b26a
27ecee0
 
 
 
 
9b5b26a
 
27ecee0
 
 
 
 
 
 
 
 
 
 
9b5b26a
27ecee0
8c01ffb
27ecee0
e121372
27ecee0
 
 
13d500a
8c01ffb
27ecee0
 
861422e
27ecee0
 
8c01ffb
8fe992b
27ecee0
 
8c01ffb
861422e
8fe992b
 
27ecee0
 
 
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
from smolagents import CodeAgent, HfApiModel, tool
import requests
import yaml

# Define a research tool using EXA API
@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"  # Replace with your free EXA API key
    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]  # Get the first search result
            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)}"

# Load model from Hugging Face
model = HfApiModel(
    model_id='mistralai/Mistral-7B-Instruct',  # Change to your preferred model
    max_tokens=200,
    temperature=0.5
)

# Load prompt templates
with open("prompts.yaml", "r") as stream:
    prompt_templates = yaml.safe_load(stream)

# Define the AI Agent
agent = CodeAgent(
    model=model,
    tools=[search_cape_town_weather],  # Add the EXA search tool
    max_steps=3,
    verbosity_level=1,
    prompt_templates=prompt_templates
)

# Test the agent (Optional)
if __name__ == "__main__":
    print(agent.run("Find the latest weather update for Cape Town."))