core[patch[: docstring update (#21036)

Added missed docstrings. Updated docstrings to consistent format.
This commit is contained in:
Leonid Ganeline
2024-04-29 12:35:34 -07:00
committed by GitHub
parent f479a337cc
commit 1a2ff56cd8
20 changed files with 159 additions and 38 deletions

View File

@@ -641,7 +641,8 @@ def make_options_spec(
description: Optional[str],
) -> ConfigurableFieldSpec:
"""Make a ConfigurableFieldSpec for a ConfigurableFieldSingleOption or
ConfigurableFieldMultiOption."""
ConfigurableFieldMultiOption.
"""
with _enums_for_spec_lock:
if enum := _enums_for_spec.get(spec):
pass

View File

@@ -26,11 +26,23 @@ if TYPE_CHECKING:
class LabelsDict(TypedDict):
"""Dictionary of labels for nodes and edges in a graph."""
nodes: dict[str, str]
"""Labels for nodes."""
edges: dict[str, str]
"""Labels for edges."""
def is_uuid(value: str) -> bool:
"""Check if a string is a valid UUID.
Args:
value: The string to check.
Returns:
True if the string is a valid UUID, False otherwise.
"""
try:
UUID(value)
return True
@@ -95,6 +107,14 @@ class MermaidDrawMethod(Enum):
def node_data_str(node: Node) -> str:
"""Convert the data of a node to a string.
Args:
node: The node to convert.
Returns:
A string representation of the data.
"""
from langchain_core.runnables.base import Runnable
if not is_uuid(node.id):
@@ -120,6 +140,16 @@ def node_data_str(node: Node) -> str:
def node_data_json(
node: Node, *, with_schemas: bool = False
) -> Dict[str, Union[str, Dict[str, Any]]]:
"""Convert the data of a node to a JSON-serializable format.
Args:
node: The node to convert.
with_schemas: Whether to include the schema of the data if
it is a Pydantic model.
Returns:
A dictionary with the type of the data and the data itself.
"""
from langchain_core.load.serializable import to_json_not_implemented
from langchain_core.runnables.base import Runnable, RunnableSerializable

View File

@@ -4,9 +4,9 @@ from langchain_core.runnables.graph import Graph, LabelsDict
class PngDrawer:
"""
A helper class to draw a state graph into a PNG file.
Requires graphviz and pygraphviz to be installed.
"""Helper class to draw a state graph into a PNG file.
It requires graphviz and pygraphviz to be installed.
:param fontname: The font to use for the labels
:param labels: A dictionary of label overrides. The dictionary
should have the following format:
@@ -30,18 +30,62 @@ class PngDrawer:
def __init__(
self, fontname: Optional[str] = None, labels: Optional[LabelsDict] = None
) -> None:
"""Initializes the PNG drawer.
Args:
fontname: The font to use for the labels
labels: A dictionary of label overrides. The dictionary
should have the following format:
{
"nodes": {
"node1": "CustomLabel1",
"node2": "CustomLabel2",
"__end__": "End Node"
},
"edges": {
"continue": "ContinueLabel",
"end": "EndLabel"
}
}
The keys are the original labels, and the values are the new labels.
"""
self.fontname = fontname or "arial"
self.labels = labels or LabelsDict(nodes={}, edges={})
def get_node_label(self, label: str) -> str:
"""Returns the label to use for a node.
Args:
label: The original label
Returns:
The new label.
"""
label = self.labels.get("nodes", {}).get(label, label)
return f"<<B>{label}</B>>"
def get_edge_label(self, label: str) -> str:
"""Returns the label to use for an edge.
Args:
label: The original label
Returns:
The new label.
"""
label = self.labels.get("edges", {}).get(label, label)
return f"<<U>{label}</U>>"
def add_node(self, viz: Any, node: str) -> None:
"""Adds a node to the graph.
Args:
viz: The graphviz object
node: The node to add
Returns:
None
"""
viz.add_node(
node,
label=self.get_node_label(node),
@@ -59,6 +103,18 @@ class PngDrawer:
label: Optional[str] = None,
conditional: bool = False,
) -> None:
"""Adds an edge to the graph.
Args:
viz: The graphviz object
source: The source node
target: The target node
label: The label for the edge. Defaults to None.
conditional: Whether the edge is conditional. Defaults to False.
Returns:
None
"""
viz.add_edge(
source,
target,
@@ -69,8 +125,8 @@ class PngDrawer:
)
def draw(self, graph: Graph, output_path: Optional[str] = None) -> Optional[bytes]:
"""
Draws the given state graph into a PNG file.
"""Draw the given state graph into a PNG file.
Requires graphviz and pygraphviz to be installed.
:param graph: The graph to draw
:param output_path: The path to save the PNG. If None, PNG bytes are returned.

View File

@@ -329,8 +329,7 @@ _graph_passthrough: RunnablePassthrough = RunnablePassthrough()
class RunnableAssign(RunnableSerializable[Dict[str, Any], Dict[str, Any]]):
"""
A runnable that assigns key-value pairs to Dict[str, Any] inputs.
"""Runnable that assigns key-value pairs to Dict[str, Any] inputs.
The `RunnableAssign` class takes input dictionaries and, through a
`RunnableParallel` instance, applies transformations, then combines

View File

@@ -507,6 +507,15 @@ def create_model(
__model_name: str,
**field_definitions: Any,
) -> Type[BaseModel]:
"""Create a pydantic model with the given field definitions.
Args:
__model_name: The name of the model.
**field_definitions: The field definitions for the model.
Returns:
Type[BaseModel]: The created model.
"""
try:
return _create_model_cached(__model_name, **field_definitions)
except TypeError: