Kevin Hu commited on
Commit
aa09acb
·
1 Parent(s): 210b45f

refactor name of duckduckgo (#1496)

Browse files

### What problem does this PR solve?


### Type of change

- [x] Refactoring

graph/component/__init__.py CHANGED
@@ -10,7 +10,7 @@ from .message import Message, MessageParam
10
  from .rewrite import RewriteQuestion, RewriteQuestionParam
11
  from .keyword import KeywordExtract, KeywordExtractParam
12
  from .baidu import Baidu, BaiduParam
13
- from .duckduckgosearch import DuckDuckGoSearch, DuckDuckGoSearchParam
14
 
15
 
16
  def component_class(class_name):
 
10
  from .rewrite import RewriteQuestion, RewriteQuestionParam
11
  from .keyword import KeywordExtract, KeywordExtractParam
12
  from .baidu import Baidu, BaiduParam
13
+ from .duckduckgo import DuckDuckGo, DuckDuckGoParam
14
 
15
 
16
  def component_class(class_name):
graph/component/{duckduckgosearch.py → duckduckgo.py} RENAMED
@@ -1,62 +1,62 @@
1
- #
2
- # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- import random
17
- from abc import ABC
18
- from functools import partial
19
- from duckduckgo_search import DDGS
20
- import pandas as pd
21
-
22
- from graph.component.base import ComponentBase, ComponentParamBase
23
-
24
-
25
- class DuckDuckGoSearchParam(ComponentParamBase):
26
- """
27
- Define the DuckDuckGoSearch component parameters.
28
- """
29
-
30
- def __init__(self):
31
- super().__init__()
32
- self.top_n = 10
33
- self.channel = "text"
34
-
35
- def check(self):
36
- self.check_positive_integer(self.top_n, "Top N")
37
- self.check_valid_value(self.channel, "Web Search or News", ["text", "news"])
38
-
39
-
40
- class DuckDuckGoSearch(ComponentBase, ABC):
41
- component_name = "DuckDuckGoSearch"
42
-
43
- def _run(self, history, **kwargs):
44
- ans = self.get_input()
45
- ans = " - ".join(ans["content"]) if "content" in ans else ""
46
- if not ans:
47
- return DuckDuckGoSearch.be_output(self._param.no)
48
-
49
- if self.channel == "text":
50
- with DDGS() as ddgs:
51
- # {'title': '', 'href': '', 'body': ''}
52
- duck_res = [{"content": '<a href="' + i["href"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
53
- ddgs.text(ans, max_results=self._param.top_n)]
54
- elif self.channel == "news":
55
- with DDGS() as ddgs:
56
- # {'date': '', 'title': '', 'body': '', 'url': '', 'image': '', 'source': ''}
57
- duck_res = [{"content": '<a href="' + i["url"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
58
- ddgs.news(ans, max_results=self._param.top_n)]
59
-
60
- df = pd.DataFrame(duck_res)
61
- print(df, ":::::::::::::::::::::::::::::::::")
62
- return df
 
1
+ #
2
+ # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ import random
17
+ from abc import ABC
18
+ from functools import partial
19
+ from duckduckgo_search import DDGS
20
+ import pandas as pd
21
+
22
+ from graph.component.base import ComponentBase, ComponentParamBase
23
+
24
+
25
+ class DuckDuckGoParam(ComponentParamBase):
26
+ """
27
+ Define the DuckDuckGo component parameters.
28
+ """
29
+
30
+ def __init__(self):
31
+ super().__init__()
32
+ self.top_n = 10
33
+ self.channel = "text"
34
+
35
+ def check(self):
36
+ self.check_positive_integer(self.top_n, "Top N")
37
+ self.check_valid_value(self.channel, "Web Search or News", ["text", "news"])
38
+
39
+
40
+ class DuckDuckGo(ComponentBase, ABC):
41
+ component_name = "DuckDuckGo"
42
+
43
+ def _run(self, history, **kwargs):
44
+ ans = self.get_input()
45
+ ans = " - ".join(ans["content"]) if "content" in ans else ""
46
+ if not ans:
47
+ return DuckDuckGo.be_output(self._param.no)
48
+
49
+ if self.channel == "text":
50
+ with DDGS() as ddgs:
51
+ # {'title': '', 'href': '', 'body': ''}
52
+ duck_res = [{"content": '<a href="' + i["href"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
53
+ ddgs.text(ans, max_results=self._param.top_n)]
54
+ elif self.channel == "news":
55
+ with DDGS() as ddgs:
56
+ # {'date': '', 'title': '', 'body': '', 'url': '', 'image': '', 'source': ''}
57
+ duck_res = [{"content": '<a href="' + i["url"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
58
+ ddgs.news(ans, max_results=self._param.top_n)]
59
+
60
+ df = pd.DataFrame(duck_res)
61
+ print(df, ":::::::::::::::::::::::::::::::::")
62
+ return df