File size: 2,303 Bytes
fd49a93
ae11f2d
fd49a93
ae11f2d
fd49a93
9c3c25d
 
dae7f12
fd49a93
 
 
ae11f2d
9c3c25d
fd49a93
 
 
 
 
 
dae7f12
 
9c3c25d
dae7f12
 
 
 
 
 
 
 
9c3c25d
 
dae7f12
9c3c25d
dae7f12
9c3c25d
dae7f12
9c3c25d
 
 
dae7f12
9c3c25d
 
 
 
dae7f12
9c3c25d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import List
from models import Question, FinalAnswer
from document_manager import DocumentManager
from retriever import VectorStoreManager
from workflow import RAGWorkflow
from rich import print as rprint
from rich.panel import Panel
import time

class QueryService:
    def __init__(self):
        self.workflow = RAGWorkflow()
        rprint(Panel("[bold green]QueryService Initialized[/bold green]", subtitle="Ready to process requests.", border_style="green"))

    def process_queries(
        self,
        document_url: str,
        questions: List[Question]
    ) -> List[FinalAnswer]:
        
        start_time = time.time()
        try:
            rprint(Panel(f"Fetching document from URL:", title="[yellow]Step 1: Document Manager[/yellow]", border_style="yellow"))
            print(f'URL: \n{document_url}')
            print(f'Questions:')
            print('{')
            for question in questions:
                print(question.question, end=",\n")
            print('}')

            doc_manager = DocumentManager(document_url)
            file_path = doc_manager.get_filepath()

            rprint(f"[green]Document located at:[/] {file_path}")
            rprint(f"[blue]Elapsed Time: {(time.time()-start_time):.2f}")

            rprint(Panel("Initializing vector store and retriever...", title="[yellow]Step 2: Vector Store Manager[/yellow]", border_style="yellow"))
            store_manager = VectorStoreManager(file_path)
            retriever = store_manager.retriever
            rprint(f"[green]Retriever created successfully. Indexed [bold]{len(store_manager.documents)}[/bold] chunks.[/green]")
            rprint(f"[blue]Elapsed Time: {(time.time()-start_time):.2f}")

            rprint(Panel(f"Invoking RAG workflow for {len(questions)} questions...", title="[yellow]Step 3: RAG Workflow[/yellow]", border_style="yellow"))
            results = self.workflow.invoke(questions, retriever)
            rprint("[bold green]Workflow finished successfully.[/bold green]")
            rprint(f"[blue]Elapsed Time: {(time.time()-start_time):.2f}")
            
            return results

        except Exception as e:
            rprint(Panel(f"[bold red]An error occurred during query processing:[/] {e}", title="[red]FATAL ERROR[/red]"))
            raise