liveBook · Manning

Top-p filtering

As explained in Spring AI in Action

Top-p is a language-model generation parameter from 0 to 1 that limits token candidates to the smallest probability set whose cumulative probability reaches the parameter value.

Top-p filtering limits generation to the smallest set of candidate tokens whose cumulative probability reaches the configured value, from 0 to 1. section 3.4.1

Configure top-p in Spring AI

Set top-p globally with spring.ai.openai.chat.options.top-p=0.8, or configure it programmatically with ChatOptions.builder().topP(0.8).build(). section 3.4.1

Top-p works alongside other chat options:

  • Temperature scales candidate probabilities; top-p and top-k eliminate lower-ranked candidates. section 3.4.1
  • A lower top-p value retains a smaller probability set, while a higher value allows more candidates. section 3.4.1
spring.ai.openai.chat.options.top-p=0.8

Use the Spring Boot property listing to configure top-p for an OpenAI chat model. section 3.4.1

ChatOptions chatOptions = ChatOptions.builder()
    .topP(0.8)
    .build();

Use the ChatOptions listing to set top-p programmatically. section 3.4.1

How Spring AI in Action covers it

How deep it goes
Defines the cumulative-probability rule and configuration paths, but stops short of mathematical implementation details.
Examples
Uses ranked candidate words with probabilities and Spring Boot or ChatOptions configuration examples.
What it assumes
Assumes familiarity with chat generation, candidate-token probabilities, and Spring AI chat options.
What you can do after
Configure it through Spring Boot properties or ChatOptions when creating or invoking a chat client.
How it connects
Connects top-p with temperature, top-k, ChatOptions, ChatClient, and token probabilities.

Also explained in

Compare how 12 books explain top-p filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.