mirror of
				https://github.com/deepset-ai/haystack.git
				synced 2025-11-04 03:39:31 +00:00 
			
		
		
		
	* Initial commit, add search_engine * Add TopPSampler * Add more TopPSampler unit tests * Remove SearchEngineSampler (converted to TopPSampler) * Add some basic WebSearch unit tests * Rename unit tests * Add WebRetriever into agent_tools * Adjust to WebRetriever * Add WebRetriever mode [snippet|document] * Minor changes * SerperDev: add peopleAlsoAsk search results * First agent for hotpotqa * Making WebRetriever work on hotpotqa * refactor: minor WebRetriever improvements (#4377) * refactor: remove doc ids rebuild + antecipate cache * refactor: improve caching, fix Document ids * Minor WebRetriever improvements * Overlooked minor fixes * feat: add Bing API as search engine * refactor: let kwargs pass-through * feat: increase search context * check sampler result, improve batch typing * refactor: increase mypy compliance * Initial commit, add search_engine * Add TopPSampler * Add more TopPSampler unit tests * Remove SearchEngineSampler (converted to TopPSampler) * Add some basic WebSearch unit tests * Rename unit tests * Add WebRetriever into agent_tools * Adjust to WebRetriever * Add WebRetriever mode [snippet|document] * Minor changes * SerperDev: add peopleAlsoAsk search results * First agent for hotpotqa * Making WebRetriever work on hotpotqa * refactor: minor WebRetriever improvements (#4377) * refactor: remove doc ids rebuild + antecipate cache * refactor: improve caching, fix Document ids * Minor WebRetriever improvements * Overlooked minor fixes * feat: add Bing API as search engine * refactor: let kwargs pass-through * feat: increase search context * check sampler result, improve batch typing * refactor: increase mypy compliance * Fix mypy * Minor example fixes * Fix the descriptions * PR feedback updates * More fixes * TopPSampler: handle top p None value, add unit test * Add top_k to WebSearch * Use boilerpy3 instead trafilatura * Remove date finding * Add more WebRetriever docs * Refactor long methods * making the preprocessor optional * hide WebSearch and make NeuralWebSearch a pipeline * remove unused imports * add WebQAPipeline and split example into two * change example search engine to SerperDev * Turn off progress bars in WebRetriever's PreProcesssor * Agent tool examples - final updates * Add webqa test, search results ranking scores * Better answer box handling for SerperDev and SerpAPI * Minor fixes * pylint * pylint fixes * extract TopPSampler from WebRetriever * use sampler only for WebRetriever modes other than snippet * add web retriever tests * add web retriever tests * exclude rdflib@6.3.2 due to license issues * add test for preprocessed docs and kwargs examples in docstrings * Move test_webqa_pipeline to test/pipelines * change docstring for join_documents_and_scores * Use WebQAPipeline in examples/web_lfqa.py * Use WebQAPipeline in examples/web_lfqa.py * Move test_webqa_pipeline to e2e * Updated lg * Sampler added automatically in WebQAPipeline, no need to add it * Updated lg * Updated lg * :ignore Update agent tools examples to new templates (#4503) * Update examples to new templates * Add print back * fix linting and black format issues --------- Co-authored-by: Daniel Bichuetti <daniel.bichuetti@gmail.com> Co-authored-by: agnieszka-m <amarzec13@gmail.com> Co-authored-by: Julian Risch <julian.risch@deepset.ai>
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import os
 | 
						|
 | 
						|
from haystack.nodes import PromptNode, PromptTemplate
 | 
						|
from haystack.nodes.retriever.web import WebRetriever
 | 
						|
from haystack.pipelines import WebQAPipeline
 | 
						|
 | 
						|
search_key = os.environ.get("SERPERDEV_API_KEY")
 | 
						|
if not search_key:
 | 
						|
    raise ValueError("Please set the SERPERDEV_API_KEY environment variable")
 | 
						|
 | 
						|
openai_key = os.environ.get("OPENAI_API_KEY")
 | 
						|
if not search_key:
 | 
						|
    raise ValueError("Please set the OPENAI_API_KEY environment variable")
 | 
						|
 | 
						|
prompt_text = """
 | 
						|
Synthesize a comprehensive answer from the following most relevant paragraphs and the given question.
 | 
						|
Provide a clear and concise response that summarizes the key points and information presented in the paragraphs.
 | 
						|
Your answer should be in your own words and be no longer than 50 words.
 | 
						|
\n\n Paragraphs: {documents} \n\n Question: {query} \n\n Answer:
 | 
						|
"""
 | 
						|
 | 
						|
prompt_node = PromptNode(
 | 
						|
    "text-davinci-003",
 | 
						|
    default_prompt_template=PromptTemplate("lfqa", prompt_text=prompt_text),
 | 
						|
    api_key=openai_key,
 | 
						|
    max_length=256,
 | 
						|
)
 | 
						|
 | 
						|
web_retriever = WebRetriever(api_key=search_key, top_search_results=2, mode="preprocessed_documents")
 | 
						|
pipeline = WebQAPipeline(retriever=web_retriever, prompt_node=prompt_node)
 | 
						|
 | 
						|
# Long-Form QA requiring multiple context paragraphs for the synthesis of an elaborate generative answer
 | 
						|
questions = [
 | 
						|
    "What are the advantages of EmbeddingRetriever in Haystack?",
 | 
						|
    "What are the advantages of PromptNode in Haystack?",
 | 
						|
    "What PromptModelInvocationLayer implementations are available in Haystack?",
 | 
						|
]
 | 
						|
 | 
						|
for q in questions:
 | 
						|
    print(f"Question: {q}")
 | 
						|
    response = pipeline.run(query=q)
 | 
						|
    print(f"Answer: {response['results'][0]}")
 |