Agent Zero AI framework
Enhance document_query to support multiple documents
linuztx63b98451821e
diff --git a/prompts/agent.system.tool.document_query.md b/prompts/agent.system.tool.document_query.mdindex e6ca44432..9cbc51db3 100644--- a/prompts/agent.system.tool.document_query.md+++ b/prompts/agent.system.tool.document_query.md@@ -1,60 +1,62 @@-### document_query:-This tool can be used to read or analyze remote and local documents.-It can be used to:- * Get webpage or remote document text content- * Get local document text content- * Answer queries about a webpage, remote or local document-By default, when the "queries" argument is empty, this tool returns the text content of the document retrieved using OCR.-Additionally, you can pass a list of "queries" - in this case, the tool returns the answers to all the passed queries about the document.-!!! This is a universal document reader qnd query tool-!!! Supported document formats: HTML, PDF, Office Documents (word,excel, powerpoint), Textfiles and many more.+### document_query+read and analyze remote/local documents get text content or answer questions+pass a single url/path or a list for multiple documents in "document"+for web documents use "http://" or "https://"" prefix+for local files "file://" prefix is optional but full path is required+if "queries" is empty tool returns document content+if "queries" is a list of strings tool returns answers+supports various formats HTML PDF Office Text etc+usage:-#### Arguments:- * "document" (string) : The web address or local path to the document in question. Webdocuments need "http://" or "https://" protocol prefix. For local files the "file:" protocol prefix is optional. Local files MUST be passed with full filesystem path.- * "queries" (Optional, list[str]) : Optionally, here you can pass one or more queries to be answered (using and/or about) the document--#### Usage example 1:-##### Request:-```json+1 get content+~~~json{"thoughts": [- "...",+ "I need to read..."],- "headline": "Reading web document content",+ "headline": "...","tool_name": "document_query","tool_args": {- "document": "https://...somexample",+ "document": "https://.../document"}}-```-##### Response:-```plaintext-... Here is the entire content of the web document requested ...-```+~~~-#### Usage example 2:-##### Request:-```json+2 query document+~~~json{"thoughts": [- "...",+ "I need to answer..."],- "headline": "Analyzing document to answer specific questions",+ "headline": "...","tool_name": "document_query","tool_args": {- "document": "https://...somexample",+ "document": "https://.../document","queries": [- "What is the topic?",- "Who is the audience?"+ "What is...",+ "Who is..."]}}-```-##### Response:-```plaintext-# What is the topic?-... Description of the document topic ...+~~~-# Who is the audience?-... The intended document audience list with short descriptions ...-```+3 query multiple documents+~~~json+{+ "thoughts": [+ "I need to compare..."+ ],+ "headline": "...",+ "tool_name": "document_query",+ "tool_args": {+ "document": [+ "https://.../document-one",+ "file:///path/to/document-two"+ ],+ "queries": [+ "Compare the main conclusions...",+ "What are the key differences..."+ ]+ }+}+~~~diff --git a/python/helpers/document_query.py b/python/helpers/document_query.pyindex d60a86574..b6bca9b1f 100644--- a/python/helpers/document_query.py+++ b/python/helpers/document_query.py@@ -361,12 +361,16 @@ class DocumentQueryHelper:self.progress_callback = progress_callback or (lambda x: None)async def document_qa(- self, document_uri: str, questions: Sequence[str]+ self, document_uris: List[str], questions: Sequence[str]) -> Tuple[bool, str]:- self.progress_callback(f"Starting Q&A process")+ self.progress_callback(+ f"Starting Q&A process for {len(document_uris)} documents"+ )- # index document- _ = await self.document_get_content(document_uri, True)+ # index documents+ await asyncio.gather(+ *[self.document_get_content(uri, True) for uri in document_uris]+ )selected_chunks = {}for question in questions:self.progress_callback(f"Optimizing query: {question}")@@ -381,14 +385,18 @@ class DocumentQueryHelper:)).strip()- self.progress_callback(f"Searching document with query: {optimized_query}")+ self.progress_callback(f"Searching documents with query: {optimized_query}")++ normalized_uris = [self.store.normalize_uri(uri) for uri in document_uris]+ doc_filter = " or ".join(+ [f"document_uri == '{uri}'" for uri in normalized_uris]+ )- normalized_uri = self.store.normalize_uri(document_uri)- chunks = await self.store.search_document(- document_uri=normalized_uri,+ chunks = await self.store.search_documents(query=optimized_query,limit=100,threshold=DEFAULT_SEARCH_THRESHOLD,+ filter=doc_filter,)self.progress_callback(f"Found {len(chunks)} chunks")@@ -397,8 +405,8 @@ class DocumentQueryHelper:selected_chunks[chunk.metadata["id"]] = chunkif not selected_chunks:- self.progress_callback(f"No relevant content found in the document")- content = f"!!! No content found for document: {document_uri} matching queries: {json.dumps(questions)}"+ self.progress_callback("No relevant content found in the documents")+ content = f"!!! No content found for documents: {json.dumps(document_uris)} matching queries: {json.dumps(questions)}"return False, contentself.progress_callback(diff --git a/python/tools/document_query.py b/python/tools/document_query.pyindex 6fca9271a..a05d0f748 100644--- a/python/tools/document_query.py+++ b/python/tools/document_query.py@@ -1,3 +1,5 @@+import asyncio+from python.helpers.tool import Tool, Responsefrom python.helpers.document_query import DocumentQueryHelper@@ -5,10 +7,24 @@ from python.helpers.document_query import DocumentQueryHelperclass DocumentQueryTool(Tool):async def execute(self, **kwargs):- document_uri = kwargs["document"] or None- queries = kwargs["queries"] if "queries" in kwargs else [kwargs["query"]] if ("query" in kwargs and kwargs["query"]) else []- if not isinstance(document_uri, str) or not document_uri:+ document_uri = kwargs.get("document")+ document_uris = []++ if isinstance(document_uri, list):+ document_uris = document_uri+ elif isinstance(document_uri, str):+ document_uris = [document_uri]++ if not document_uris:return Response(message="Error: no document provided", break_loop=False)++ queries = (+ kwargs["queries"]+ if "queries" in kwargs+ else [kwargs["query"]]+ if ("query" in kwargs and kwargs["query"])+ else []+ )try:progress = []@@ -20,9 +36,12 @@ class DocumentQueryTool(Tool):helper = DocumentQueryHelper(self.agent, progress_callback)if not queries:- content = await helper.document_get_content(document_uri)+ contents = await asyncio.gather(+ *[helper.document_get_content(uri) for uri in document_uris]+ )+ content = "\n\n---\n\n".join(contents)else:- _, content = await helper.document_qa(document_uri, queries)+ _, content = await helper.document_qa(document_uris, queries)return Response(message=content, break_loop=False)except Exception as e: # pylint: disable=broad-exception-caughtreturn Response(message=f"Error processing document: {e}", break_loop=False)