|
from copy import deepcopy |
|
|
|
from typing import List, Dict, Optional, Any |
|
|
|
from aiflows.base_flows import AtomicFlow |
|
from aiflows.messages import FlowMessage |
|
from aiflows.utils import logging |
|
from .wikipediaAPI import WikipediaAPIWrapper |
|
|
|
log = logging.get_logger(__name__) |
|
|
|
|
|
class WikiSearchAtomicFlow(AtomicFlow): |
|
""" This class implements a WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries. |
|
|
|
*Configuration Parameters*: |
|
|
|
- `name` (str): The name of the flow. Default: "WikiSearchAtomicFlow" |
|
- `description` (str): A description of the flow. This description is used to generate the help message of the flow. |
|
Default: "A Flow that queries the wikipedia API for a page content." |
|
- `lang` (str): The language of the Wikipedia page. Default: "en" |
|
- `top_k_results` (int): The number of top results to return. Default: 5 |
|
- `doc_content_chars_max` (int): The maximum number of characters of the content of the Wikipedia page. Default: 3000 |
|
- Other parameters are inherited from the default configuration of AtomicFlow (see AtomicFlow) |
|
|
|
*input_interface*: |
|
|
|
- `search_term` (str): The search term to search for. |
|
|
|
*output_interface*: |
|
|
|
- `wiki_content` (str): The content of the Wikipedia page. |
|
|
|
:param \**kwargs: The keyword arguments passed to the AtomicFlow constructor |
|
""" |
|
REQUIRED_KEYS_CONFIG = ["lang", "top_k_results", "doc_content_chars_max"] |
|
REQUIRED_KEYS_CONSTRUCTOR = [] |
|
|
|
SUPPORTS_CACHING: bool = True |
|
|
|
api_wrapper: WikipediaAPIWrapper |
|
|
|
def __init__(self, **kwargs): |
|
super().__init__(**kwargs) |
|
|
|
def run(self, |
|
input_message: FlowMessage): |
|
""" Runs the WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries. |
|
|
|
:param input_message: The input message |
|
:type input_message: FlowMessage |
|
""" |
|
input_data = input_message.data |
|
|
|
term = input_data.get("search_term", None) |
|
api_wrapper = WikipediaAPIWrapper( |
|
lang=self.flow_config["lang"], |
|
top_k_results=self.flow_config["top_k_results"], |
|
doc_content_chars_max=self.flow_config["doc_content_chars_max"] |
|
) |
|
|
|
|
|
if page_content := api_wrapper._fetch_page(term): |
|
search_response = {"wiki_content": page_content, "relevant_pages": None} |
|
else: |
|
page_titles = api_wrapper.search_page_titles(term) |
|
search_response = {"wiki_content": None, "relevant_pages": f"Could not find [{term}]. similar: {page_titles}"} |
|
|
|
|
|
observation = search_response["wiki_content"] if search_response["wiki_content"] else search_response["relevant_pages"] |
|
|
|
reply = self._package_output_message( |
|
input_message = input_message, |
|
response = {"wiki_content": observation}, |
|
) |
|
|
|
self.reply_to_message(reply=reply, to=input_message) |
|
|