Skip to main content

haystack_threshold_node

This component filters documents based on a threshold percentage, ensuring only the documents above the threshold get passed down the pipeline. This allows you to query your document store for a larger top_k, but then filter the results down to those which are above a set confidence score.

Installation

pip install haystack-threshold-node

Usage

Include it in your pipeline - example as follows:

import logging
import re

from datasets import load_dataset
from haystack.document_stores import InMemoryDocumentStore
from haystack.nodes import PromptNode, PromptTemplate, AnswerParser, BM25Retriever
from haystack.pipelines import Pipeline
from haystack_lemmatize_node import LemmatizeDocuments


logging.basicConfig(format="%(levelname)s - %(name)s -  %(message)s", level=logging.WARNING)
logging.getLogger("haystack").setLevel(logging.INFO)

document_store = InMemoryDocumentStore(use_bm25=True)

dataset = load_dataset("bilgeyucel/seven-wonders", split="train")
document_store.write_documents(dataset)

retriever = BM25Retriever(document_store=document_store, top_k=10)

lfqa_prompt = PromptTemplate(
    name="lfqa",
    prompt_text="Given the context please answer the question using your own words. Generate a comprehensive, summarized answer. If the information is not included in the provided context, reply with 'Provided documents didn't contain the necessary information to provide the answer'\n\nContext: {documents}\n\nQuestion: {query} \n\nAnswer:",
    output_parser=AnswerParser(),
)

prompt_node = PromptNode(
    model_name_or_path="text-davinci-003",
    default_prompt_template=lfqa_prompt,
    max_length=500,
    api_key="sk-OPENAIKEY",
)

# The value you pass for threshold is the lowest % score you will accept. Whole numbers only.
# In this example, the threshold is set to 80%.
threshold = DocumentThreshold(threshold=80) 

pipe = Pipeline()
pipe.add_node(component=retriever, name="Retriever", inputs=["Query"])
pipe.add_node(component=threshold, name="Threshold", inputs=["Retriever"])
pipe.add_node(component=prompt_node, name="prompt_node", inputs=["Threshold"])

query = "What does the Rhodes Statue look like?"
  
output = pipe.run(query)

print(output['answers'][0].answer)

Metadata

Release files for haystack-threshold-node 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for haystack-threshold-node 0.0.1
File Size Uploaded
haystack_threshold_node-0.0.1.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for haystack-threshold-node 0.0.1
File Interpreter ABI Platform
haystack_threshold_node-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / haystack_threshold_node-0.0.1.tar.gz

Download URL haystack_threshold_node-0.0.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
39d14457171184e15ffbb8f49a8c03783794e7627d34151b47ac0daf5b9ade16
BLAKE2b-256 checksum
How to use checksums
271a20b5c579e7684ade0ae9956f4b64af594ee2505e408530c271ee5f12165f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / haystack_threshold_node-0.0.1-py3-none-any.whl

Download URL haystack_threshold_node-0.0.1-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74fe1cac6b771a466ac31a2f0a68917a5f68e90155bc1d934f9ef54fdba1a021
BLAKE2b-256 checksum
How to use checksums
0329316905d81030f5fc926f65f8f5fd6f32555232bfebe1e4c981ed87b12fbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page