Skip to content

How to Get dialogues from the Database

Dilyara Baymurzina edited this page Nov 22, 2022 · 2 revisions

How to Get dialogues from the Database

Dialogues are stored in the mongodb database. So, to get an access to the dialogues conducted with your chatbot, it would be easier to use special endpoints provided by DeepPavlov Dream.

Attention! The dialogues can be downloaded only when the agent is hosted with http NOT via Telegram! If you host agent via Telegram, re-built agent with http and use the method described below.

Consider agent is hosted locally (even if all the components are proxied, agent and mongo are always local) and available on http://0.0.0.0:4242. By the way, one can utilize web-interface for chatting with the chatbot http://0.0.0.0:4242/chat. Conducted dialogues identifiers are available at the address http://0.0.0.0:4242/api/dialogs/:

import requests


requests.get("http://0.0.0.0:4242/api/dialogs/").json()
>>{'dialog_ids': ['blablaidentifier', ...], 'next': None}

Now we can get the full stored dialogue state of the dialogue with blablaidentifier identifier as http://0.0.0.0:4242/api/dialogs/blablaidentifier.

Finally, one can utilize the following function to get and save dialogues locally:

import json
import requests
from datetime import date, datetime


def get_database_from_agent(agent_url="http://0.0.0.0:4242", fname=None):
    DATABASE_URL = f"{agent_url}/api/dialogs/"
    database = requests.get(DATABASE_URL).json()
    print(database)
    timestamp = datetime.now().timestamp()
    date_time = datetime.fromtimestamp(timestamp)
    str_date_time= date_time.strftime("%d%m%Y-%H%M%S")
    dialogs = []

    for dialog_id in database["dialog_ids"]:
        dialogs += [requests.get(f"{DATABASE_URL}{dialog_id}").json()]
    
    if fname:
        with open(fname, "w") as f:
            json.dump(dialogs, f)
    else:
        with open(f"loaded_database_{str_date_time}.json", "w") as f:
            json.dump(dialogs, f)