|
| 1 | +""" |
| 2 | +PDFScraperGraph Module |
| 3 | +""" |
| 4 | + |
| 5 | +from .base_graph import BaseGraph |
| 6 | +from ..nodes import ( |
| 7 | + FetchNode, |
| 8 | + ParseNode, |
| 9 | + RAGNode, |
| 10 | + GenerateAnswerNode |
| 11 | +) |
| 12 | +from .abstract_graph import AbstractGraph |
| 13 | + |
| 14 | + |
| 15 | +class PDFScraperGraph(AbstractGraph): |
| 16 | + """ |
| 17 | + PDFScraperGraph is a scraping pipeline that extracts information from pdf files using a natural |
| 18 | + language model to interpret and answer prompts. |
| 19 | +
|
| 20 | + Attributes: |
| 21 | + prompt (str): The prompt for the graph. |
| 22 | + source (str): The source of the graph. |
| 23 | + config (dict): Configuration parameters for the graph. |
| 24 | + llm_model: An instance of a language model client, configured for generating answers. |
| 25 | + embedder_model: An instance of an embedding model client, |
| 26 | + configured for generating embeddings. |
| 27 | + verbose (bool): A flag indicating whether to show print statements during execution. |
| 28 | + headless (bool): A flag indicating whether to run the graph in headless mode. |
| 29 | + model_token (int): The token limit for the language model. |
| 30 | +
|
| 31 | + Args: |
| 32 | + prompt (str): The prompt for the graph. |
| 33 | + source (str): The source of the graph. |
| 34 | + config (dict): Configuration parameters for the graph. |
| 35 | +
|
| 36 | + Example: |
| 37 | + >>> pdf_scraper = PDFScraperGraph( |
| 38 | + ... "List me all the attractions in Chioggia.", |
| 39 | + ... "data/chioggia.pdf", |
| 40 | + ... {"llm": {"model": "gpt-3.5-turbo"}} |
| 41 | + ... ) |
| 42 | + >>> result = pdf_scraper.run() |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, prompt: str, source: str, config: dict): |
| 46 | + super().__init__(prompt, config, source) |
| 47 | + |
| 48 | + self.input_key = "pdf" if source.endswith("pdf") else "pdf_dir" |
| 49 | + |
| 50 | + def _create_graph(self) -> BaseGraph: |
| 51 | + """ |
| 52 | + Creates the graph of nodes representing the workflow for web scraping. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + BaseGraph: A graph instance representing the web scraping workflow. |
| 56 | + """ |
| 57 | + |
| 58 | + fetch_node = FetchNode( |
| 59 | + input="pdf_dir", |
| 60 | + output=["doc"], |
| 61 | + node_config={ |
| 62 | + "headless": self.headless, |
| 63 | + "verbose": self.verbose |
| 64 | + } |
| 65 | + ) |
| 66 | + parse_node = ParseNode( |
| 67 | + input="doc", |
| 68 | + output=["parsed_doc"], |
| 69 | + node_config={ |
| 70 | + "chunk_size": self.model_token, |
| 71 | + "verbose": self.verbose |
| 72 | + } |
| 73 | + ) |
| 74 | + rag_node = RAGNode( |
| 75 | + input="user_prompt & (parsed_doc | doc)", |
| 76 | + output=["relevant_chunks"], |
| 77 | + node_config={ |
| 78 | + "llm": self.llm_model, |
| 79 | + "embedder_model": self.embedder_model, |
| 80 | + "verbose": self.verbose |
| 81 | + } |
| 82 | + ) |
| 83 | + generate_answer_node = GenerateAnswerNode( |
| 84 | + input="user_prompt & (relevant_chunks | parsed_doc | doc)", |
| 85 | + output=["answer"], |
| 86 | + node_config={ |
| 87 | + "llm": self.llm_model, |
| 88 | + "verbose": self.verbose |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + return BaseGraph( |
| 93 | + nodes=[ |
| 94 | + fetch_node, |
| 95 | + parse_node, |
| 96 | + rag_node, |
| 97 | + generate_answer_node, |
| 98 | + ], |
| 99 | + edges=[ |
| 100 | + (fetch_node, parse_node), |
| 101 | + (parse_node, rag_node), |
| 102 | + (rag_node, generate_answer_node) |
| 103 | + ], |
| 104 | + entry_point=fetch_node |
| 105 | + ) |
| 106 | + |
| 107 | + def run(self) -> str: |
| 108 | + """ |
| 109 | + Executes the web scraping process and returns the answer to the prompt. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + str: The answer to the prompt. |
| 113 | + """ |
| 114 | + |
| 115 | + inputs = {"user_prompt": self.prompt, self.input_key: self.source} |
| 116 | + self.final_state, self.execution_info = self.graph.execute(inputs) |
| 117 | + |
| 118 | + return self.final_state.get("answer", "No answer found.") |
0 commit comments