From ab9efb3c23fc72dffa0dcefa7753027dd8df1cde Mon Sep 17 00:00:00 2001
From: H <43509927+guoyuhao2330@users.noreply.github.com>
Date: Mon, 2 Sep 2024 16:19:41 +0800
Subject: [PATCH] Fix component PubMed (#2192)
### What problem does this PR solve?
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
---
agent/component/pubmed.py | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/agent/component/pubmed.py b/agent/component/pubmed.py
index b586d67e7..19cbdecc0 100644
--- a/agent/component/pubmed.py
+++ b/agent/component/pubmed.py
@@ -49,11 +49,19 @@ class PubMed(ComponentBase, ABC):
pubmedids = Entrez.read(Entrez.esearch(db='pubmed', retmax=self._param.top_n, term=ans))['IdList']
pubmedcnt = ET.fromstring(
Entrez.efetch(db='pubmed', id=",".join(pubmedids), retmode="xml").read().decode("utf-8"))
- pubmed_res = [{"content": 'Title:' + child.find("MedlineCitation").find("Article").find(
- "ArticleTitle").text + '\nUrl:' + '\n' + 'Abstract:' + child.find(
- "MedlineCitation").find("Article").find("Abstract").find("AbstractText").text} for child in
- pubmedcnt.findall("PubmedArticle")]
+ pubmed_res = []
+ for child in pubmedcnt.findall("PubmedArticle"):
+ if child.find("MedlineCitation").find("Article").find("ArticleTitle").text:
+ title_tmp = 'Title:' + child.find("MedlineCitation").find("Article").find("ArticleTitle").text
+ else:
+ title_tmp = 'Title:' + "".join(
+ [childtitle.text for childtitle in
+ child.find("MedlineCitation").find("Article").find("ArticleTitle")])
+ url_tmp = '\nUrl:' + ''
+ abstract_tmp = '\nAbstract:' + child.find("MedlineCitation").find("Article").find("Abstract").find(
+ "AbstractText").text
+ pubmed_res.append({"content": title_tmp + url_tmp + abstract_tmp})
except Exception as e:
return PubMed.be_output("**ERROR**: " + str(e))