コード例 #1
0
    def _process(self, input_pack: DataPack):
        # Make sure we take the last utterance from the user.
        utterance: Optional[Utterance] = get_last_utterance(input_pack, "user")

        if utterance is not None:
            logging.info("The last utterance is %s", utterance)
            # Create the query using the last utterance from user.
            size = self.configs.size or 1000
            field = self.configs.field or "content"
            query_value = {
                "query": {"match": {field: utterance.text}},
                "size": size,
            }

            # Search against the index.
            results = self.index.search(query_value)
            hits = results["hits"]["hits"]

            conn = sqlite3.connect(self.configs.stave_db_path)

            answers = []
            for idx, hit in enumerate(hits):
                source = hit["_source"]
                # The raw pack string and pack id (not database id)
                raw_pack_str: str = source["pack_info"]
                pack_id: str = source["doc_id"]

                # Now you can write the pack into the database and generate url.
                item = {
                    "name": f"clinical_results_{idx}",
                    "textPack": raw_pack_str,
                    "project_id": 5,
                }
                db_id = sqlite_insert(conn, "stave_backend_document", item)
                answers += [db_id]
                print(pack_id, db_id)

            if len(answers) == 0:
                create_utterance(
                    input_pack,
                    "No results found. Please try another query.",
                    "ai",
                )
            else:
                links: List[str] = create_links(self.configs.url_stub, answers)
                response_text: str = (
                    "I found the following results: <br> -- "
                    + "<br> -- ".join(links)
                )
                print(response_text)

                create_utterance(input_pack, response_text, "ai")
        else:
            logging.info("Cannot get another utterance.")
            create_utterance(
                input_pack,
                "Hey, I didn't get what you say, could you try again?",
                "ai",
            )
コード例 #2
0
    def _process(self, input_pack: DataPack):
        utterance: Optional[Utterance] = get_last_utterance(input_pack, "user")
        if utterance is None:
            logging.info("Cannot get new user utterance. Showing initials.")
            create_utterance(input_pack, random.choice(self.initials), "ai")
        else:
            output = self._respond(utterance.text)

            if output is None:
                create_utterance(input_pack, random.choice(self.finals), "ai")
            else:
                create_utterance(input_pack, output, "ai")
コード例 #3
0
 def _process(self, input_pack: DataPack):
     create_utterance(input_pack, self.configs.user_input, "user")