[3af7d7]: / aiagents4pharma / talk2scholars / tools / zotero / zotero_read.py

Download this file

76 lines (62 with data), 2.3 kB

 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
This tool is used to search for papers in Zotero library.
"""
import logging
from typing import Annotated, Any
from langchain_core.messages import ToolMessage
from langchain_core.tools import tool
from langchain_core.tools.base import InjectedToolCallId
from langgraph.types import Command
from pydantic import BaseModel, Field
from .utils.read_helper import ZoteroSearchData
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ZoteroSearchInput(BaseModel):
"""Input schema for the Zotero search tool."""
query: str = Field(
description="Search query string to find papers in Zotero library."
)
only_articles: bool = Field(
default=True,
description="Whether to only search for journal articles/conference papers.",
)
limit: int = Field(
default=2, description="Maximum number of results to return", ge=1, le=100
)
tool_call_id: Annotated[str, InjectedToolCallId]
@tool(args_schema=ZoteroSearchInput, parse_docstring=True)
def zotero_read(
query: str,
only_articles: bool,
tool_call_id: Annotated[str, InjectedToolCallId],
limit: int = 2,
) -> Command[Any]:
"""
Use this tool to search and retrieve papers from Zotero library.
Args:
query (str): The search query string to find papers.
tool_call_id (Annotated[str, InjectedToolCallId]): The tool call ID.
limit (int, optional): The maximum number of results to return. Defaults to 2.
Returns:
Dict[str, Any]: The search results and related information.
"""
# Create search data object to organize variables
search_data = ZoteroSearchData(query, only_articles, limit, tool_call_id)
# Process the search
search_data.process_search()
results = search_data.get_search_results()
return Command(
update={
"zotero_read": results["filtered_papers"],
"last_displayed_papers": "zotero_read",
"messages": [
ToolMessage(
content=results["content"],
tool_call_id=tool_call_id,
artifact=results["filtered_papers"],
)
],
}
)