mirror of
https://github.com/dzhng/deep-research.git
synced 2026-03-22 07:57:16 +08:00
feat: added API to generate report
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -11,7 +11,7 @@ node_modules
|
|||||||
.pnp.js
|
.pnp.js
|
||||||
|
|
||||||
# Local env files
|
# Local env files
|
||||||
.env
|
.env*
|
||||||
.env.local
|
.env.local
|
||||||
.env.development.local
|
.env.development.local
|
||||||
.env.test.local
|
.env.test.local
|
||||||
|
|||||||
38
src/api.ts
38
src/api.ts
@@ -1,7 +1,7 @@
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import express, { Request, Response } from 'express';
|
import express, { Request, Response } from 'express';
|
||||||
|
|
||||||
import { deepResearch, writeFinalAnswer } from './deep-research';
|
import { deepResearch, writeFinalAnswer,writeFinalReport } from './deep-research';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 3051;
|
const port = process.env.PORT || 3051;
|
||||||
@@ -58,6 +58,42 @@ app.post('/api/research', async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// generate report API
|
||||||
|
app.post('/api/generate-report',async(req:Request,res:Response)=>{
|
||||||
|
try{
|
||||||
|
const {query,depth = 3,breadth=3 } = req.body;
|
||||||
|
if(!query){
|
||||||
|
return res.status(400).json({error:'Query is required'});
|
||||||
|
}
|
||||||
|
log('\n Starting research...\n')
|
||||||
|
const {learnings,visitedUrls} = await deepResearch({
|
||||||
|
query,
|
||||||
|
breadth,
|
||||||
|
depth
|
||||||
|
});
|
||||||
|
log(`\n\nLearnings:\n\n${learnings.join('\n')}`);
|
||||||
|
log(
|
||||||
|
`\n\nVisited URLs (${visitedUrls.length}):\n\n${visitedUrls.join('\n')}`,
|
||||||
|
);
|
||||||
|
const report = await writeFinalReport({
|
||||||
|
prompt:query,
|
||||||
|
learnings,
|
||||||
|
visitedUrls
|
||||||
|
});
|
||||||
|
|
||||||
|
return report
|
||||||
|
|
||||||
|
}catch(error:unknown){
|
||||||
|
console.error("Error in generate report API:",error)
|
||||||
|
return res.status(500).json({
|
||||||
|
error:'An error occurred during research',
|
||||||
|
message:error instanceof Error? error.message: String(error),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Deep Research API running on port ${port}`);
|
console.log(`Deep Research API running on port ${port}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user