import { Agentset } from 'agentset';
import { makeAgentsetTool, DEFAULT_SYSTEM_PROMPT } from '@agentset/ai-sdk';
import { generateText } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';
import fs from 'fs';
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const model = openai('gpt-4o');
async function financialAnalysis() {
// Initialize with environment variable
const agentset = new Agentset({
apiKey: process.env.AGENTSET_API_KEY,
});
// Create a namespace for financial reports
const namespace = await agentset.namespaces.create({
name: "Financial Reports",
});
// Get a reference to the namespace
const ns = agentset.namespace(namespace.id);
// Upload documents one by one via ingest jobs
// Example: Create an ingest job for annual report
const annualReportJob = await ns.ingestion.create({
payload: {
type: "FILE",
fileUrl: "https://example.com/reports/annual-report-2024.pdf",
name: "Annual Report 2024"
},
config: {
metadata: {
documentType: "annual_report",
year: 2024
}
}
});
// Create ingest job for quarterly report
const quarterlyReportJob = await ns.ingestion.create({
payload: {
type: "FILE",
fileUrl: "https://example.com/reports/quarterly-report-q1-2024.pdf",
name: "Quarterly Report Q1 2024"
},
config: {
metadata: {
documentType: "quarterly_report",
year: 2024,
quarter: "Q1"
}
}
});
// Create ingest job for benchmarks
const benchmarksJob = await ns.ingestion.create({
payload: {
type: "FILE",
fileUrl: "https://example.com/reports/industry-benchmarks-2024.pdf",
name: "Industry Benchmarks 2024"
},
config: {
metadata: {
documentType: "benchmarks",
year: 2024
}
}
});
console.log(`Ingest jobs created with IDs: ${annualReportJob.id}, ${quarterlyReportJob.id}, ${benchmarksJob.id}`);
// Get all ingestion jobs and check their status
const { jobs } = await ns.ingestion.all();
console.log("All jobs:", jobs.map(job => ({ id: job.id, status: job.status })));
// Run comparative analysis using chat
const response = await generateText({
model,
systemPrompt: DEFAULT_SYSTEM_PROMPT,
tools: {
knowledgeBase: makeAgentsetTool(ns, {
topK: 15,
rerank: true
}),
},
messages: [
{
role: "user",
content: "Compare our Q1 2024 performance with annual projections and industry benchmarks. Highlight areas where we're outperforming or underperforming the industry."
}
],
});
console.log("Analysis:", response.text);
}
financialAnalysis().catch(console.error);