-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_research_assistant.py
More file actions
1063 lines (841 loc) Β· 47.2 KB
/
enhanced_research_assistant.py
File metadata and controls
1063 lines (841 loc) Β· 47.2 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Enhanced Research Assistant with PDF Processing and Google Scholar Integration
"""
import asyncio
import os
import re
import requests
from pathlib import Path
from typing import List, Dict, Any, Optional
import json
import tempfile
import aiofiles
import xml.etree.ElementTree as ET
from urllib.parse import quote_plus
from datetime import datetime
# PDF processing
import PyPDF2
import pdfplumber
# Web scraping
from bs4 import BeautifulSoup
import time
# AutoGen imports
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
class PaperProcessor:
"""Handles different types of paper inputs"""
@staticmethod
async def extract_text_from_pdf(pdf_path: str) -> str:
"""Extract text from PDF file"""
try:
text = ""
with open(pdf_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
# If PyPDF2 fails, try pdfplumber
if len(text.strip()) < 100:
with pdfplumber.open(pdf_path) as pdf:
text = ""
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
return text.strip()
except Exception as e:
return f"Error extracting PDF text: {str(e)}"
@staticmethod
async def download_arxiv_paper(arxiv_url: str) -> tuple[str, str]:
"""Download arXiv paper and extract text"""
try:
# Extract arXiv ID
arxiv_id_match = re.search(r'(\d+\.\d+)', arxiv_url)
if not arxiv_id_match:
return "", "Invalid arXiv URL format"
arxiv_id = arxiv_id_match.group(1)
# Get paper metadata from arXiv API
api_url = f"http://export.arxiv.org/api/query?id_list={arxiv_id}"
response = requests.get(api_url)
metadata = ""
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'xml')
entry = soup.find('entry')
if entry:
title = entry.find('title').text.strip() if entry.find('title') else "Unknown"
authors = [author.find('name').text for author in entry.find_all('author')]
summary = entry.find('summary').text.strip() if entry.find('summary') else ""
metadata = f"Title: {title}\nAuthors: {', '.join(authors)}\nAbstract: {summary}\n\n"
# Download PDF
pdf_url = f"https://arxiv.org/pdf/{arxiv_id}.pdf"
pdf_response = requests.get(pdf_url)
pdf_response.raise_for_status()
# Save to temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
tmp_file.write(pdf_response.content)
pdf_path = tmp_file.name
# Extract text
pdf_text = await PaperProcessor.extract_text_from_pdf(pdf_path)
# Clean up
os.unlink(pdf_path)
return metadata + pdf_text, ""
except Exception as e:
return "", f"Error processing arXiv paper: {str(e)}"
@staticmethod
async def process_latex_content(latex_content: str) -> str:
"""Process LaTeX content and extract readable text"""
# Remove common LaTeX commands
text = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', latex_content)
text = re.sub(r'\\[a-zA-Z]+', '', text)
text = re.sub(r'\{|\}', '', text)
text = re.sub(r'%.*', '', text) # Remove comments
text = re.sub(r'\n\s*\n', '\n\n', text) # Clean up whitespace
return text.strip()
@staticmethod
async def process_paper(paper_input: str) -> dict:
"""Process different types of paper inputs and return structured data"""
try:
paper_data = {
'input': paper_input,
'type': '',
'content': '',
'metadata': {}
}
# Determine input type and process accordingly
if paper_input.startswith('http') and 'arxiv.org' in paper_input:
# arXiv URL
paper_data['type'] = 'arxiv_url'
content, error = await PaperProcessor.download_arxiv_paper(paper_input)
if error:
paper_data['error'] = error
return paper_data
paper_data['content'] = content
elif paper_input.endswith('.pdf') and os.path.exists(paper_input):
# PDF file path
paper_data['type'] = 'pdf_file'
content = await PaperProcessor.extract_text_from_pdf(paper_input)
paper_data['content'] = content
paper_data['metadata']['file_path'] = paper_input
elif '\\documentclass' in paper_input or '\\begin{document}' in paper_input:
# LaTeX content
paper_data['type'] = 'latex_content'
content = await PaperProcessor.process_latex_content(paper_input)
paper_data['content'] = content
else:
# Plain text content
paper_data['type'] = 'plain_text'
paper_data['content'] = paper_input
return paper_data
except Exception as e:
return {
'input': paper_input,
'type': 'error',
'content': '',
'error': str(e)
}
class ArxivSearcher:
"""Handles arXiv API searches"""
@staticmethod
async def search_arxiv(query: str, num_results: int = 10) -> List[Dict[str, str]]:
"""Search arXiv for papers using the official API"""
try:
# arXiv API endpoint
base_url = "http://export.arxiv.org/api/query?"
search_query = quote_plus(query)
url = f"{base_url}search_query=all:{search_query}&start=0&max_results={num_results}&sortBy=relevance&sortOrder=descending"
response = requests.get(url)
response.raise_for_status()
# Parse XML response
root = ET.fromstring(response.content)
# Define namespace
ns = {'atom': 'http://www.w3.org/2005/Atom',
'arxiv': 'http://arxiv.org/schemas/atom'}
results = []
for entry in root.findall('atom:entry', ns):
title = entry.find('atom:title', ns).text.strip().replace('\n', ' ')
# Get arXiv ID and create links
arxiv_id = entry.find('atom:id', ns).text.split('/')[-1]
arxiv_url = f"https://arxiv.org/abs/{arxiv_id}"
pdf_url = f"https://arxiv.org/pdf/{arxiv_id}.pdf"
# Get authors
authors = []
for author in entry.findall('atom:author', ns):
name = author.find('atom:name', ns).text
authors.append(name)
authors_str = ", ".join(authors)
# Get abstract
summary = entry.find('atom:summary', ns).text.strip().replace('\n', ' ')
# Get published date
published = entry.find('atom:published', ns).text[:10] # YYYY-MM-DD
# Get categories
categories = []
for category in entry.findall('atom:category', ns):
categories.append(category.get('term'))
categories_str = ", ".join(categories)
results.append({
'title': title,
'arxiv_id': arxiv_id,
'arxiv_url': arxiv_url,
'pdf_url': pdf_url,
'authors': authors_str,
'abstract': summary[:300] + "..." if len(summary) > 300 else summary,
'published': published,
'categories': categories_str
})
return results
except Exception as e:
print(f"Error searching arXiv: {str(e)}")
return []
class ScholarSearcher:
"""Handles Google Scholar searches"""
@staticmethod
async def search_google_scholar(query: str, num_results: int = 10) -> List[Dict[str, str]]:
"""Search Google Scholar for papers"""
try:
# Simple web scraping approach (in production, use scholarly library or API)
search_url = f"https://scholar.google.com/scholar?q={query.replace(' ', '+')}&hl=en"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
response = requests.get(search_url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
results = []
for result in soup.find_all('div', class_='gs_r gs_or gs_scl')[:num_results]:
title_elem = result.find('h3', class_='gs_rt')
if title_elem:
title = title_elem.get_text()
link = title_elem.find('a')['href'] if title_elem.find('a') else ""
# Extract authors and venue
meta_elem = result.find('div', class_='gs_a')
meta_text = meta_elem.get_text() if meta_elem else ""
# Extract snippet
snippet_elem = result.find('div', class_='gs_rs')
snippet = snippet_elem.get_text() if snippet_elem else ""
results.append({
'title': title,
'link': link,
'meta': meta_text,
'snippet': snippet
})
return results
except Exception as e:
print(f"Error searching Google Scholar: {str(e)}")
return []
class EnhancedResearchAssistant:
def __init__(self, openai_api_key: str):
self.model_client = OpenAIChatCompletionClient(
model="gpt-4o",
api_key=openai_api_key
)
self.paper_processor = PaperProcessor()
self.scholar_searcher = ScholarSearcher()
self.arxiv_searcher = ArxivSearcher()
self.research_context = {}
self.context_file = "research_context.json"
self.setup_agents()
# Try to load existing context
self.load_context()
def _serialize_context_safely(self, max_length: int = 2000) -> str:
"""Safely serialize research context for JSON, handling non-serializable objects"""
try:
# Create a safe copy of the context with only serializable data
safe_context = {}
for key, value in self.research_context.items():
if isinstance(value, (str, int, float, bool, list, dict)):
safe_context[key] = value
else:
# Convert complex objects to string representation
safe_context[key] = str(value)
context_str = json.dumps(safe_context, indent=2)
return context_str[:max_length] if len(context_str) > max_length else context_str
except Exception as e:
# Fallback to string representation
return f"Research context summary: {str(self.research_context)[:max_length]}"
def format_arxiv_results(self, results: List[Dict[str, str]]) -> str:
"""Format arXiv search results with clickable links"""
if not results:
return "No papers found."
formatted_results = "π **Related Papers from arXiv:**\n\n"
for i, paper in enumerate(results, 1):
formatted_results += f"**{i}. {paper['title']}**\n"
formatted_results += f" π₯ Authors: {paper['authors']}\n"
formatted_results += f" π
Published: {paper['published']}\n"
formatted_results += f" π·οΈ Categories: {paper['categories']}\n"
formatted_results += f" π **Links:**\n"
formatted_results += f" β’ [View Abstract]({paper['arxiv_url']})\n"
formatted_results += f" β’ [Download PDF]({paper['pdf_url']})\n"
formatted_results += f" π Abstract: {paper['abstract']}\n\n"
return formatted_results
async def search_related_papers(self, query: str, num_results: int = 8) -> str:
"""Search for related papers and return formatted results with links"""
try:
print(f"π Searching arXiv for: {query}")
arxiv_results = await self.arxiv_searcher.search_arxiv(query, num_results)
if arxiv_results:
formatted_results = self.format_arxiv_results(arxiv_results)
# Store results in context for future reference
if 'related_papers' not in self.research_context:
self.research_context['related_papers'] = []
self.research_context['related_papers'].extend(arxiv_results)
return formatted_results
else:
return f"No papers found for query: {query}"
except Exception as e:
return f"Error searching for papers: {str(e)}"
def save_context(self, filename: str = None) -> str:
"""Save the current research context to a JSON file"""
try:
if filename is None:
filename = self.context_file
# Create a safe copy of the context for saving
safe_context = {}
for key, value in self.research_context.items():
if isinstance(value, (str, int, float, bool, list, dict)):
safe_context[key] = value
else:
# Convert non-serializable objects to string representation
safe_context[key] = str(value)
# Add metadata
safe_context['_metadata'] = {
'saved_at': datetime.now().isoformat(),
'version': '1.0'
}
# Save to file
with open(filename, 'w', encoding='utf-8') as f:
json.dump(safe_context, f, indent=2, ensure_ascii=False)
return f"β
Research context saved to {filename}"
except Exception as e:
return f"β Error saving context: {str(e)}"
def load_context(self, filename: str = None) -> str:
"""Load research context from a JSON file"""
try:
if filename is None:
filename = self.context_file
if not os.path.exists(filename):
return f"No saved context found at {filename}"
with open(filename, 'r', encoding='utf-8') as f:
loaded_context = json.load(f)
# Remove metadata before loading
if '_metadata' in loaded_context:
metadata = loaded_context.pop('_metadata')
saved_at = metadata.get('saved_at', 'unknown')
print(f"π Loading research context saved at {saved_at}")
# Merge with existing context (in case there's already some data)
self.research_context.update(loaded_context)
return f"β
Research context loaded from {filename}"
except Exception as e:
return f"β Error loading context: {str(e)}"
def get_context_summary(self) -> str:
"""Get a summary of the current research context"""
if not self.research_context:
return "No research context available."
summary = "π **Current Research Context Summary:**\n\n"
# Count different types of data
if 'processed_papers' in self.research_context:
papers_count = len(self.research_context['processed_papers'])
summary += f"π Processed Papers: {papers_count}\n"
if 'related_papers' in self.research_context:
related_count = len(self.research_context['related_papers'])
summary += f"π Related Papers Found: {related_count}\n"
if 'analysis_results' in self.research_context:
summary += f"π§ Analysis Results: Available\n"
if 'research_proposal' in self.research_context:
summary += f"π Research Proposal: Generated\n"
if 'literature_search' in self.research_context:
summary += f"π Literature Search: Completed\n"
# Show available keys
summary += f"\nποΈ Available Data: {', '.join(self.research_context.keys())}\n"
return summary
async def analyze_aspect(self, aspect: str) -> str:
"""Analyze a specific aspect of the research"""
try:
context_str = self._serialize_context_safely(3000)
analysis = await self.paper_analyzer.run(
task=f"Provide detailed analysis of this aspect: {aspect}\n\nResearch Context:\n{context_str}"
)
result = str(analysis.messages[-1].content) if hasattr(analysis, 'messages') and analysis.messages else str(analysis)
return result
except Exception as e:
return f"β Error analyzing aspect: {str(e)}"
async def get_research_summary(self) -> str:
"""Get a comprehensive research summary"""
try:
context_str = self._serialize_context_safely(4000)
summary = await self.research_synthesizer.run(
task=f"Provide a comprehensive summary of our research exploration so far.\n\nContext: {context_str}"
)
result = str(summary.messages[-1].content) if hasattr(summary, 'messages') and summary.messages else str(summary)
return result
except Exception as e:
return f"β Error generating summary: {str(e)}"
async def investigate_authors(self, author_name: str) -> str:
"""Investigate specific authors"""
try:
context_str = self._serialize_context_safely(2000)
investigation = await self.literature_searcher.run(
task=f"Investigate author: {author_name}. Find their key papers, research areas, and collaborations.\n\nContext: {context_str}"
)
result = str(investigation.messages[-1].content) if hasattr(investigation, 'messages') and investigation.messages else str(investigation)
return result
except Exception as e:
return f"β Error investigating authors: {str(e)}"
async def refine_proposal(self) -> str:
"""Refine the research proposal"""
try:
current_proposal = str(self.research_context.get('proposal', ''))
context_str = self._serialize_context_safely(2000)
refined = await self.proposal_writer.run(
task=f"Refine and improve this research proposal based on our current understanding:\n\nCurrent Proposal:\n{current_proposal}\n\nContext: {context_str}"
)
result = str(refined.messages[-1].content) if hasattr(refined, 'messages') and refined.messages else str(refined)
# Update the proposal in context
self.research_context['proposal'] = result
return result
except Exception as e:
return f"β Error refining proposal: {str(e)}"
async def explore_direction(self, direction: str) -> str:
"""Explore a specific research direction"""
try:
context_str = self._serialize_context_safely(2000)
exploration = await self.research_synthesizer.run(
task=f"Explore this research direction: {direction}\n\nProvide insights, potential approaches, and connections to our current research.\n\nContext: {context_str}"
)
result = str(exploration.messages[-1].content) if hasattr(exploration, 'messages') and exploration.messages else str(exploration)
return result
except Exception as e:
return f"β Error exploring direction: {str(e)}"
async def general_query(self, query: str) -> str:
"""Handle general research queries"""
try:
context_str = self._serialize_context_safely(2000)
response = await self.research_synthesizer.run(
task=f"Help with this research question: {query}\n\nContext: {context_str}"
)
result = str(response.messages[-1].content) if hasattr(response, 'messages') and response.messages else str(response)
return result
except Exception as e:
return f"β Error processing query: {str(e)}"
async def add_new_paper(self, paper_input: str) -> str:
"""Add and analyze a new paper during interactive session"""
try:
print(f"π Processing new paper: {paper_input[:100]}...")
# Process the new paper
processed_paper = await self.paper_processor.process_paper(paper_input)
if not processed_paper or not processed_paper.get('content'):
return "β Failed to process the paper. Please check the input format."
# Initialize processed_papers list if it doesn't exist
if 'processed_papers' not in self.research_context:
self.research_context['processed_papers'] = []
# Add to processed papers
paper_id = len(self.research_context['processed_papers']) + 1
processed_paper['id'] = paper_id
self.research_context['processed_papers'].append(processed_paper)
print(f"π§ Analyzing new paper {paper_id}...")
# Analyze the new paper
analysis_result = await self.paper_analyzer.run(
task=f"Analyze this research paper in detail:\n\n{processed_paper['content'][:3000]}..."
)
new_analysis = str(analysis_result.messages[-1].content) if hasattr(analysis_result, 'messages') and analysis_result.messages else str(analysis_result)
# Update analysis in context
if 'analysis' not in self.research_context:
self.research_context['analysis'] = {}
self.research_context['analysis'][f'paper_{paper_id}'] = new_analysis
print(f"π Searching for related literature...")
# Search for related papers
related_search = await self.literature_searcher.run(
task=f"Based on this new paper analysis, suggest search strategies and find related work:\n\n{str(new_analysis)[:2000]}..."
)
related_papers = str(related_search.messages[-1].content) if hasattr(related_search, 'messages') and related_search.messages else str(related_search)
# Update literature search in context
if 'literature' not in self.research_context:
self.research_context['literature'] = {}
self.research_context['literature'][f'paper_{paper_id}'] = related_papers
print(f"π§© Updating research synthesis...")
# Update synthesis with new paper
current_synthesis = str(self.research_context.get('synthesis', ''))
synthesis_update = await self.research_synthesizer.run(
task=f"Update the research landscape synthesis with this new paper analysis:\n\nNew Paper Analysis:\n{str(new_analysis)[:2000]}...\n\nCurrent Synthesis:\n{current_synthesis[:2000]}..."
)
updated_synthesis = str(synthesis_update.messages[-1].content) if hasattr(synthesis_update, 'messages') and synthesis_update.messages else str(synthesis_update)
self.research_context['synthesis'] = updated_synthesis
print(f"π Updating research proposal...")
# Update proposal with new insights
current_proposal = str(self.research_context.get('proposal', ''))
proposal_update = await self.proposal_writer.run(
task=f"Update the research proposal with insights from this new paper:\n\nNew Paper Analysis:\n{str(new_analysis)[:2000]}...\n\nCurrent Proposal:\n{current_proposal[:2000]}..."
)
updated_proposal = str(proposal_update.messages[-1].content) if hasattr(proposal_update, 'messages') and proposal_update.messages else str(proposal_update)
self.research_context['proposal'] = updated_proposal
return f"β
Successfully added and analyzed paper {paper_id}!\nπ Research context updated with new insights\nπ Synthesis and proposal have been refreshed"
except Exception as e:
return f"β Error adding new paper: {str(e)}"
def setup_agents(self):
"""Setup specialized research agents"""
self.paper_analyzer = AssistantAgent(
name="PaperAnalyzer",
model_client=self.model_client,
system_message="""You are an expert research paper analyzer. Analyze papers thoroughly and extract:
1. **Basic Information**: Title, authors, venue, year
2. **Research Context**: Problem being addressed, motivation, related work
3. **Methodology**: Approach, techniques, experimental setup
4. **Contributions**: Main contributions, novelty, significance
5. **Results**: Key findings, performance metrics, comparisons
6. **Limitations**: Acknowledged limitations, potential weaknesses
7. **Future Work**: Suggested directions, open problems
8. **Keywords**: Technical terms, research areas, methods
Provide structured, detailed analysis that captures the essence of the research.
Focus on understanding the research question, approach, and significance.
""",
description="Analyzes research papers comprehensively"
)
self.literature_searcher = AssistantAgent(
name="LiteratureSearcher",
model_client=self.model_client,
system_message="""You are an expert at literature search and discovery. Your expertise includes:
1. **Search Strategy**: Generate effective search queries for different databases
2. **Related Work**: Identify papers that cite or are cited by the analyzed papers
3. **Key Authors**: Find leading researchers and research groups in the field
4. **Venues**: Identify top conferences and journals in the research area
5. **Trends**: Spot emerging trends and hot topics
6. **Gaps**: Identify under-explored areas and research opportunities
Provide comprehensive search strategies and explain the reasoning behind them.
Suggest both broad surveys and specific technical papers.
""",
description="Expert at finding and organizing related literature"
)
self.research_synthesizer = AssistantAgent(
name="ResearchSynthesizer",
model_client=self.model_client,
system_message="""You are an expert research synthesizer who understands the big picture. Your role:
1. **Field Analysis**: Map out the current state of the research field
2. **Problem Importance**: Assess why research problems matter and their impact
3. **Approach Comparison**: Compare different methodological approaches
4. **Gap Identification**: Find unexplored areas and research opportunities
5. **Trend Analysis**: Identify emerging directions and future possibilities
6. **Critical Assessment**: Evaluate strengths and weaknesses of current work
Think like a senior researcher who can see connections across papers and identify
the most promising research directions. Focus on impact and significance.
""",
description="Synthesizes research landscape and identifies opportunities"
)
self.proposal_writer = AssistantAgent(
name="ProposalWriter",
model_client=self.model_client,
system_message="""You are an expert research proposal writer. Create compelling, well-structured proposals:
1. **Problem Statement**: Clear, motivated problem definition
2. **Literature Review**: Concise summary of relevant work and gaps
3. **Research Questions**: Specific, answerable research questions
4. **Methodology**: Detailed approach with justification
5. **Expected Outcomes**: Clear contributions and impact
6. **Timeline**: Realistic milestones and deliverables
7. **References**: Comprehensive, well-organized bibliography
Write proposals that are convincing, feasible, and impactful.
Make sure the research questions are novel and the approach is sound.
""",
description="Writes structured research proposals"
)
async def process_papers(self, paper_inputs: List[str]) -> List[Dict[str, Any]]:
"""Process different types of paper inputs"""
processed_papers = []
for i, paper_input in enumerate(paper_inputs):
print(f"π Processing paper {i+1}/{len(paper_inputs)}...")
paper_data = {"input": paper_input, "content": "", "error": ""}
try:
if "arxiv.org" in paper_input:
content, error = await self.paper_processor.download_arxiv_paper(paper_input)
paper_data["content"] = content
paper_data["error"] = error
paper_data["type"] = "arxiv"
elif paper_input.endswith('.pdf'):
if os.path.exists(paper_input):
content = await self.paper_processor.extract_text_from_pdf(paper_input)
paper_data["content"] = content
paper_data["type"] = "pdf"
else:
paper_data["error"] = f"PDF file not found: {paper_input}"
elif '\\documentclass' in paper_input or '\\begin{document}' in paper_input:
content = await self.paper_processor.process_latex_content(paper_input)
paper_data["content"] = content
paper_data["type"] = "latex"
else:
paper_data["content"] = paper_input
paper_data["type"] = "text"
except Exception as e:
paper_data["error"] = str(e)
processed_papers.append(paper_data)
return processed_papers
async def analyze_papers(self, processed_papers: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Analyze processed papers"""
print("π Analyzing papers...")
analyses = []
for i, paper in enumerate(processed_papers):
if paper["error"]:
print(f"β οΈ Skipping paper {i+1} due to error: {paper['error']}")
continue
print(f"π§ Analyzing paper {i+1}...")
analysis_prompt = f"""
Please provide a comprehensive analysis of this research paper:
{paper['content'][:8000]} # Limit content to avoid token limits
Provide detailed analysis covering all aspects mentioned in your system message.
Focus on understanding the research contribution and its significance.
"""
try:
analysis_result = await self.paper_analyzer.run(task=analysis_prompt)
# Extract the actual text content from the TaskResult
analysis_text = str(analysis_result.messages[-1].content) if hasattr(analysis_result, 'messages') and analysis_result.messages else str(analysis_result)
analyses.append({
"paper_index": i,
"analysis": analysis_text,
"paper_type": paper["type"]
})
except Exception as e:
print(f"β Error analyzing paper {i+1}: {str(e)}")
return {"analyses": analyses, "total_papers": len(processed_papers)}
async def search_related_work(self, analysis_results: Dict[str, Any]) -> Dict[str, Any]:
"""Search for related work based on analysis"""
print("π Searching for related literature...")
# Generate search queries based on analyses
search_prompt = f"""
Based on these paper analyses, generate comprehensive search strategies:
{json.dumps(analysis_results, indent=2)[:4000]}
Provide:
1. Specific search queries for Google Scholar
2. Key authors and research groups to investigate
3. Important venues and conferences
4. Related research areas and keywords
5. Potential survey papers and seminal works
"""
search_result = await self.literature_searcher.run(task=search_prompt)
search_strategy = str(search_result.messages[-1].content) if hasattr(search_result, 'messages') and search_result.messages else str(search_result)
# Extract search queries and perform actual searches
# For demo purposes, we'll simulate this
related_papers = []
# In a real implementation, you would:
# 1. Extract search queries from the strategy
# 2. Perform actual Google Scholar searches
# 3. Process and rank the results
return {
"search_strategy": search_strategy,
"related_papers": related_papers
}
async def synthesize_research_landscape(self, analysis_results: Dict[str, Any],
literature_search: Dict[str, Any]) -> Dict[str, Any]:
"""Synthesize the overall research landscape"""
print("π§© Synthesizing research landscape...")
synthesis_prompt = f"""
Synthesize the research landscape based on the paper analyses and literature search:
Paper Analyses: {json.dumps(analysis_results, indent=2)[:3000]}
Literature Search: {json.dumps(literature_search, indent=2)[:3000]}
Provide comprehensive synthesis covering:
1. Current state of the field - what's being studied and why
2. Key research questions and their importance
3. Main approaches and their trade-offs
4. Significant findings and breakthroughs
5. Open problems and research gaps
6. Emerging trends and future directions
7. Potential high-impact research opportunities
Think like a senior researcher mapping out the field.
"""
synthesis_result = await self.research_synthesizer.run(task=synthesis_prompt)
synthesis = str(synthesis_result.messages[-1].content) if hasattr(synthesis_result, 'messages') and synthesis_result.messages else str(synthesis_result)
return {"synthesis": synthesis}
async def generate_research_proposal(self, synthesis_results: Dict[str, Any]) -> Dict[str, Any]:
"""Generate research proposal outline"""
print("π Generating research proposal...")
proposal_prompt = f"""
Create a comprehensive research proposal based on this synthesis:
{json.dumps(synthesis_results, indent=2)[:4000]}
Generate a detailed proposal with:
1. **Executive Summary**
2. **Problem Statement and Motivation**
3. **Literature Review and Gap Analysis**
4. **Research Objectives and Questions**
5. **Proposed Methodology**
6. **Expected Contributions and Impact**
7. **Timeline and Milestones**
8. **Resource Requirements**
9. **Risk Assessment**
10. **Comprehensive Reference List**
Make it compelling, feasible, and impactful.
"""
proposal_result = await self.proposal_writer.run(task=proposal_prompt)
proposal = str(proposal_result.messages[-1].content) if hasattr(proposal_result, 'messages') and proposal_result.messages else str(proposal_result)
return {"proposal": proposal}
async def interactive_mode(self):
"""Interactive exploration mode"""
print("\n" + "="*60)
print("π€ INTERACTIVE RESEARCH EXPLORATION MODE")
print("="*60)
# Show context status
if self.research_context:
print(f"π Research context loaded with {len(self.research_context)} data items")
else:
print("π Starting with empty research context")
print("="*60)
print("Available commands:")
print("β’ 'search [topic]' - Find papers with clickable links from arXiv")
print("β’ 'add [paper]' - Add and analyze a new paper (arXiv URL, PDF path, or text)")
print("β’ 'analyze [aspect]' - Deep dive into specific aspects")
print("β’ 'refine proposal' - Refine the research proposal")
print("β’ 'explore [direction]' - Explore research directions")
print("β’ 'authors [name]' - Investigate specific authors")
print("β’ 'summary' - Get summary of current research context")
print("β’ 'save' - Save current research context")
print("β’ 'load [filename]' - Load saved research context")
print("β’ 'context' - Show detailed context summary")
print("β’ 'export' - Export research findings")
print("β’ 'exit' - End session")
print("="*60)
while True:
try:
user_input = input("\nπ Your request: ").strip()
if user_input.lower() in ['exit', 'quit', 'done']:
# Auto-save context before exiting
if self.research_context:
save_result = self.save_context()
print(f"πΎ {save_result}")
print("π Research session completed!")
break
if not user_input:
continue
# Route requests to appropriate agents
if user_input.lower().startswith('search'):
# Extract search query from user input
search_query = user_input[6:].strip() # Remove 'search' prefix
if not search_query:
response = "Please provide a search query. Example: 'search machine learning transformers'"
else:
# Get actual paper links from arXiv
paper_results = await self.search_related_papers(search_query)
# Also get AI analysis of the search
response_result = await self.literature_searcher.run(
task=f"Provide search strategy and analysis for: {search_query}\n\nContext: {self._serialize_context_safely(2000)}"
)
ai_analysis = str(response_result.messages[-1].content) if hasattr(response_result, 'messages') and response_result.messages else str(response_result)
# Combine AI analysis with actual paper links
response = f"{ai_analysis}\n\n{paper_results}"
elif user_input.lower().startswith('add'):
# Extract paper input
paper_input = user_input[3:].strip() # Remove 'add' prefix
if paper_input:
response = await self.add_new_paper(paper_input)
else:
response = "Please provide a paper to add. Example: add https://arxiv.org/abs/2301.00001"
elif user_input.lower().startswith('refine') or 'proposal' in user_input.lower():
response_result = await self.proposal_writer.run(
task=f"Help refine the research proposal: {user_input}\n\nContext: {self._serialize_context_safely(2000)}"
)
response = str(response_result.messages[-1].content) if hasattr(response_result, 'messages') and response_result.messages else str(response_result)
elif user_input.lower().startswith('analyze'):
response_result = await self.paper_analyzer.run(
task=f"Provide detailed analysis: {user_input}\n\nContext: {self._serialize_context_safely(2000)}"
)
response = str(response_result.messages[-1].content) if hasattr(response_result, 'messages') and response_result.messages else str(response_result)
elif user_input.lower() == 'summary':
response_result = await self.research_synthesizer.run(
task=f"Provide a comprehensive summary of our research exploration so far.\n\nContext: {self._serialize_context_safely(3000)}"
)
response = str(response_result.messages[-1].content) if hasattr(response_result, 'messages') and response_result.messages else str(response_result)
elif user_input.lower() == 'save':
response = self.save_context()
elif user_input.lower().startswith('load'):
# Extract filename if provided
parts = user_input.split(maxsplit=1)
filename = parts[1] if len(parts) > 1 else None
response = self.load_context(filename)
elif user_input.lower() == 'context':
response = self.get_context_summary()
else:
# Default to synthesizer for general questions
response_result = await self.research_synthesizer.run(
task=f"Help with this research question: {user_input}\n\nContext: {self._serialize_context_safely(2000)}"
)
response = str(response_result.messages[-1].content) if hasattr(response_result, 'messages') and response_result.messages else str(response_result)
print(f"\nπ€ **Response:**\n{response}")
except KeyboardInterrupt:
print("\nπ Session interrupted. Goodbye!")
break
except Exception as e:
print(f"β Error: {str(e)}")
async def run_complete_workflow(self, paper_inputs: List[str]):
"""Run the complete research assistant workflow"""
print("π ADVANCED RESEARCH ASSISTANT")
print("="*60)
try:
# Step 1: Process paper inputs
processed_papers = await self.process_papers(paper_inputs)
self.research_context['processed_papers'] = processed_papers
# Step 2: Analyze papers
analysis_results = await self.analyze_papers(processed_papers)
self.research_context['analysis'] = analysis_results
# Step 3: Search related literature
literature_search = await self.search_related_work(analysis_results)
self.research_context['literature'] = literature_search
# Step 4: Synthesize research landscape
synthesis = await self.synthesize_research_landscape(analysis_results, literature_search)
self.research_context['synthesis'] = synthesis
# Step 5: Generate research proposal
proposal = await self.generate_research_proposal(synthesis)
self.research_context['proposal'] = proposal
print("\n" + "="*60)
print("β
INITIAL RESEARCH WORKFLOW COMPLETED!")
print("π Research context built successfully")
print("π Research proposal generated")
print("="*60)
# Step 6: Interactive exploration
await self.interactive_mode()
except Exception as e:
print(f"β Error in workflow: {str(e)}")
finally:
await self.model_client.close()