-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathacademicResearchAgent.ts
More file actions
141 lines (128 loc) · 4.74 KB
/
academicResearchAgent.ts
File metadata and controls
141 lines (128 loc) · 4.74 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
import type { GoogleLanguageModelOptions } from '@ai-sdk/google'
import { Agent } from '@mastra/core/agent'
import type { RequestContext } from '@mastra/core/request-context'
import type { AgentRequestContext } from './request-context'
import { google } from '../config/google'
import { log } from '../config/logger'
import { pgMemory } from '../config/pg-storage'
import {
// serpapiSearchTool,
// serpapiAcademicTool,
arxivTool,
// pdfTool,
// documentChunkingTool,
// dataValidatorTool,
} from '../tools'
import { extractLearningsTool } from '../tools/extractLearningsTool'
import { InternalSpans } from '@mastra/core/observability'
export type AcademicResearchRuntimeContext = AgentRequestContext<{
researchType?: 'quick' | 'deep' | 'systematic'
}>
log.info('Initializing Academic Research Agent...')
export const academicResearchAgent = new Agent({
id: 'academicResearchAgent',
name: 'Academic Research Agent',
description:
'Expert at finding and analyzing academic papers, research articles, and scholarly sources. Performs systematic literature reviews and knowledge extraction.',
instructions: ({
requestContext,
}: {
requestContext: RequestContext<AcademicResearchRuntimeContext>
}) => {
const userTier = requestContext.get('user-tier') ?? 'free'
const language = requestContext.get('language') ?? 'en'
const researchType = requestContext.get('researchType') ?? 'quick'
return {
role: 'system',
content: `
# Academic Research Specialist
Tier: ${userTier} | Lang: ${language} | Type: ${researchType}
## Expertise
- Academic paper discovery and retrieval
- Scholarly search across multiple databases
- PDF processing and text extraction
- Document chunking for large papers
- Systematic literature reviews
- Citation management
## Tool Selection Guide
- **General Search**: Use 'serpapiSearchTool' for broad academic queries.
- **Academic Search**: Use 'serpapiAcademicTool' for scholarly sources.
- **ArXiv Papers**: Use 'arxivTool' for preprints and research papers.
- **PDF Processing**: Use 'pdfTool' to extract text from PDF documents.
- **Document Chunking**: Use 'documentChunkingTool' to split large papers.
- **Data Validation**: Use 'dataValidatorTool' to ensure data quality.
- **Insight Extraction**: Use 'extractLearningsTool' to surface key findings.
## Research Protocol
1. **Discovery**: Use academic search tools to find relevant papers.
2. **Retrieval**: Download and process PDF documents.
3. **Extraction**: Extract and structure relevant information.
4. **Analysis**: Synthesize findings across multiple sources.
5. **Validation**: Cross-verify claims and data.
## Rules
- **Citations**: Always include proper citations with DOI, title, authors.
- **Quality**: Prioritize peer-reviewed sources over preprints.
- **Comprehensiveness**: Cover multiple perspectives and methodologies.
- **Sourcing**: Track all sources with metadata.
- **Critical Thinking**: Evaluate methodology and limitations.
## Output Format
Provide structured research with:
- Executive summary of key findings
- Detailed analysis of top papers
- Citation list with DOIs and links
- Methodology comparison
- Gaps and future research directions
- Confidence levels for claims
${
researchType === 'systematic'
? `
## Systematic Review Protocol
1. Define search strategy and inclusion criteria
2. Search multiple databases comprehensively
3. Screen abstracts and full texts systematically
4. Extract data using standardized forms
5. Assess risk of bias
6. Synthesize findings with meta-analysis when possible
`
: ''
}
`,
providerOptions: {
google: {
responseModalities: ['TEXT'],
thinkingConfig: {
includeThoughts: true,
thinkingLevel: 'high',
},
} satisfies GoogleLanguageModelOptions,
},
}
},
model: ({ requestContext }) => {
const userTier = requestContext.get('user-tier') ?? 'free'
if (userTier === 'enterprise') {
return google.chat('gemini-3-pro-preview')
} else if (userTier === 'pro') {
return google.chat('gemini-3.1-flash-preview')
}
return google.chat('gemini-3.1-flash-lite-preview')
},
tools: {
// serpapiSearchTool,
// serpapiAcademicTool,
arxivTool,
// pdfTool,
// documentChunkingTool,
// dataValidatorTool,
extractLearningsTool,
},
memory: pgMemory,
options: {
tracingPolicy: {
internal: InternalSpans.ALL,
},
},
maxRetries: 3,
defaultOptions: {
// autoResumeSuspendedTools: true,
},
})