Skip to main content

Additional Prompt

For our final prompt engineering asset, we can reuse many of the concepts we have already learned. We can start with the prompt itself. Unlike the original prompt, which would come in from the user, we can be very specific with the input value for the additional prompt. In this case, we will provide the current time (we can ignore timezones for this example) and the hours of operation from the fuel station. As with the last prompt, we will also ensure the output is in JSON. To accomplish this, our additional prompt might look like:

PROMPT_FUEL_STATION_OPEN = """
Given the hours of operation ('hours_of_operation': str) and and timestamp ('datetime': str).
Determine if the datetime ('%Y-%m-%d %H:%M:%S') falls within the hours of operation.

Only return the answer as a JSON object containing the is_open (as a boolean). No other information.

<example>
Input: {{
'hours_of_operation': '7am-7pm M-Th and Sat, 7am-8pm F, 9am-5pm Sun',
'datetime': '2025-01-21 18:00:00',
}}

Output:
{{
'is_open': True,
}}
</example>

<example>
Input: {{
'hours_of_operation': '7am-7pm M-Th and Sat, 7am-8pm F, 9am-5pm Sun',
'datetime': '2025-01-21 6:00:00',
}}

Output:
{{
'is_open': False,
}}
</example>

Input: {fuel_station_hours}
"""

Now that we have the prompt, we can create our asset. The asset will combine the current time with the alternative fuel stations data. It will then generate a prompt for each fuel station to ask the AI model and determine if the fuel station is currently available.


@dg.asset(
kinds={"anthropic"},
description="Determine if the nearest stations are available",
)
def available_fuel_stations(
context: dg.AssetExecutionContext,
anthropic: AnthropicResource,
nearest_fuel_stations,
):
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

fuel_stations_open = 0
with anthropic.get_client(context) as client:
for fuel_station in nearest_fuel_stations:
prompt_input = {
"access_days_time": fuel_station["access_days_time"],
"datetime": current_time,
}

prompt = PROMPT_FUEL_STATION_OPEN.format(fuel_station_hours=prompt_input)
resp = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
)

message = resp.content[0].text
data = json.loads(message)

if data["is_open"]:
context.log.info(
f"{fuel_station['station_name']} at {fuel_station['street_address']} is {fuel_station['distance']} miles away"
)
fuel_stations_open += 1

if fuel_stations_open == 0:

Running the pipeline

With all the pieces in place, we can execute our full pipeline. The prompts at each step will ensure that all the assets exchange data correctly. We can use the parsed down input from before: "I'm near the The Art Institute of Chicago and driving a Kia EV9". This input can be supplied at execution within the run launcher -- just set the configuration for the user_input_prompt asset:

ops:
user_input_prompt:
config:
location: I'm near the The Art Institute of Chicago and driving a Kia EV9

After the assets have all materialized, you can view the results in the logs of the available_fuel_stations asset:

INTERPARK ADAMWABASH 1 at 17 E Adams St is 0.16458 miles away
INTERPARK ADAMWABASH 2 at 17 E Adams St is 0.16613 miles away
MILLENNIUM GRGS MPG STATION #3 at 5 S Columbus Dr is 0.23195 miles away