Skip to content

Commit

Permalink
✨ Add try catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Toly committed Feb 13, 2025
1 parent a7a4435 commit 0230abe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
23 changes: 18 additions & 5 deletions routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,15 @@ router.post("/parse/newsinlevels", async function (req, res, next) {
router.post("/guardian", async function (req, res, next) {
const { title, summary, originalUrl, content, groupId } = req.body;
const userId = req.tUser._id;
const existGuardianList = await theGuardianModel.find({
groupId,
author: userId,
});
const existGuardianList = await theGuardianModel.find(
{
groupId,
author: userId,
},
"_id title"
);
if (existGuardianList.length > 0) {
res.json(generateBadResponse());
res.json(existGuardianList[0]);
} else {
if (title && content && groupId && originalUrl && req.tUser) {
const t = await theGuardianModel.create({
Expand All @@ -253,6 +256,16 @@ router.post("/guardian", async function (req, res, next) {
}
}
});

router.get("/guardian/:id", async function (req, res, next) {
const id = req.params.id;
const t = await theGuardianModel.findById(id);
if (t) {
res.json(generateResponse(t));
} else {
res.json(generateBadResponse());
}
});
// #endregion

module.exports = router;
29 changes: 18 additions & 11 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,25 @@ router.post("/deepseek", async function (req, res, next) {
res.json(generateBadResponse());
return;
}
const completion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: `请根据这篇文章的内容,帮我出5个阅读理解题目,每个题目提供4个选项。答案和解析统一在所有题目后提供。以下是文章内容: ${content}`,
},
],
model: "deepseek-chat",
});
try {
const completion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: `请根据这篇文章的内容,帮我出5个阅读理解题目,每个题目提供4个选项,题目和选项使用英文。答案和解析使用中文进行分析,答案和解析统一在所有题目后提供。以下是文章内容: ${content}`,
},
],
model: "deepseek-chat",
});

console.log(completion.choices[0].message.content);
res.send(completion);
console.log(completion.choices[0].message.content);
res.json(
generateResponse({ content: completion.choices[0].message.content })
);
} catch (error) {
console.log("deepseek error.");
console.log(error);
}
});

module.exports = router;

0 comments on commit 0230abe

Please sign in to comment.