|
1 | 1 | from omagent_core.engine.worker.base import BaseWorker |
2 | 2 | from omagent_core.utils.registry import registry |
3 | 3 | from omagent_core.utils.logger import logging |
4 | | -from langchain.agents.react.base import DocstoreExplorer |
5 | | -from langchain_community.docstore.wikipedia import Wikipedia |
| 4 | +import wikipedia |
6 | 5 | from pydantic import Field |
7 | 6 |
|
| 7 | +class WikipediaSearcher: |
| 8 | + """Simple Wikipedia search implementation that mimics DocstoreExplorer""" |
| 9 | + |
| 10 | + def search(self, query: str) -> str: |
| 11 | + """Search Wikipedia and return the page content""" |
| 12 | + try: |
| 13 | + page_content = wikipedia.page(query).content |
| 14 | + return page_content |
| 15 | + except wikipedia.PageError: |
| 16 | + return f"Could not find [{query}]. Similar: {wikipedia.search(query)}" |
| 17 | + except wikipedia.DisambiguationError: |
| 18 | + return f"Could not find [{query}]. Similar: {wikipedia.search(query)}" |
| 19 | + |
8 | 20 | @registry.register_worker() |
9 | 21 | class WikiSearch(BaseWorker): |
10 | 22 | """Wiki Search worker for React workflow""" |
11 | 23 |
|
12 | | - def __init__(self, *args, **kwargs): |
13 | | - super().__init__(*args, **kwargs) |
14 | | - self.docstore = DocstoreExplorer(Wikipedia()) |
| 24 | + wiki_searcher: WikipediaSearcher = Field(default_factory=WikipediaSearcher) |
15 | 25 |
|
16 | 26 | def _run(self, action_output: str, *args, **kwargs): |
17 | 27 | """Execute search or lookup based on action output""" |
@@ -60,7 +70,7 @@ def _handle_search(self, action_output: str) -> str: |
60 | 70 | return action_output |
61 | 71 |
|
62 | 72 | try: |
63 | | - result = self.docstore.search(search_term) |
| 73 | + result = self.wiki_searcher.search(search_term) |
64 | 74 | if result: |
65 | 75 | result_text = result.strip('\n').strip().replace('\n', '') |
66 | 76 |
|
|
0 commit comments