aurelian 0.3.3__py3-none-any.whl → 0.3.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aurelian/agents/paperqa/__init__.py +27 -0
- aurelian/agents/paperqa/paperqa_agent.py +65 -0
- aurelian/agents/paperqa/paperqa_cli.py +305 -0
- aurelian/agents/paperqa/paperqa_config.py +142 -0
- aurelian/agents/paperqa/paperqa_gradio.py +90 -0
- aurelian/agents/paperqa/paperqa_mcp.py +155 -0
- aurelian/agents/paperqa/paperqa_tools.py +566 -0
- aurelian/cli.py +27 -0
- {aurelian-0.3.3.dist-info → aurelian-0.3.4.dist-info}/METADATA +3 -1
- {aurelian-0.3.3.dist-info → aurelian-0.3.4.dist-info}/RECORD +13 -6
- {aurelian-0.3.3.dist-info → aurelian-0.3.4.dist-info}/WHEEL +1 -1
- {aurelian-0.3.3.dist-info → aurelian-0.3.4.dist-info}/LICENSE +0 -0
- {aurelian-0.3.3.dist-info → aurelian-0.3.4.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
"""
|
2
|
+
PaperQA agent package for scientific literature search and analysis.
|
3
|
+
"""
|
4
|
+
|
5
|
+
# isort: skip_file
|
6
|
+
from .paperqa_agent import paperqa_agent # noqa: E402
|
7
|
+
from .paperqa_config import PaperQADependencies, get_config # noqa: E402
|
8
|
+
from .paperqa_gradio import chat # noqa: E402
|
9
|
+
from .paperqa_tools import ( # noqa: E402
|
10
|
+
search_papers,
|
11
|
+
query_papers,
|
12
|
+
add_paper,
|
13
|
+
add_papers,
|
14
|
+
list_papers,
|
15
|
+
)
|
16
|
+
|
17
|
+
__all__ = [
|
18
|
+
"paperqa_agent",
|
19
|
+
"PaperQADependencies",
|
20
|
+
"get_config",
|
21
|
+
"search_papers",
|
22
|
+
"query_papers",
|
23
|
+
"add_paper",
|
24
|
+
"add_papers",
|
25
|
+
"list_papers",
|
26
|
+
"chat",
|
27
|
+
]
|
@@ -0,0 +1,65 @@
|
|
1
|
+
"""
|
2
|
+
Agent for PaperQA integration with Aurelian.
|
3
|
+
"""
|
4
|
+
import logging
|
5
|
+
from pydantic_ai import Agent
|
6
|
+
|
7
|
+
paperqa_logger = logging.getLogger("aurelian.agents.paperqa")
|
8
|
+
paperqa_logger.setLevel(logging.INFO)
|
9
|
+
|
10
|
+
for handler in list(paperqa_logger.handlers):
|
11
|
+
paperqa_logger.removeHandler(handler)
|
12
|
+
|
13
|
+
console = logging.StreamHandler()
|
14
|
+
console.setLevel(logging.INFO)
|
15
|
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
16
|
+
console.setFormatter(formatter)
|
17
|
+
paperqa_logger.addHandler(console)
|
18
|
+
|
19
|
+
paperqa_logger.propagate = False
|
20
|
+
|
21
|
+
from .paperqa_config import PaperQADependencies
|
22
|
+
from .paperqa_tools import (
|
23
|
+
search_papers,
|
24
|
+
query_papers,
|
25
|
+
add_paper,
|
26
|
+
add_papers,
|
27
|
+
list_papers,
|
28
|
+
build_index
|
29
|
+
)
|
30
|
+
|
31
|
+
PAPERQA_SYSTEM_PROMPT = """
|
32
|
+
You are an AI assistant that helps explore scientific literature using PaperQA.
|
33
|
+
You can use different functions to search for papers and analyze them:
|
34
|
+
- `search_papers` to find papers by topic or keyword from outside this repository.
|
35
|
+
- `query_papers` to ask questions about the papers in the repository
|
36
|
+
- `add_paper` to add a specific paper by file path or URL (with auto_index=True by default)
|
37
|
+
- `add_papers` to add multiple papers from a directory (with auto_index=True by default)
|
38
|
+
- `list_papers` to see all papers in the collection
|
39
|
+
- `build_index` to manually rebuild the search index
|
40
|
+
|
41
|
+
When adding papers with `add_paper` or `add_papers`:
|
42
|
+
- For `add_paper`, the URL must be a direct link to a PDF (e.g., "https://example.com/paper.pdf")
|
43
|
+
- For `add_paper`, you can provide a citation string to attribute the source
|
44
|
+
- For `add_papers`, you provide a directory containing papers and an optional citation format
|
45
|
+
- By default, auto_index=True, which automatically rebuilds the index after adding papers
|
46
|
+
- You can set auto_index=False if you want to add multiple papers before indexing
|
47
|
+
- After adding papers with auto_index=False, use `build_index()` to make them searchable
|
48
|
+
|
49
|
+
When showing paper information, format using Markdown for readability.
|
50
|
+
When papers have been successfully retrieved, proceed to analyzing them.
|
51
|
+
"""
|
52
|
+
|
53
|
+
paperqa_agent = Agent(
|
54
|
+
model="openai:gpt-4o-2024-11-20",
|
55
|
+
deps_type=PaperQADependencies,
|
56
|
+
result_type=str,
|
57
|
+
system_prompt=PAPERQA_SYSTEM_PROMPT,
|
58
|
+
)
|
59
|
+
|
60
|
+
paperqa_agent.tool(search_papers)
|
61
|
+
paperqa_agent.tool(query_papers)
|
62
|
+
paperqa_agent.tool(add_paper)
|
63
|
+
paperqa_agent.tool(add_papers)
|
64
|
+
paperqa_agent.tool(list_papers)
|
65
|
+
paperqa_agent.tool(build_index)
|
@@ -0,0 +1,305 @@
|
|
1
|
+
"""
|
2
|
+
CLI commands for the PaperQA agent.
|
3
|
+
"""
|
4
|
+
import os
|
5
|
+
import asyncio
|
6
|
+
import logging
|
7
|
+
import sys
|
8
|
+
from pathlib import Path
|
9
|
+
import click
|
10
|
+
from paperqa import agent_query
|
11
|
+
|
12
|
+
from aurelian.agents.paperqa.paperqa_config import get_config
|
13
|
+
from paperqa.agents.search import get_directory_index
|
14
|
+
from paperqa.settings import IndexSettings
|
15
|
+
|
16
|
+
logger = logging.getLogger(__name__)
|
17
|
+
|
18
|
+
def setup_logging():
|
19
|
+
"""Set up logging for the PaperQA CLI."""
|
20
|
+
logging.basicConfig(
|
21
|
+
level=logging.INFO,
|
22
|
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
23
|
+
handlers=[logging.StreamHandler(sys.stdout)]
|
24
|
+
)
|
25
|
+
|
26
|
+
|
27
|
+
def check_api_key():
|
28
|
+
"""Check if the OpenAI API key is set.
|
29
|
+
|
30
|
+
Returns:
|
31
|
+
bool: True if key is set, False otherwise
|
32
|
+
"""
|
33
|
+
if not os.environ.get("OPENAI_API_KEY"):
|
34
|
+
logger.error("OPENAI_API_KEY environment variable must be set.")
|
35
|
+
click.echo("Error: OPENAI_API_KEY environment variable must be set.")
|
36
|
+
return False
|
37
|
+
return True
|
38
|
+
|
39
|
+
|
40
|
+
def setup_and_configure_paper_directory(directory):
|
41
|
+
"""
|
42
|
+
Setup and configure a paper directory with proper paths.
|
43
|
+
|
44
|
+
Args:
|
45
|
+
directory: Input directory path (can be relative)
|
46
|
+
|
47
|
+
Returns:
|
48
|
+
tuple: (resolved_path, settings, config) tuple with properly configured settings
|
49
|
+
"""
|
50
|
+
directory = str(Path(directory).resolve())
|
51
|
+
|
52
|
+
config = get_config()
|
53
|
+
config.paper_directory = directory
|
54
|
+
|
55
|
+
os.environ["PQA_HOME"] = directory
|
56
|
+
|
57
|
+
if not os.path.exists(directory):
|
58
|
+
logger.info(f"Creating paper directory: {directory}")
|
59
|
+
os.makedirs(directory, exist_ok=True)
|
60
|
+
|
61
|
+
settings = config.set_paperqa_settings()
|
62
|
+
settings.agent.index = IndexSettings(
|
63
|
+
name=config.index_name,
|
64
|
+
paper_directory=directory,
|
65
|
+
recurse_subdirectories=False
|
66
|
+
)
|
67
|
+
|
68
|
+
return directory, settings, config
|
69
|
+
|
70
|
+
|
71
|
+
def get_document_files(directory):
|
72
|
+
"""
|
73
|
+
Get all indexable document files in the given directory.
|
74
|
+
|
75
|
+
Args:
|
76
|
+
directory: Directory to search for document files
|
77
|
+
|
78
|
+
Returns:
|
79
|
+
dict: Dictionary with file lists by type and a combined list
|
80
|
+
"""
|
81
|
+
document_extensions = ['.pdf', '.txt', '.html', '.md']
|
82
|
+
all_files = [f for f in os.listdir(directory)
|
83
|
+
if any(f.lower().endswith(ext) for ext in document_extensions)]
|
84
|
+
|
85
|
+
return {
|
86
|
+
'all': all_files,
|
87
|
+
'pdf': [f for f in all_files if f.lower().endswith('.pdf')],
|
88
|
+
'txt': [f for f in all_files if f.lower().endswith('.txt')],
|
89
|
+
'html': [f for f in all_files if f.lower().endswith('.html')],
|
90
|
+
'md': [f for f in all_files if f.lower().endswith('.md')],
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
@click.group(name="paperqa")
|
95
|
+
@click.option("-v", "--verbose", count=True, help="Increase verbosity level (-v for INFO, -vv for DEBUG)")
|
96
|
+
@click.option("-q", "--quiet", is_flag=True, help="Suppress non-error output")
|
97
|
+
def paperqa_cli(verbose, quiet):
|
98
|
+
"""PaperQA management commands for indexing and querying documents.
|
99
|
+
|
100
|
+
PaperQA supports PDF, TXT, HTML, and Markdown files in all operations.
|
101
|
+
|
102
|
+
Examples:
|
103
|
+
# Index documents in a directory
|
104
|
+
aurelian paperqa index -d /path/to/papers
|
105
|
+
|
106
|
+
# Ask a question about indexed papers
|
107
|
+
aurelian paperqa ask "What is the role of tau protein in Alzheimer's?" -d /path/to/papers
|
108
|
+
|
109
|
+
# List indexed papers
|
110
|
+
aurelian paperqa list -d /path/to/papers
|
111
|
+
|
112
|
+
# Run with increased verbosity
|
113
|
+
aurelian paperqa --verbose index -d /path/to/papers
|
114
|
+
|
115
|
+
# Add documents through the agent
|
116
|
+
# (Using these commands in chat modes like Gradio or MCP)
|
117
|
+
"Add the paper from /path/to/paper.pdf"
|
118
|
+
"Add all papers from the directory /path/to/papers/"
|
119
|
+
"""
|
120
|
+
setup_logging()
|
121
|
+
|
122
|
+
if verbose >= 2:
|
123
|
+
logging.getLogger("aurelian.agents.paperqa").setLevel(logging.DEBUG)
|
124
|
+
elif verbose == 1:
|
125
|
+
logging.getLogger("aurelian.agents.paperqa").setLevel(logging.INFO)
|
126
|
+
else:
|
127
|
+
logging.getLogger("aurelian.agents.paperqa").setLevel(logging.WARNING)
|
128
|
+
|
129
|
+
if quiet:
|
130
|
+
logging.getLogger("aurelian.agents.paperqa").setLevel(logging.ERROR)
|
131
|
+
|
132
|
+
|
133
|
+
@paperqa_cli.command()
|
134
|
+
@click.option(
|
135
|
+
"--directory", "-d",
|
136
|
+
required=True,
|
137
|
+
help="Paper directory containing PDF, TXT, HTML, and MD files to index",
|
138
|
+
)
|
139
|
+
def index(directory):
|
140
|
+
"""Index documents for search and querying.
|
141
|
+
|
142
|
+
This command scans the specified directory for documents (PDF, TXT, HTML, MD)
|
143
|
+
and creates a searchable index for them. The index is stored in the .pqa
|
144
|
+
subdirectory of the specified directory.
|
145
|
+
|
146
|
+
Example:
|
147
|
+
aurelian paperqa index -d ~/research/papers
|
148
|
+
"""
|
149
|
+
if not check_api_key():
|
150
|
+
return
|
151
|
+
|
152
|
+
paper_dir, settings, _ = setup_and_configure_paper_directory(directory)
|
153
|
+
|
154
|
+
docs = get_document_files(paper_dir)
|
155
|
+
|
156
|
+
if not docs['all']:
|
157
|
+
logger.warning(f"No indexable documents found in {paper_dir}")
|
158
|
+
click.echo(f"No indexable documents found in {paper_dir}")
|
159
|
+
return
|
160
|
+
|
161
|
+
# detailed breakdown
|
162
|
+
logger.info(f"Found {len(docs['all'])} documents in {paper_dir}:")
|
163
|
+
if docs['pdf']: logger.info(f" - {len(docs['pdf'])} PDF files")
|
164
|
+
if docs['txt']: logger.info(f" - {len(docs['txt'])} text files")
|
165
|
+
if docs['html']: logger.info(f" - {len(docs['html'])} HTML files")
|
166
|
+
if docs['md']: logger.info(f" - {len(docs['md'])} Markdown files")
|
167
|
+
logger.info(f"Index will be stored in: {paper_dir}/.pqa")
|
168
|
+
logger.info("Indexing papers... (this may take a while)")
|
169
|
+
|
170
|
+
async def run_index():
|
171
|
+
try:
|
172
|
+
index = await get_directory_index(
|
173
|
+
settings=settings,
|
174
|
+
build=True,
|
175
|
+
)
|
176
|
+
index_files = await index.index_files
|
177
|
+
logger.info(f"Success! Indexed {len(index_files)} document chunks from your PDF files.")
|
178
|
+
except Exception as e:
|
179
|
+
logger.error(f"Error indexing papers: {str(e)}")
|
180
|
+
|
181
|
+
try:
|
182
|
+
asyncio.run(run_index())
|
183
|
+
except Exception as e:
|
184
|
+
logger.error(f"Error: {str(e)}")
|
185
|
+
|
186
|
+
|
187
|
+
@paperqa_cli.command()
|
188
|
+
@click.argument("query", required=True)
|
189
|
+
@click.option(
|
190
|
+
"--directory", "-d",
|
191
|
+
required=True,
|
192
|
+
help="Paper directory containing indexed documents",
|
193
|
+
)
|
194
|
+
def ask(query, directory):
|
195
|
+
"""Ask a question about the indexed documents.
|
196
|
+
|
197
|
+
This command searches the indexed documents for information relevant to the
|
198
|
+
provided query and generates an AI-powered answer with references. Make sure
|
199
|
+
to run the 'index' command first to create an index.
|
200
|
+
|
201
|
+
Example:
|
202
|
+
aurelian paperqa ask "What are the key findings on tau proteins?" -d ~/research/papers
|
203
|
+
"""
|
204
|
+
if not check_api_key():
|
205
|
+
return
|
206
|
+
|
207
|
+
paper_dir, settings, _ = setup_and_configure_paper_directory(directory)
|
208
|
+
|
209
|
+
async def run_query():
|
210
|
+
try:
|
211
|
+
docs = get_document_files(paper_dir)
|
212
|
+
|
213
|
+
if not docs['all']:
|
214
|
+
logger.warning(f"No indexable documents found in {paper_dir}")
|
215
|
+
logger.info(f"Add documents (PDF, TXT, HTML, MD) to the directory and then run 'aurelian paperqa index -d {paper_dir}'")
|
216
|
+
return
|
217
|
+
|
218
|
+
try:
|
219
|
+
index = await get_directory_index(settings=settings, build=False)
|
220
|
+
index_files = await index.index_files
|
221
|
+
|
222
|
+
if not index_files:
|
223
|
+
logger.warning(f"No indexed papers found. PDF files exist but haven't been indexed.")
|
224
|
+
logger.info(f"Run 'aurelian paperqa index -d {paper_dir}' to index the papers.")
|
225
|
+
return
|
226
|
+
except Exception as e:
|
227
|
+
if "was empty, please rebuild it" in str(e):
|
228
|
+
logger.warning(f"Index is empty. Run 'aurelian paperqa index -d {paper_dir}' to index papers.")
|
229
|
+
return
|
230
|
+
raise
|
231
|
+
|
232
|
+
logger.info(f"Querying {len(index_files)} papers about: {query}")
|
233
|
+
logger.info("This may take a moment...")
|
234
|
+
|
235
|
+
response = await agent_query(
|
236
|
+
query=query,
|
237
|
+
settings=settings
|
238
|
+
)
|
239
|
+
|
240
|
+
click.echo(f"Answer: {response.session.answer}" +
|
241
|
+
f"\n\nReferences: {response.session.references}")
|
242
|
+
|
243
|
+
except Exception as e:
|
244
|
+
logger.error(f"Error querying papers: {str(e)}")
|
245
|
+
|
246
|
+
loop = asyncio.get_event_loop()
|
247
|
+
try:
|
248
|
+
loop.run_until_complete(run_query())
|
249
|
+
except Exception as e:
|
250
|
+
logger.error(f"Error: {str(e)}")
|
251
|
+
|
252
|
+
|
253
|
+
@paperqa_cli.command()
|
254
|
+
@click.option(
|
255
|
+
"--directory", "-d",
|
256
|
+
required=True,
|
257
|
+
help="Paper directory containing documents",
|
258
|
+
)
|
259
|
+
def list(directory):
|
260
|
+
"""List documents in the directory and their indexing status.
|
261
|
+
|
262
|
+
This command displays all documents (PDF, TXT, HTML, MD) in the specified
|
263
|
+
directory and shows which ones have been indexed. Use this to verify that
|
264
|
+
your documents are properly recognized and indexed.
|
265
|
+
|
266
|
+
Example:
|
267
|
+
aurelian paperqa list -d ~/research/papers
|
268
|
+
"""
|
269
|
+
if not check_api_key():
|
270
|
+
return
|
271
|
+
|
272
|
+
paper_dir, settings, _ = setup_and_configure_paper_directory(directory)
|
273
|
+
|
274
|
+
docs = get_document_files(paper_dir)
|
275
|
+
|
276
|
+
logger.info(f"Documents in directory {paper_dir}:")
|
277
|
+
for doc in docs['all']:
|
278
|
+
if doc.lower().endswith('.pdf'):
|
279
|
+
logger.info(f" - {doc} [PDF]")
|
280
|
+
elif doc.lower().endswith('.txt'):
|
281
|
+
logger.info(f" - {doc} [TXT]")
|
282
|
+
elif doc.lower().endswith('.html'):
|
283
|
+
logger.info(f" - {doc} [HTML]")
|
284
|
+
elif doc.lower().endswith('.md'):
|
285
|
+
logger.info(f" - {doc} [MD]")
|
286
|
+
|
287
|
+
async def list_indexed():
|
288
|
+
try:
|
289
|
+
index = await get_directory_index(settings=settings, build=False)
|
290
|
+
index_files = await index.index_files
|
291
|
+
if index_files:
|
292
|
+
logger.info(f"Indexed papers ({len(index_files)}):")
|
293
|
+
for file in index_files:
|
294
|
+
logger.info(f" - {file}")
|
295
|
+
else:
|
296
|
+
logger.warning(f"No indexed papers found. Run 'aurelian paperqa index -d {paper_dir}' to index papers.")
|
297
|
+
except Exception as e:
|
298
|
+
logger.error(f"Error accessing index: {str(e)}")
|
299
|
+
logger.info(f"Run 'aurelian paperqa index -d {paper_dir}' to create or rebuild the index.")
|
300
|
+
|
301
|
+
loop = asyncio.get_event_loop()
|
302
|
+
try:
|
303
|
+
loop.run_until_complete(list_indexed())
|
304
|
+
except Exception as e:
|
305
|
+
logger.error(f"Error: {str(e)}")
|
@@ -0,0 +1,142 @@
|
|
1
|
+
"""
|
2
|
+
Configuration for the PaperQA agent.
|
3
|
+
"""
|
4
|
+
from dataclasses import dataclass, field
|
5
|
+
import os
|
6
|
+
from typing import Optional, List
|
7
|
+
|
8
|
+
from paperqa import Settings as PQASettings
|
9
|
+
from paperqa.settings import (
|
10
|
+
AnswerSettings,
|
11
|
+
ParsingSettings,
|
12
|
+
PromptSettings,
|
13
|
+
AgentSettings,
|
14
|
+
IndexSettings, Settings,
|
15
|
+
)
|
16
|
+
|
17
|
+
from aurelian.dependencies.workdir import HasWorkdir, WorkDir
|
18
|
+
|
19
|
+
|
20
|
+
@dataclass
|
21
|
+
class PaperQADependencies(HasWorkdir):
|
22
|
+
"""Configuration for the PaperQA agent."""
|
23
|
+
|
24
|
+
paper_directory: str = field(
|
25
|
+
default_factory=lambda: os.getcwd(),
|
26
|
+
metadata={"description": "Directory containing papers to be searched"}
|
27
|
+
)
|
28
|
+
index_name: Optional[str] = field(
|
29
|
+
default=None,
|
30
|
+
metadata={"description": "Optional name for the search index. If None, it will be generated based on settings."}
|
31
|
+
)
|
32
|
+
|
33
|
+
llm: str = field(
|
34
|
+
default="gpt-4.1-2025-04-14",
|
35
|
+
metadata={"description": "LLM to use for queries and answer generation. Default is gpt-4.1-2025-04-14."}
|
36
|
+
)
|
37
|
+
summary_llm: str = field(
|
38
|
+
default="gpt-4.1-2025-04-14",
|
39
|
+
metadata={"description": "LLM to use for summarization. Default is gpt-4.1-2025-04-14."}
|
40
|
+
)
|
41
|
+
embedding: str = field(
|
42
|
+
default="text-embedding-3-small",
|
43
|
+
metadata={"description": "Embedding model to use. Default is text-embedding-3-small."}
|
44
|
+
)
|
45
|
+
temperature: float = field(
|
46
|
+
default=0.1,
|
47
|
+
metadata={"description": "Temperature for LLM generation. Default is 0.1."}
|
48
|
+
)
|
49
|
+
|
50
|
+
search_count: int = field(
|
51
|
+
default=8,
|
52
|
+
metadata={"description": "Number of papers to retrieve in searches. Default is 8."}
|
53
|
+
)
|
54
|
+
|
55
|
+
evidence_k: int = field(
|
56
|
+
default=10,
|
57
|
+
metadata={"description": "Number of evidence pieces to retrieve. Default is 10."}
|
58
|
+
)
|
59
|
+
answer_max_sources: int = field(
|
60
|
+
default=5,
|
61
|
+
metadata={"description": "Maximum number of sources to use in answers. Default is 5."}
|
62
|
+
)
|
63
|
+
max_concurrent_requests: int = field(
|
64
|
+
default=4,
|
65
|
+
metadata={"description": "Maximum number of concurrent requests to LLMs. Default is 4."}
|
66
|
+
)
|
67
|
+
|
68
|
+
chunk_size: int = field(
|
69
|
+
default=5000,
|
70
|
+
metadata={"description": "Size of document chunks for embedding. Default is 5000."}
|
71
|
+
)
|
72
|
+
overlap: int = field(
|
73
|
+
default=250,
|
74
|
+
metadata={"description": "Overlap between chunks. Default is 250."}
|
75
|
+
)
|
76
|
+
|
77
|
+
workdir: Optional[WorkDir] = None
|
78
|
+
|
79
|
+
def __post_init__(self):
|
80
|
+
"""Initialize the config with default values."""
|
81
|
+
if self.workdir is None:
|
82
|
+
self.workdir = WorkDir()
|
83
|
+
|
84
|
+
def set_paperqa_settings(self) -> PQASettings:
|
85
|
+
"""
|
86
|
+
Convert to PaperQA Settings object.
|
87
|
+
|
88
|
+
This allows users to customize all PaperQA settings through the dependencies object.
|
89
|
+
Any changes to the dependencies will be reflected in the returned Settings object.
|
90
|
+
"""
|
91
|
+
return PQASettings(
|
92
|
+
llm=self.llm,
|
93
|
+
summary_llm=self.summary_llm,
|
94
|
+
embedding=self.embedding,
|
95
|
+
temperature=self.temperature,
|
96
|
+
|
97
|
+
answer=AnswerSettings(
|
98
|
+
evidence_k=self.evidence_k,
|
99
|
+
answer_max_sources=self.answer_max_sources,
|
100
|
+
max_concurrent_requests=self.max_concurrent_requests,
|
101
|
+
),
|
102
|
+
|
103
|
+
parsing=ParsingSettings(
|
104
|
+
chunk_size=self.chunk_size,
|
105
|
+
overlap=self.overlap,
|
106
|
+
),
|
107
|
+
|
108
|
+
agent=AgentSettings(
|
109
|
+
agent_llm=self.llm,
|
110
|
+
search_count=self.search_count,
|
111
|
+
index=IndexSettings(
|
112
|
+
name=self.index_name,
|
113
|
+
paper_directory=self.paper_directory,
|
114
|
+
recurse_subdirectories=False,
|
115
|
+
),
|
116
|
+
),
|
117
|
+
)
|
118
|
+
|
119
|
+
|
120
|
+
def get_config() -> PaperQADependencies:
|
121
|
+
"""
|
122
|
+
Get the PaperQA configuration from environment variables or defaults.
|
123
|
+
|
124
|
+
Returns:
|
125
|
+
A PaperQADependencies instance with default settings.
|
126
|
+
|
127
|
+
Note:
|
128
|
+
Users can modify the returned object to customize settings.
|
129
|
+
Example:
|
130
|
+
```python
|
131
|
+
deps = get_config()
|
132
|
+
deps.llm = "claude-3-sonnet-20240229" # Use Claude instead of default GPT-4
|
133
|
+
deps.temperature = 0.5 # Increase temperature
|
134
|
+
deps.evidence_k = 15 # Retrieve more evidence
|
135
|
+
```
|
136
|
+
"""
|
137
|
+
workdir_path = os.environ.get("AURELIAN_WORKDIR", None)
|
138
|
+
workdir = WorkDir(location=workdir_path) if workdir_path else None
|
139
|
+
|
140
|
+
return PaperQADependencies(
|
141
|
+
workdir=workdir,
|
142
|
+
)
|
@@ -0,0 +1,90 @@
|
|
1
|
+
"""
|
2
|
+
Gradio interface for the PaperQA agent.
|
3
|
+
"""
|
4
|
+
import os
|
5
|
+
import logging
|
6
|
+
from typing import List, Optional, Any
|
7
|
+
|
8
|
+
import gradio as gr
|
9
|
+
|
10
|
+
from aurelian.utils.async_utils import run_sync
|
11
|
+
from .paperqa_agent import paperqa_agent
|
12
|
+
from .paperqa_config import PaperQADependencies, get_config
|
13
|
+
|
14
|
+
logger = logging.getLogger(__name__)
|
15
|
+
|
16
|
+
|
17
|
+
async def get_info(query: str, history: List[str], deps: PaperQADependencies, **kwargs) -> str:
|
18
|
+
"""
|
19
|
+
Process a query using the PaperQA agent.
|
20
|
+
|
21
|
+
Args:
|
22
|
+
query: The user query
|
23
|
+
history: The conversation history
|
24
|
+
deps: The dependencies for the agent
|
25
|
+
model: Optional model override
|
26
|
+
|
27
|
+
Returns:
|
28
|
+
The agent's response
|
29
|
+
"""
|
30
|
+
logger.info(f"QUERY: {query}")
|
31
|
+
logger.debug(f"HISTORY: {history}")
|
32
|
+
|
33
|
+
if history:
|
34
|
+
query += "\n\n## Previous Conversation:\n"
|
35
|
+
for h in history:
|
36
|
+
query += f"\n{h}"
|
37
|
+
|
38
|
+
result = await paperqa_agent.run(query, deps=deps, **kwargs)
|
39
|
+
return result.data
|
40
|
+
|
41
|
+
|
42
|
+
def chat(deps: Optional[PaperQADependencies] = None, model=None, **kwargs):
|
43
|
+
"""
|
44
|
+
Create a Gradio chat interface for the PaperQA agent.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
deps: Optional dependencies configuration
|
48
|
+
model: Optional model override
|
49
|
+
kwargs: Additional keyword arguments for dependencies
|
50
|
+
|
51
|
+
Returns:
|
52
|
+
A Gradio ChatInterface
|
53
|
+
"""
|
54
|
+
if deps is None:
|
55
|
+
deps = get_config()
|
56
|
+
|
57
|
+
for key, value in kwargs.items():
|
58
|
+
if hasattr(deps, key):
|
59
|
+
setattr(deps, key, value)
|
60
|
+
|
61
|
+
paper_dir = os.path.join(os.getcwd(), "pqa_source")
|
62
|
+
os.makedirs(paper_dir, exist_ok=True)
|
63
|
+
deps.paper_directory = paper_dir
|
64
|
+
os.environ["PQA_HOME"] = paper_dir
|
65
|
+
print(f"Using dedicated papers directory at: {paper_dir}")
|
66
|
+
|
67
|
+
def get_info_wrapper(query: str, history: List[str]) -> str:
|
68
|
+
"""Wrapper for the async get_info function."""
|
69
|
+
import asyncio
|
70
|
+
return asyncio.run(get_info(query, history, deps, **kwargs))
|
71
|
+
|
72
|
+
return gr.ChatInterface(
|
73
|
+
fn=get_info_wrapper,
|
74
|
+
type="messages",
|
75
|
+
title="PaperQA AI Assistant",
|
76
|
+
description="""This assistant helps you search and analyze scientific papers. You can:
|
77
|
+
- Search for papers on a topic
|
78
|
+
- Ask questions about the papers in the repository
|
79
|
+
- Add specific papers by path or URL: (if paths, use absolute paths!)"
|
80
|
+
- Add multiple papers from a directory: (use absolute paths!)"
|
81
|
+
- List all papers in the collection
|
82
|
+
|
83
|
+
Supported document types: PDF, TXT, HTML, and Markdown files""",
|
84
|
+
examples=[
|
85
|
+
["Search for papers on CRISPR gene editing"],
|
86
|
+
["What are the main challenges in CRISPR gene editing?"],
|
87
|
+
["What is the relationship between CRISPR and Cas9?"],
|
88
|
+
["List all the indexed papers"],
|
89
|
+
],
|
90
|
+
)
|