Few-shot learning supplies a language model with a small set of input-output examples that demonstrate the required task and response format. In forecasting, examples nudge the model toward returning numerical predictions rather than explanatory sentences. section 8.1.2
A practical few-shot prompt typically does the following:
- Frames a numerical problem as a natural-language task, such as predicting the next values in a sequence. section 8.1.2
- Shows representative inputs alongside desired outputs, then appends a new input for the model to complete. section 8.1.2
- Constrains the response to a usable format, reducing sentence parsing and downstream processing. section 8.1.2
- Works with preprocessing and postprocessing: scale values, generate predictions, cast strings to numbers, adjust output length, and inverse-transform results. section 8.2.1
Sequence forecasting
A prompt can demonstrate that eight scaled historical values map to seven numeric future values, then ask Flan-T5 to forecast the next sequence. A low temperature favors repeatable outputs, while scaling keeps generated numbers relatively small. section 8.2.1
Listing 8.2 Forecasting function with a local LLM
def llm_forecast(
df,
model,
tokenizer,
horizon,
context_len,
target_col,
max_new_tokens=100,
temperature=1.0
):
scaler = MinMaxScaler(feature_range=(0, 1))
all_values = df[target_col].values.reshape(-1, 1)
scaler.fit(all_values) #1
context_values = df[target_col].tail(context_len).values.reshape(-1, 1)
scaled_context_values = scaler.transform(context_values)
➥.flatten() #2
few_shot_examples = """
Input: The last 8 scaled values of Weekly_Sales were 0.5283317 ,
➥0.58826193, 0.71939157, 0.95514386, 0.43612893, 0.45639017,
➥0.45729709,0.46355804. What will be the next values?
Output: 0.53329332, 0.55390031, 0.59245479, 0.52100866, 0.52860909,
➥0.50953767, 0.53251093
""" #3
values_str = ", ".join(map(lambda x: f"{x:.4f}", scaled_context_values))
prompt = f"""
{few_shot_examples}
Input: The last {context_len} scaled values of {target_col} were
➥{values_str}.
What will be the next {horizon} scaled values? Remember to provide
➥exactly {horizon} comma-separated numbers between 0 and 1.
""" #4
prompt += "\nOutput:"
inputs = tokenizer(prompt, return_tensors='pt')
with torch.no_grad(): #5
output = tokenizer.decode(
model.generate(
inputs["input_ids"],
max_new_tokens=max_new_tokens,
temperature=temperature
)[0],
skip_special_tokens=True
)
print("Raw model output:", output)
output = output.rstrip('.')
try: #6
scaled_preds = [float(x.strip()) for x in output.split(',') if
➥x.strip()]
except ValueError:
print(f"Warning: Could not convert all outputs to float. Using only
➥valid floats.")
scaled_preds = [float(x.strip()) for x in output.split(',') if
➥x.strip() and x.replace('.', '').isdigit()]
if len(scaled_preds) < horizon: #7
print(f"Warning: Did not generate enough predictions. Only
➥generated {len(scaled_preds)} out of {horizon}")
last_valid = scaled_preds[-1] if scaled_preds else 0.5 # Use 0.5
➥if no valid predictions
scaled_preds.extend([last_valid] * (horizon - len(scaled_preds)))
elif len(scaled_preds) > horizon:
scaled_preds = scaled_preds[:horizon]
scaled_preds = np.clip(scaled_preds, 0, 1) #8
preds = scaler.inverse_transform(np.array(scaled_preds)
➥.reshape(-1, 1)).flatten() #9
return preds.tolist()
Inspect the forecasting function to see where few-shot examples enter the prompt and how generated text becomes an evaluated forecast. section 8.2.1

















