liveBook · Manning

Prompt

As explained in Time Series Forecasting Using Foundation Models

A system prompt is an instruction supplied to a language model that establishes its context, behavior, tone, or response constraints.

A prompt is an instruction or formatted input that gives a language model context, behavior, tone, boundaries, or a task. In time-series work, prompts convert numerical sequences into natural-language requests and guide the model toward a structured prediction. section 8.0 section 8.6.1

Prompt-based forecasting typically separates two roles:

  • A system prompt supplies high-level instructions, such as treating the model as a time-series prediction assistant and specifying the expected numeric representation. section 8.6.1
  • A user prompt supplies the input sequence, forecast horizon, and output constraint, such as requesting only the next values. section 8.6.2
  • Few-shot and chain-of-thought prompts are identified as generally effective prompt forms, although language models may still forecast poorly because they were not built specifically for forecasting. section 8.10

Forecasting and anomaly detection

A forecasting prompt can describe comma-separated decimal values and ask for the next sequence. A modified system prompt can instead classify each value as anomalous or normal, using outputs such as [0, 0, 0, 1, 0] for an unusually large point. section 8.6.2 section 8.8.1

Listing 8.15 Setting the system prompt
system_prompt = """
    You are a helpful assistant that performs time series predictions.
    The user will provide a sequence and you will predict the next
    sequence.
    The sequence is represented by decimal numbers as strings separated by
    commas.
    """

Inspect a concrete system prompt that assigns the forecasting role, defines the input format, and requests the next sequence. section 8.6.1

Listing 8.16 Function to forecast via API call
def prompt_forecast(
    df,
    horizon,
    context_len,
    target_col
):
    input_values = df[f'{target_col}'].iloc[-context_len:]
    ➥.values.tolist()  #1

    response = requests.post(  #2
      url="https://openrouter.ai/api/v1
      ➥/chat/completions",  #3
      headers={
        "Authorization":
        ➥f"Bearer {OPENROUTER_API_KEY}",  #4
      },
      data=json.dumps({  #5
        "model":
        ➥"meta-llama/llama-3.2-3b-instruct:free",  #6
        "messages": [
            {
               "role": "system",
                "content": system_prompt  #7
            },
          {
            "role": "user",
            "content": f"The past values of the series are {input_values}.
            ➥What will be the next {horizon} values? Just output the
            ➥next values and nothing else."  #8
          }
        ]

      })
    )

    response_json = response.json()  #9
    content = response_json['choices'][0]
    ➥['message']['content']  #10
    if content.startswith('[') and content.endswith(']'):
        preds = json.loads(content)  #11
    else:
        wrapped = f'[{content}]'
        preds = json.loads(wrapped)

    return preds

Inspect the API implementation that sends system and user messages, authenticates the request, parses JSON, and extracts forecast values. section 8.6.2

How Time Series Forecasting Using Foundation Models covers it

How deep it goes
Explains prompt roles, formatting, API delivery, and output parsing, but defers robust safeguards and model internals.
Examples
Uses comma-separated sequences, forecast requests, anomaly labels, weekly sales context, and daily website-visit seasonality.
What it assumes
Assumes access to a language model and a way to format numerical inputs, while emphasizing robust prompt construction.
What you can do after
Apply it via API calls or Time-LLM configuration to provide context, request forecasts, and constrain model output.
How it connects
Connects prompts with large language models, time-series forecasting, anomaly detection, few-shot prompting, chain-of-thought prompting, and Time-LLM.

Also explained in

Compare how 50 books explain prompt →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Time Series Forecasting Using Foundation Models. The section links point to the parts of the book it draws on.