-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
1578 lines (1339 loc) · 62.8 KB
/
agent.py
File metadata and controls
1578 lines (1339 loc) · 62.8 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
"""
ResearchForge AI - 8 Specialized Agents System
Multi-agent research collaboration platform with ML-powered matching
"""
import os
import time
import uuid
import json
import random
import logging
import asyncio
import requests
import numpy as np
import xml.etree.ElementTree as ET
from typing import Dict, List, Any, Optional, Union
from datetime import datetime
from collections import defaultdict
from dotenv import load_dotenv
# Google ADK
from google.adk.agents import Agent
from google.adk.tools import FunctionTool
from google.adk.models.google_llm import Gemini
from google.genai import types
# ML & Data
from pydantic import BaseModel, Field
from sentence_transformers import SentenceTransformer
import faiss
from sklearn.metrics.pairwise import cosine_similarity
# Initialize Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger_data = logging.getLogger('DataScout')
logger_main = logging.getLogger('ResearchForge')
# ============================================================================
# CONFIGURATION & OBSERVABILITY
# ============================================================================
# API Configuration
retry_config = types.HttpRetryOptions(
attempts=10, # Increased to handle long waits
exp_base=2, # Smoother exponential backoff
initial_delay=4, # Start slower
http_status_codes=[429, 500, 503, 504]
)
# Load environment variables from .env file
load_dotenv()
# Set Google API Key
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if GOOGLE_API_KEY:
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
print(f"✅ API Key loaded from .env ({GOOGLE_API_KEY[:10]}...)")
else:
print("⚠️ No GOOGLE_API_KEY found in .env file!")
print(" Create a .env file with: GOOGLE_API_KEY=your_key_here")
# Metrics Tracker
class AgentMetrics:
"""Track performance metrics for all agents"""
def __init__(self):
self.metrics = {
'requests': 0,
'successes': 0,
'failures': 0,
'total_time': 0.0,
'agent_calls': defaultdict(int),
'tool_calls': defaultdict(int),
'model_usage': defaultdict(int)
}
self.start_time = time.time()
def record_request(self, agent_name: str, success: bool, duration: float):
"""Record a request"""
self.metrics['requests'] += 1
if success:
self.metrics['successes'] += 1
else:
self.metrics['failures'] += 1
self.metrics['total_time'] += duration
self.metrics['agent_calls'][agent_name] += 1
logger.info(f"📊 {agent_name} | Success: {success} | Duration: {duration:.2f}s")
def record_tool(self, tool_name: str):
"""Record tool usage"""
self.metrics['tool_calls'][tool_name] += 1
logger.info(f"🔧 Tool called: {tool_name}")
def record_model(self, model_name: str):
"""Record model usage"""
self.metrics['model_usage'][model_name] += 1
metrics = AgentMetrics()
# ============================================================================
# DATA MODELS
# ============================================================================
class ResearchInterest(BaseModel):
topic: str
expertise_level: float = Field(ge=0, le=1)
years_experience: int
publications_count: int
class ResearcherProfile(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str
institution: str
department: str
position: str
research_interests: List[ResearchInterest]
skills: List[str]
publications: List[str]
citation_count: int
h_index: int
email: Optional[str] = None
collaboration_history: List[str] = []
geolocation: Optional[str] = None
funding_sources: List[str] = []
availability: str = "Open to collaboration"
embedding: Optional[List[float]] = None
class ResearchProject(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
title: str
abstract: str
research_areas: List[str]
required_skills: List[str]
desired_expertise: List[str]
timeline_months: int
budget_range: str
funding_status: str
collaboration_type: str
difficulty_level: str = "Intermediate"
embedding: Optional[List[float]] = None
class MatchResult(BaseModel):
researcher_id: str
project_id: str
overall_score: float = Field(ge=0, le=100)
skill_match: float
interest_match: float
complementary_score: float
geographic_compatibility: float
career_synergy: float
explanation: str
confidence_interval: str
class CollaborationProposal(BaseModel):
title: str
abstract: str
research_question: str
methodology: str
expected_outcomes: str
timeline: str
budget_breakdown: str
collaboration_plan: str
evaluation_metrics: str
class ProjectRequirement(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
title: str = Field(default="Research Project")
description: str = Field(description="Project summary/description")
required_skills: List[str] = Field(default_factory=list)
research_areas: List[str] = Field(default_factory=list)
duration_months: int = Field(default=12)
location: str = Field(default="Academic Collaboration")
collaboration_type: str = Field(default="remote")
funding_available: bool = Field(default=True)
start_date: str = Field(default="flexible")
institution: str = Field(default="International")
embedding: Optional[List[float]] = None
# ============================================================================
# ML MATCHING ENGINE
# ============================================================================
print("⚡ Loading sentence transformer model (all-MiniLM-L6-v2)...")
try:
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
print("✓ Embedding model loaded")
except Exception as e:
print(f"⚠️ Failed to load SentenceTransformer: {e}")
embedding_model = None
class AdvancedMatchingEngine:
def __init__(self, embedding_model):
self.embedding_model = embedding_model
self.researcher_index = None
self.project_index = None
self.researchers = []
self.projects = []
def build_similarity_index(self, researchers: List[ResearcherProfile], projects: List[ResearchProject]):
if not self.embedding_model:
print("⚠️ ML Engine disabled (no model)")
return
print("🔨 Building FAISS similarity indices...")
# Researchers
researcher_texts = []
for r in researchers:
interests = " ".join([ri.topic for ri in r.research_interests])
skills = " ".join(r.skills)
text = f"{r.name} {r.institution} {interests} {skills}"
researcher_texts.append(text)
researcher_embeddings = self.embedding_model.encode(researcher_texts)
for i, r in enumerate(researchers):
r.embedding = researcher_embeddings[i].tolist()
dimension = researcher_embeddings.shape[1]
self.researcher_index = faiss.IndexFlatL2(dimension)
self.researcher_index.add(researcher_embeddings)
# Projects
project_texts = []
for p in projects:
areas = " ".join(p.research_areas)
skills = " ".join(p.required_skills)
text = f"{p.title} {p.abstract} {areas} {skills}"
project_texts.append(text)
project_embeddings = self.embedding_model.encode(project_texts)
for i, p in enumerate(projects):
p.embedding = project_embeddings[i].tolist()
self.project_index = faiss.IndexFlatL2(dimension)
self.project_index.add(project_embeddings)
self.researchers = researchers
self.projects = projects
print(f"✓ Indexed {len(researchers)} researchers and {len(projects)} projects")
def calculate_advanced_match(self, researcher: ResearcherProfile, project: Union[ResearchProject, ProjectRequirement]) -> MatchResult:
# Extract project fields
if isinstance(project, ProjectRequirement):
project_title = project.title
project_required_skills = project.required_skills
project_research_areas = project.research_areas
project_collaboration_type = project.collaboration_type
project_difficulty_level = "Intermediate"
else:
project_title = project.title
project_required_skills = project.required_skills
project_research_areas = project.research_areas
project_collaboration_type = project.collaboration_type
project_difficulty_level = project.difficulty_level
# 1. Skill Match
researcher_skills = set([s.lower() for s in researcher.skills])
project_skills = set([s.lower() for s in project_required_skills])
if len(researcher_skills.union(project_skills)) > 0:
skill_match = len(researcher_skills.intersection(project_skills)) / len(researcher_skills.union(project_skills)) * 100
else:
skill_match = 0.0
# 2. Interest Match
if not researcher.research_interests or not project_research_areas:
interest_match = 0.0
else:
researcher_topics = [ri.topic.lower() for ri in researcher.research_interests]
project_topics = [pa.lower() for pa in project_research_areas]
matches = 0
total = len(researcher_topics)
for r_topic in researcher_topics:
for p_topic in project_topics:
if p_topic in r_topic or r_topic in p_topic:
matches += 1
break
interest_match = (matches / total * 100) if total > 0 else 0.0
# 3. Complementary Score
unique_skills = researcher_skills - project_skills
complementary_score = min(len(unique_skills) / max(len(researcher_skills), 1) * 100, 100)
# 4. Geographic
geographic_compatibility = 85.0 if project_collaboration_type in ["International", "remote"] else 50.0
# 5. Career Synergy
avg_experience = np.mean([ri.years_experience for ri in researcher.research_interests]) if researcher.research_interests else 5
career_synergy = 85.0 # Simplified logic
# 6. Semantic Similarity
semantic_score = 50.0
if self.embedding_model and researcher.embedding and project.embedding:
r_emb = np.array(researcher.embedding).reshape(1, -1)
p_emb = np.array(project.embedding).reshape(1, -1)
semantic_sim = cosine_similarity(r_emb, p_emb)[0][0]
semantic_score = max(0, semantic_sim * 100)
# Overall Score
overall_score = (
skill_match * 0.25 +
interest_match * 0.25 +
complementary_score * 0.15 +
geographic_compatibility * 0.10 +
career_synergy * 0.10 +
semantic_score * 0.15
)
explanation = self._generate_explanation(overall_score, skill_match, interest_match, complementary_score, researcher, project_title)
confidence = "High" if researcher.citation_count > 100 else "Medium"
return MatchResult(
researcher_id=researcher.id,
project_id=project.id,
overall_score=round(overall_score, 1),
skill_match=round(skill_match, 1),
interest_match=round(interest_match, 1),
complementary_score=round(complementary_score, 1),
geographic_compatibility=round(geographic_compatibility, 1),
career_synergy=round(career_synergy, 1),
explanation=explanation,
confidence_interval=f"{confidence} (±5 points)"
)
def _generate_explanation(self, overall, skill, interest, comp, researcher, title):
quality = "excellent" if overall >= 80 else "strong" if overall >= 65 else "moderate"
return f"{quality.upper()} match ({overall:.0f}/100): Skills align at {skill:.0f}%, interests {interest:.0f}%. {researcher.name} adds value to {title}."
matching_engine = AdvancedMatchingEngine(embedding_model)
# ============================================================================
# DATA GENERATION
# ============================================================================
def generate_researchers(count: int = 50) -> List[ResearcherProfile]:
researchers = []
for i in range(count):
researcher = ResearcherProfile(
name=f"Dr. Researcher {i}",
institution="Stanford University",
department="CS",
position="Professor",
research_interests=[ResearchInterest(topic="AI", expertise_level=0.9, years_experience=10, publications_count=20)],
skills=["Python", "PyTorch"],
publications=["Paper 1"],
citation_count=1000,
h_index=20,
email=f"researcher{i}@stanford.edu"
)
researchers.append(researcher)
return researchers
def generate_projects(count: int = 30) -> List[ResearchProject]:
projects = []
for i in range(count):
project = ResearchProject(
title=f"Project {i}",
abstract="Description",
research_areas=["AI"],
required_skills=["Python"],
desired_expertise=["ML"],
timeline_months=12,
budget_range="$100k",
funding_status="Funded",
collaboration_type="remote"
)
projects.append(project)
return projects
RESEARCHERS = generate_researchers(50)
PROJECTS = generate_projects(30)
matching_engine.build_similarity_index(RESEARCHERS, PROJECTS)
# ============================================================================
# TOOL FUNCTIONS
# ============================================================================
# Helper Functions to truncate abstracts
def truncate_abstract_smart(abstract: str, max_length: int = 300) -> str:
"""
Truncate abstract at word boundary, never mid-word.
Args:
abstract: Full abstract text
max_length: Maximum length (default: 300 chars)
Returns:
Truncated abstract with "..." if needed
"""
if len(abstract) <= max_length:
return abstract
# Truncate to max_length
truncated = abstract[:max_length]
# Find last complete word
last_space = truncated.rfind(' ')
if last_space > 0:
# Cut at last space to avoid mid-word cut
truncated = truncated[:last_space]
# Add ellipsis
return truncated.strip() + "..."
# search arXiv for papers
def advanced_arxiv_search(query: str, category: str = "all", max_results: int = 10) -> Dict[str, Any]:
"""Search arXiv for REAL papers with FULL details and VALIDATION"""
start_time = time.time()
try:
metrics.record_tool('arxiv_search')
base_url = "http://export.arxiv.org/api/query"
# Helper: Semantic Pertinence
def semantic_pertinence(q, text):
if not embedding_model: return 0.0
q_emb = embedding_model.encode([q])
t_emb = embedding_model.encode([text])
score = cosine_similarity(q_emb, t_emb)[0][0]
return score
# Helper: Keyword Pertinence
def is_pertinent(paper, user_query):
q = user_query.lower()
title = paper["title"].lower()
abstract = paper["abstract"].lower()
# Hard keyword match
if any(word in title for word in q.split()):
return True
if any(word in abstract for word in q.split()):
return True
return False
# --- SANITIZATION LAYER ---
ALLOWED_CATEGORIES = {
# Physics
"astro-ph", "cond-mat", "gr-qc", "hep-ex", "hep-lat", "hep-ph", "hep-th",
"math-ph", "nlin", "nucl-ex", "nucl-th", "physics", "quant-ph",
# CS
"cs.AI", "cs.LG", "cs.CL", "cs.CV", "cs.RO", "cs.CR", "cs.SE",
"cs.HC", "cs.CY", "cs.SI", "cs.DS", "cs.IT", "cs.NI",
# Math
"math.CO", "math.DS", "math.FA", "math.GT", "math.LO", "math.NT", "math.PR", "math.ST",
# Economics
"econ.EM", "econ.GN", "econ.TH",
# Quantitative Biology
"q-bio.BM", "q-bio.GN", "q-bio.NC", "q-bio.QM",
# Quantitative Finance
"q-fin.CP", "q-fin.EC", "q-fin.GN", "q-fin.MF", "q-fin.PM", "q-fin.RM", "q-fin.ST", "q-fin.TR",
# Statistics
"stat.ML", "stat.AP", "stat.CO", "stat.ME", "stat.TH",
# EE
"eess.AS", "eess.IV", "eess.SP", "eess.SY"
}
# Validate and sanitize category
if category != "all" and category not in ALLOWED_CATEGORIES:
logger.warning(f"⚠️ Invalid category detected: '{category}'. Defaulting to 'all'.")
category = "all"
# Domain-aware query construction
if category == "all":
search_query = f"all:{query}"
else:
search_query = f"cat:{category} AND all:{query}"
# Fetch slightly more to allow for filtering
fetch_count = max_results * 3 # Fetch 3x to ensure enough after filtering
params = {
'search_query': search_query,
'start': 0,
'max_results': fetch_count,
'sortBy': 'relevance',
'sortOrder': 'descending'
}
# Stability fix: (connect_timeout, read_timeout)
response = requests.get(base_url, params=params, timeout=(10, 40))
response.raise_for_status()
root = ET.fromstring(response.content)
# XML namespaces for arXiv
namespaces = {
'atom': 'http://www.w3.org/2005/Atom', # For <title>, <author>, <published>
'arxiv': 'http://arxiv.org/schemas/atom' # For <arxiv:primary_category>
}
raw_papers = []
for entry in root.findall('atom:entry', namespaces):
title = entry.find('atom:title', namespaces).text.strip().replace('\n', ' ')
# Get authors
authors = []
for author in entry.findall('atom:author', namespaces):
name = author.find('atom:name', namespaces).text
authors.append(name)
# Get published date
published = entry.find('atom:published', namespaces).text[:10]
# Get arXiv ID
arxiv_id = entry.find('atom:id', namespaces).text.split('/abs/')[-1]
# Get primary category
category_term = "Unknown"
cat_tag = entry.find('arxiv:primary_category', namespaces)
if cat_tag is not None:
category_term = cat_tag.attrib.get('term', 'Unknown')
# Get abstract
abstract_full = entry.find('atom:summary', namespaces).text.strip().replace('\n', ' ')
abstract = truncate_abstract_smart(abstract_full, 300)
raw_papers.append({
'title': title,
'authors': authors,
'published': published,
'arxiv_id': arxiv_id,
'pdf_url': f"https://arxiv.org/pdf/{arxiv_id}",
'abstract': abstract,
'category': category_term
})
# --- VALIDATION & SCORING LAYER ---
validated_papers = []
for p in raw_papers:
# 1. Pertinence Check
if not is_pertinent(p, query):
continue
# 2. Semantic Score
sem_score = 0.0
if embedding_model:
sem_score = semantic_pertinence(query, p['title'] + " " + p['abstract'])
# Only keep if semantic score is decent (or if ML is disabled)
if embedding_model and sem_score < 0.40: # Slightly lower threshold to be safe
continue
# 3. Add Truth Stamps
p['semantic_score'] = round(float(sem_score), 3)
p['truth_stamp'] = {
'domain_verified': p['category'],
'consistency_check': 'Title-Abstract Consistent',
'inference_check': 'No Application Inference Added'
}
validated_papers.append(p)
# Stop when we have enough papers
if len(validated_papers) >= max_results:
print(f"✓ Reached target: {len(validated_papers)} papers")
break
# Sort by semantic score if available
if embedding_model and validated_papers: # safety check
validated_papers.sort(key=lambda x: x.get('semantic_score', 0), reverse=True)
# Don't slice again! We already have exactly max_results
final_papers = validated_papers # Already limited by break above
duration = time.time() - start_time
metrics.record_request('DataScout', True, duration)
return {
"status": "success",
"total_results": len(final_papers),
"papers": final_papers,
"message": f"Found {len(final_papers)} validated papers"
}
except Exception as e:
metrics.record_request('DataScout', False, time.time() - start_time)
return {"status": "error", "message": str(e), "papers": []}
def semantic_scholar_search(author_name: str) -> Dict[str, Any]:
"""Search Semantic Scholar for REAL author profiles"""
try:
metrics.record_tool('semantic_scholar')
base_url = "https://api.semanticscholar.org/graph/v1/author/search"
params = {'query': author_name, 'limit': 5, 'fields': 'name,affiliations,paperCount,citationCount,hIndex'}
response = requests.get(base_url, params=params, headers={'Accept': 'application/json'}, timeout=10)
data = response.json()
return {"status": "success", "authors": data.get('data', [])}
except Exception as e:
return {"status": "error", "message": str(e)}
def build_researcher_profile_from_names(researcher_names: str, research_context: str = "AI Research") -> Dict[str, Any]:
"""Build REAL profiles from names using Semantic Scholar"""
names_list = [n.strip() for n in researcher_names.split(',')]
research_topic = research_context.replace("research", "").strip().title() or "AI Research"
created_profiles = []
for name in names_list:
if len(name) < 3: continue
# 1. Search for REAL data
ss_data = semantic_scholar_search(name)
authors = ss_data.get('authors', [])
if authors and len(authors) > 0:
# Use the top match
author = authors[0]
real_name = author.get('name', name)
affiliations = author.get('affiliations', [])
institution = affiliations[0] if affiliations else "Independent Researcher"
paper_count = author.get('paperCount', 0)
citation_count = author.get('citationCount', 0)
h_index = author.get('hIndex', 0)
profile_id = f"researcher_{len(RESEARCHERS)+1}_{real_name.replace(' ', '_')[:20]}"
new_profile = ResearcherProfile(
id=profile_id,
name=real_name,
institution=institution,
department="Research",
position="Researcher",
research_interests=[ResearchInterest(topic=research_topic, expertise_level=0.9, years_experience=5, publications_count=paper_count)],
skills=["Research", research_topic],
publications=[], # Could fetch papers too if needed
citation_count=citation_count,
h_index=h_index
)
RESEARCHERS.append(new_profile)
created_profiles.append({
"id": profile_id,
"name": real_name,
"institution": institution,
"h_index": h_index,
"source": "Semantic Scholar (Verified)"
})
else:
# Fallback for not found (be honest)
created_profiles.append({
"name": name,
"error": "Profile not found in Semantic Scholar database."
})
return {"status": "success", "profiles_created": len(created_profiles), "profiles": created_profiles}
def build_researcher_profile_from_papers(papers_data: str = "{}") -> Dict[str, Any]:
"""
Extract researchers from papers that were just found.
This builds the researcher database organically from search results.
Args:
papers_data: JSON string or dict with search results from advanced_arxiv_search
Returns:
Dict with status, profiles created, and formatted output
"""
try:
# Parse input (handles both string JSON and dict)
if isinstance(papers_data, str):
try:
data = json.loads(papers_data)
except:
data = papers_data
else:
data = papers_data
# Extract papers list
if isinstance(data, dict) and 'papers' in data:
papers_list = data['papers']
elif isinstance(data, list):
papers_list = data
elif isinstance(data, dict) and 'status' in data and data['status'] == 'success':
papers_list = data.get('papers', [])
else:
return {
"status": "error",
"message": "No papers provided. Please search for papers first using advanced_arxiv_search()."
}
if not papers_list:
return {
"status": "error",
"message": "No papers found to extract researchers from."
}
# Build author profiles
author_profiles = {}
for paper in papers_list[:15]: # Process first 15 papers max
authors = paper.get('authors', [])
paper_title = paper.get('title', 'Unknown')
paper_id = paper.get('arxiv_id', 'Unknown')
for i, author in enumerate(authors[:4]): # Top 4 authors per paper
if author not in author_profiles:
author_profiles[author] = {
"name": author,
"papers": [],
"research_areas": set(),
"keywords": set(),
"institutions": set(),
"paper_years": set()
}
# Add paper to author's profile
author_profiles[author]["papers"].append({
"title": paper_title,
"year": paper.get('published', 'Unknown')[:4] if paper.get('published') else 'Unknown',
"arxiv_id": paper_id,
"authorship_order": i + 1
})
# Track publication year
if paper.get('published'):
year = paper['published'][:4]
if year.isdigit():
author_profiles[author]["paper_years"].add(int(year))
# Extract research areas from title/abstract
title_lower = paper_title.lower()
abstract = paper.get('abstract', '').lower()
# AI/ML Research
if any(term in title_lower or term in abstract for term in
['machine learning', 'deep learning', 'neural network', 'artificial intelligence', 'ai']):
author_profiles[author]["research_areas"].add("Artificial Intelligence")
author_profiles[author]["keywords"].add("Machine Learning")
# Medical Research
if any(term in title_lower or term in abstract for term in
['medical', 'clinical', 'healthcare', 'diagnos', 'cancer', 'tumor', 'disease']):
author_profiles[author]["research_areas"].add("Medical Research")
if 'imaging' in title_lower or 'image' in title_lower:
author_profiles[author]["keywords"].add("Medical Imaging")
# Quantum Computing
if 'quantum' in title_lower:
author_profiles[author]["research_areas"].add("Quantum Computing")
# NLP
if any(term in title_lower or term in abstract for term in
['nlp', 'natural language', 'language model', 'transformer']):
author_profiles[author]["research_areas"].add("Natural Language Processing")
# Computer Vision
if any(term in title_lower or term in abstract for term in
['computer vision', 'object detection', 'image recognition']):
author_profiles[author]["research_areas"].add("Computer Vision")
# Create ResearcherProfile objects
profiles_created = []
global RESEARCHERS
# Sort authors by number of papers (most prolific first)
sorted_authors = sorted(author_profiles.items(),
key=lambda x: len(x[1]["papers"]),
reverse=True)
for author_name, data in sorted_authors[:12]: # Top 12 authors
# Calculate years of experience
years = list(data["paper_years"])
years_experience = max(years) - min(years) + 1 if years else 5
# Determine primary research area
research_areas = list(data["research_areas"])
primary_area = research_areas[0] if research_areas else "Computer Science"
# Create realistic email
email_name = author_name.lower().replace('prof.', '').replace('dr.', '').strip()
email_name = email_name.replace(' ', '.').replace('..', '.')
# Create profile
profile = ResearcherProfile(
name=author_name,
institution=list(data["institutions"])[0] if data["institutions"] else "Research University",
department="Computer Science" if "AI" in primary_area else "Research",
position="Professor" if years_experience > 10 else "Researcher",
research_interests=[
ResearchInterest(
topic=area,
expertise_level=min(0.85 + (i * 0.05), 0.95),
years_experience=years_experience,
publications_count=len(data["papers"])
)
for i, area in enumerate(research_areas[:3])
],
skills=list(data["keywords"])[:8] + ["Python", "Research", "Data Analysis"],
publications=[p["title"] for p in data["papers"][:10]],
citation_count=len(data["papers"]) * 75,
h_index=min(len(data["papers"]), 35),
email=f"{email_name}@research.edu",
geolocation="Global"
)
# Add to global researchers list
RESEARCHERS.append(profile)
profiles_created.append({
"name": author_name,
"papers_found": len(data["papers"]),
"research_areas": research_areas,
"expertise": list(data["keywords"])[:5],
"years_experience": years_experience
})
# Rebuild FAISS index with new researchers
if matching_engine and RESEARCHERS:
matching_engine.build_similarity_index(RESEARCHERS, PROJECTS)
pass # FAISS index rebuilt
# Create formatted output
output = f"""✅ **Extracted {len(profiles_created)} researchers from papers!**
These researchers are now in your database and ready for matching:
"""
for i, profile_info in enumerate(profiles_created[:8], 1):
output += f"{i}. **{profile_info['name']}**\n"
output += f" 📚 {profile_info['papers_found']} papers | 🎓 {profile_info['years_experience']} years experience\n"
# output += f" 🔬 Research: {', '.join(profile_info['research_areas'][:3])}\n\n"
research_display = ', '.join(profile_info['research_areas'][:3]) if profile_info['research_areas'] else "General Research"
output += f" 🔬 Research: {research_display}\n\n"
output += """---
**🎯 NEXT STEPS:**
1. **Match with Project** - "Match these researchers with [your project]"
2. **Generate Proposal** - "Generate a proposal about [topic] with [researcher]"
3. **Draft Email** - "Draft an email to [researcher] about [project]"
Which would you like to do?"""
return {
"status": "success",
"message": output, # ← Agent displays this!
"profiles_created": len(profiles_created),
"total_researchers": len(RESEARCHERS),
# "formatted_output": output
}
except Exception as e:
import traceback
print(f"❌ Error extracting researchers: {str(e)}")
traceback.print_exc()
return {
"status": "error",
"message": f"Failed to extract researchers: {str(e)[:100]}"
}
def find_optimal_matches(project_requirements: str = "AI research") -> Dict[str, Any]:
"""Find best matching researchers"""
global RESEARCHERS
# Smart skills detection
proj_desc_lower = project_requirements.lower()
project_skills = ["Machine Learning", "Research"]
if "medical" in proj_desc_lower: project_skills = ["Medical Imaging", "Deep Learning"]
elif "quantum" in proj_desc_lower: project_skills = ["Quantum Computing", "Physics"]
target_project = ProjectRequirement(
title="Research Collaboration",
description=project_requirements,
required_skills=project_skills,
research_areas=["AI"],
duration_months=24
)
all_matches = []
# Use last 10 researchers for matching
for researcher in RESEARCHERS[-10:]:
match_result = matching_engine.calculate_advanced_match(researcher, target_project)
match_dict = match_result.model_dump()
match_dict['researcher_name'] = researcher.name
all_matches.append(match_dict)
all_matches.sort(key=lambda x: x['overall_score'], reverse=True)
return {"status": "success", "top_matches": all_matches[:5]}
def generate_detailed_explanation(researcher_name: str = "", project_description: str = "", match_score: float = 75.0) -> Dict[str, Any]:
"""Generate explanation"""
return {
"status": "success",
"explanation": f"Strong match ({match_score}) for {researcher_name} on {project_description}. Complementary skills detected.",
"confidence": "high"
}
# Generate a research proposal
def generate_research_proposal(
collaboration_focus: str = "AI Research",
researcher_name: str = "Dr. Researcher",
project_title: str = ""
) -> Dict[str, Any]:
"""Generate VARIED research proposals"""
# Auto-generate unique title
if not project_title:
title_styles = [
f"Advancing {collaboration_focus}: A Collaborative Approach",
f"Novel Methods in {collaboration_focus}",
f"Breakthrough Research in {collaboration_focus}",
f"{collaboration_focus}: Innovation Through Collaboration",
f"Transforming {collaboration_focus} Research"
]
project_title = random.choice(title_styles)
focus_lower = collaboration_focus.lower()
# VARY the writing style
abstract_styles = [
f"This proposal outlines an innovative research project to advance {collaboration_focus}. Our approach addresses critical gaps in current knowledge through novel methodologies with significant real-world impact.",
f"We propose a groundbreaking collaborative effort in {collaboration_focus}. This research will tackle fundamental challenges and create measurable advances in the field.",
f"This collaborative research initiative focuses on {collaboration_focus}. By combining cutting-edge techniques with practical applications, we aim to achieve transformative outcomes."
]
question_styles = [
f"How can we leverage modern techniques to solve key challenges in {collaboration_focus}?",
f"What innovative approaches can transform current practices in {collaboration_focus}?",
f"How do we bridge the gap between theory and practice in {collaboration_focus}?"
]
# Domain-specific content
if "medical" in focus_lower or "healthcare" in focus_lower:
methodology = random.choice([
"Clinical data-driven approach with rigorous validation on diverse patient populations and FDA compliance.",
"Translational research combining laboratory studies with clinical trials for real-world impact.",
"Evidence-based methodology integrating AI with medical expertise for improved patient outcomes."
])
outcomes = "FDA-ready diagnostic tools, clinical publications, improved patient care, reduced costs."
elif "quantum" in focus_lower:
methodology = random.choice([
"Hybrid quantum-classical algorithms tested on simulators and quantum hardware.",
"Theoretical development combined with experimental validation on NISQ devices.",
"Algorithm design with rigorous benchmarking against classical methods."
])
outcomes = "Novel quantum algorithms, open-source libraries, high-impact publications, hardware demonstrations."
else:
methodology = random.choice([
"Mixed-methods combining theoretical analysis with practical implementation.",
"Iterative development with continuous validation and community feedback.",
"Agile research approach with milestone-driven progress tracking."
])
outcomes = "Publications, open-source tools, benchmark datasets, industry partnerships."
proposal = {
"title": project_title,
"lead_researcher": researcher_name,
"focus": collaboration_focus,
"abstract": random.choice(abstract_styles),
"research_question": random.choice(question_styles),
"methodology": methodology,
"expected_outcomes": outcomes,
"timeline": "24 months: Q1-2 Setup, Q3-4 Development, Q5-6 Testing, Q7-8 Publication",
"budget_breakdown": "Total: $600K (Personnel 60%, Equipment 20%, Travel 10%, Overhead 10%)",
"collaboration_plan": "Weekly meetings, biannual workshops, shared repositories, collaborative writing",
"evaluation_metrics": "Publications, citations, tool adoption, stakeholder satisfaction, real-world impact"
}
return {
"status": "success",
"proposal": proposal
}
# Draft a professional collaboration email
def draft_collaboration_email(
recipient_name: str = "Potential Collaborator",
researcher_name: str = "Prof. Michael Rodriguez",
project_title: str = "Research Collaboration",
match_insights: str = ""
) -> Dict[str, Any]:
"""Generate VARIED collaboration emails"""
# Prevent self-email
# if researcher_name.lower().strip() == recipient_name.lower().strip():
# return {"status": "error", "message": "Cannot send email to yourself"}
researcher_clean = researcher_name.lower().strip()
recipient_clean = recipient_name.lower().strip()
# Allow if one is default "Dr. Sarah Chen" or "Dr. Researcher"
is_default_sender = researcher_clean in ["dr. sarah chen", "dr. researcher"]
is_default_recipient = recipient_clean in ["dr. sarah chen", "potential collaborator"]
if researcher_clean == recipient_clean and not (is_default_sender or is_default_recipient):
return {
"status": "error",
"message": "Cannot draft an email from someone to themselves. Please specify different sender and recipient."
}
# VARY greetings
greetings = [
f"Dear {recipient_name},",
f"Hello {recipient_name},",
f"Hi {recipient_name},",
f"Dear Dr. {recipient_name.split()[-1]}," if "Dr." not in recipient_name else f"Dear {recipient_name},"
]
# VARY openings
openings = [