Weather Forecast Agent with Pydantic AI & Nebius¶
This notebook demonstrates how to build a simple weather forecast agent using Pydantic AI and Nebius Token Factory. The agent fetches the current weather forecast for a specified city by searching DuckDuckGo with the meta-llama/Meta-Llama-3.1-70B-Instruct model.
Nebius Token Factory provides access to many state-of-the-art LLM models. Check out the full list of models here.
Visit http://tokenfactory.nebius.com/ and sign up to get an API key.
Step 1: Install Dependencies¶
!pip install pydantic_ai 'pydantic-ai-slim[duckduckgo]'
Step 2: Enter Your API Key¶
For security, we’ll prompt you to enter your Nebius API key instead of hardcoding it, since Colab doesn’t natively support .env files.
# set API key in env or in llm
import os
os.environ["NEBIUS_API_KEY"] = "Your Nebius API Key"
import nest_asyncio
nest_asyncio.apply()
Step 3: Import Required Modules¶
We import essential modules from pydantic_ai to work with Nebius Token Factory and DuckDuckGo search.
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
from pydantic_ai.common_tools.duckduckgo import duckduckgo_search_tool
Step 4: Initializing the Weather Agent¶
Set up the Nebius AI model and configure the agent to fetch weather forecasts.
# Set up the model with the user-provided API key
model = OpenAIModel(
model_name='meta-llama/Meta-Llama-3.1-70B-Instruct',
provider=OpenAIProvider(
base_url='https://api.tokenfactory.nebius.com/v1/',
api_key=os.environ['NEBIUS_API_KEY']
)
)
# Create the agent with a weather-focused prompt
agent = Agent(
model=model,
tools=[duckduckgo_search_tool()],
system_prompt="You are a weather assistant. Use DuckDuckGo to find the current weather forecast for the requested city."
)
Step 5: Running a Weather Forecast Query¶
Ask the agent for the weather forecast of a city (e.g., Kolkata). You can change the city to any location you’d like!
# Define the city
city = "Kolkata" # Change this to any city you like!
# Run the agent
result = agent.run_sync(f"What is the weather forecast for {city} today?")
# Display the result
print(f"Weather forecast for {city}:")
print(result.data)
Weather forecast for Kolkata: The current weather forecast for Kolkata today is mostly sunny with a high of 90°F and a low of 69°F. There is a chance of showers on Wednesday afternoon, but the rest of the week is expected to be mostly clear.