Learn base dialog2graph classes#

Graph and Dialog are the base structures used in the dialog2graph project. They are perfect to store information about your graphs and dialogs in a clear format.

Create dialog2graph.Graph#

Graph enables to create a directed dialog graph and work with it. You can visualise it and sample dialogs from it.

For experimenting with dialog graphs a set with generated data was created. It can be loaded from HuggingFace and contains 402 dialog graphs on various topics concerning custom support and other topics. See more information about available data on data collection page.

from datasets import load_dataset

dataset = load_dataset("DeepPavlov/d2g_generated", token=True)

First, initialize Graph by passing a dictionary with graph edges and nodes ({"edges": [...], "nodes": [...]}).

from dialog2graph import Graph

graph = Graph(graph_dict=dataset['train'][5]["graph"])

Then you can visualise the graph with all the utterances it contains.

graph.visualise()

Or you can visualise the graph in a more schematic way to see its general structure.

graph_title = "Schematic graph view"
graph.visualise_short(graph_title)

Create RecursiveDialogSampler#

RecursiveDialogSampler is a class that helps to sample dialogs from existing dialog graphs. It uses recursion to get all possible dialogs from the graph. To sample dialogs from the graph, create RecursiveDialogSampler instance and use invoke method to start sampling process.

from dialog2graph.pipelines.core.dialog_sampling import RecursiveDialogSampler
from langchain_openai import ChatOpenAI

sampler = RecursiveDialogSampler()
model = ChatOpenAI(model="gpt-3.5-turbo")
dialogs: list = sampler.invoke(graph=graph, upper_limit=10, cycle_ends_model=model)

The output of invoke method is a list of Dialog instances. This class is also helpful when working with dialog graphs.

type(dialogs[0])

Use dialog2graph.Dialog#

Dialog is a class that represents a complete dialog and provide method for visualisation and converting.

print(dialogs[0])
dialogs[0].to_list()