class_docstr: Implements top-p (nucleus) sampling for document filtering based on cumulative probability scores.
  
  This class provides functionality to filter a list of documents by selecting those whose scores fall
  within the top 'p' percent of the cumulative distribution. The method is useful for focusing on high-probability
  documents while filtering out less relevant ones based on their assigned scores.
  
  Usage example:
  
  ```python
  from haystack import Document
  from haystack.components.samplers import TopPSampler
  
  sampler = TopPSampler(top_p=0.95, score_field="similarity_score")
  docs = [
      Document(text="Berlin", meta={"similarity_score": -10.6}),
      Document(text="Belgrade", meta={"similarity_score": -8.9}),
      Document(text="Sarajevo", meta={"similarity_score": -4.6}),
  ]
  output = sampler.run(documents=docs)
  docs = output["documents"]
  assert len(docs) == 1
  assert docs[0].content == "Sarajevo"
  ```
docstr: Creates an instance of TopPSampler.
  
  :param top_p: Float between 0 and 1 representing the cumulative probability threshold for document selection.
  Defaults to 1.0, indicating no filtering (all documents are retained).
  :param score_field: Name of the field in each document's metadata that contains the score. If None, the default
  document score field is used.
docstr_tok: ["Creates","an","instance","of","TopPSampler",".",":","param","top_p",":","Float","between","0","and","1","representing","the","cumulative","probability","threshold","for","document","selection",".","Defaults","to","1.0",",","indicating","no","filtering","(","all","documents","are","retained",")",".",":","param","score_field",":","Name","of","the","field","in","each","document","'s","metadata","that","contains","the","score",".","If","None",",","the","default","document","score","field","is","used","."]
code: def __init__(
      self, top_p: float = 1.0, score_field: Optional[str] = None
  ):
      """
      Creates an instance of TopPSampler.
  
      :param top_p: Float between 0 and 1 representing the cumulative probability threshold for document selection.
      Defaults to 1.0, indicating no filtering (all documents are retained).
      :param score_field: Name of the field in each document's metadata that contains the score. If None, the default
      document score field is used.
      """
      torch_import.check()
  
      self.top_p = top_p
      self.score_field = score_field
  
imports: import logging
  from typing import List, Optional
  from haystack import ComponentError, Document, component
  from haystack.lazy_imports import LazyImport
label_desc: Use image node_id 1 to create a new TopPSampler object  with example: obj = TopPSampler(top_p, score_field)