How It Works

  1. User Question The assistant receives a natural language weather question like “Should I take a jacket in London today?”

  2. Get Weather Context Pine’s API analyzes the question and returns relevant real-time weather information formatted as LLM-ready context.

  3. Combine with LLM The weather context is passed along with the original question to OpenAI’s API, which generates a natural language response.

Step 1: Set API Keys

For this example you will need both a Pine API Key and an OpenAI API Key. After getting the keys, put them into environment variables or directly into your python code.

export PINE_API_KEY="sk-cj9I..."
export OPENAI_API_KEY="sk-A4Yk4E..."

Step 2: Install Dependencies

For this example we will need openai and requests package.

pip3 install openai
pip3 install requests

Step 3: Get Context from Pine

We write a function which calles the Pine Context API, and returns the context:

def get_weather_context(query):
    response = requests.post(
        'https://api.pine.dev/context',
        headers={
            'Authorization': f'Bearer {PINE_API_KEY}',
            'Content-Type': 'application/json'
        },
        json={
            'query': query
        }
    )

    return response.json()['markdown']

Step 4: Ask OpenAI with Context

We write a function which promps OpenAI to answer our query with Pine’s weather context:

def ask_open_ai_with_context(query, weather_context):
    client = OpenAI(api_key = OPENAI_API_KEY)
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a weather man. Answer with the provided context."},
            {"role": "user", "content": f"""Query: {query} \n\n Context: {weather_context} """}
        ]
    )

    return response.choices[0].message.content

Step 5: Putting Together

We now combine our functions,

question = sys.argv[1] # gets the first argument to our program
context = get_weather_context(question)
response = ask_open_ai_with_context(question, context)

print(response)

Final Script

The script should look like,

weather.py
from openai import OpenAI
import requests
import os
import sys

PINE_API_KEY = os.getenv('PINE_API_KEY')
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

def get_weather_context(query):
    response = requests.post(
        'https://api.pine.dev/context',
        headers={
            'Authorization': f'Bearer {PINE_API_KEY}',
            'Content-Type': 'application/json'
        },
        json={
            'query': query
        }
    )
    return response.json()['markdown']

def ask_open_ai_with_context(query, weather_context):
    client = OpenAI(api_key = OPENAI_API_KEY)
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a weather man. Answer with the provided context."},
            {"role": "user", "content": f"""Query: {query} \n\n Context: {weather_context} """}
        ]
    )

    return response.choices[0].message.content

question = sys.argv[1]
context = get_weather_context(question)
response = ask_open_ai_with_context(question, context)

print(response)

You can now run the script via:

python3 weather.py "Should I wear a jacket in London Tomorrow?"

Sample Responses

“What days this should I wear a jacket in NYC?”

Based on the weather forecast for New York City over the next few days, you should definitely consider wearing a jacket on the following days:

- **Wednesday, November 27**: Expect rain with temperatures dropping to 46°F at night.

- **Thursday, November 28**: Rain is expected with a high of 53°F and a low of 39°F, followed by partly cloudy skies at night.

- **Friday, November 29**: Mostly sunny but cold with a high of 47°F and a low of 32°F at night.

- **Saturday, November 30**: Mostly sunny with temperatures ranging from 40°F during the day to 29°F at night.

For all these days, particularly during evenings and nights, having a jacket will be beneficial to keep warm due to the chilly temperatures.

“How much snow did Mammoth get in the past couple days?”

In the past couple of days, Mammoth Mountain received between 48 to 60 inches of new snow from the Main Lodge to the top. Additionally, around two to three feet of new snow has accumulated at Canyon Lodge, with at least two feet at Little Eagle Lodge.