File size: 3,080 Bytes
cbb225c
 
 
 
93c74cc
1beb8a2
93c74cc
cbb225c
 
 
 
 
 
40a0d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbb225c
 
 
 
 
 
 
 
 
 
 
1beb8a2
40a0d94
 
1beb8a2
 
40a0d94
1beb8a2
cbb225c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1beb8a2
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
        # ~~~ Process input ~~~
        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"]
        )

        # ~~~ Call ~~~
        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}"}

        # Log the update to the flow messages list
        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)