docstrings `core` update (#14871)

Added missed docstrings
pull/14881/head
Leonid Ganeline 7 months ago committed by GitHub
parent d2cce54bf1
commit 14d04180eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -128,6 +128,15 @@ def aconfig_with_context(
config: RunnableConfig,
steps: List[Runnable],
) -> RunnableConfig:
"""Asynchronously patch a runnable config with context getters and setters.
Args:
config: The runnable config.
steps: The runnable steps.
Returns:
The patched runnable config.
"""
return _config_with_context(config, steps, _asetter, _agetter, asyncio.Event)
@ -135,10 +144,21 @@ def config_with_context(
config: RunnableConfig,
steps: List[Runnable],
) -> RunnableConfig:
"""Patch a runnable config with context getters and setters.
Args:
config: The runnable config.
steps: The runnable steps.
Returns:
The patched runnable config.
"""
return _config_with_context(config, steps, _setter, _getter, threading.Event)
class ContextGet(RunnableSerializable):
"""Get a context value."""
prefix: str = ""
key: Union[str, List[str]]
@ -197,6 +217,8 @@ def _coerce_set_value(value: SetValue) -> Runnable[Input, Output]:
class ContextSet(RunnableSerializable):
"""Set a context value."""
prefix: str = ""
keys: Mapping[str, Optional[Runnable]]
@ -276,8 +298,18 @@ class ContextSet(RunnableSerializable):
class Context:
"""Context for a runnable."""
@staticmethod
def create_scope(scope: str, /) -> "PrefixContext":
"""Create a context scope.
Args:
scope: The scope.
Returns:
The context scope.
"""
return PrefixContext(prefix=scope)
@staticmethod
@ -295,6 +327,8 @@ class Context:
class PrefixContext:
"""Context for a runnable with a prefix."""
prefix: str = ""
def __init__(self, prefix: str = ""):

@ -57,6 +57,8 @@ def _get_verbosity() -> bool:
def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:
"""Generate from a stream."""
generation: Optional[ChatGenerationChunk] = None
for chunk in stream:
if generation is None:
@ -77,6 +79,8 @@ def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:
async def agenerate_from_stream(
stream: AsyncIterator[ChatGenerationChunk],
) -> ChatResult:
"""Async generate from a stream."""
generation: Optional[ChatGenerationChunk] = None
async for chunk in stream:
if generation is None:

@ -32,6 +32,16 @@ class SerializedNotImplemented(BaseSerialized):
def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
"""Try to determine if a value is different from the default.
Args:
value: The value.
key: The key.
model: The model.
Returns:
Whether the value is different from the default.
"""
try:
return model.__fields__[key].get_default() != value
except Exception:

@ -47,6 +47,15 @@ def merge_content(
first_content: Union[str, List[Union[str, Dict]]],
second_content: Union[str, List[Union[str, Dict]]],
) -> Union[str, List[Union[str, Dict]]]:
"""Merge two message contents.
Args:
first_content: The first content.
second_content: The second content.
Returns:
The merged content.
"""
# If first chunk is a string
if isinstance(first_content, str):
# If the second chunk is also a string, then merge them naively
@ -146,6 +155,14 @@ class BaseMessageChunk(BaseMessage):
def message_to_dict(message: BaseMessage) -> dict:
"""Convert a Message to a dictionary.
Args:
message: Message to convert.
Returns:
Message as a dict.
"""
return {"type": message.type, "data": message.dict()}

@ -422,6 +422,18 @@ def _strremoveprefix(s: str, prefix: str) -> str:
def prefix_config_spec(
spec: ConfigurableFieldSpec, prefix: str
) -> ConfigurableFieldSpec:
"""Prefix the id of a ConfigurableFieldSpec.
This is useful when a RunnableConfigurableAlternatives is used as a
ConfigurableField of another RunnableConfigurableAlternatives.
Args:
spec: The ConfigurableFieldSpec to prefix.
prefix: The prefix to add.
Returns:
"""
return (
ConfigurableFieldSpec(
id=f"{prefix}/{spec.id}",

@ -205,6 +205,19 @@ def register_configure_hook(
handle_class: Optional[Type[BaseCallbackHandler]] = None,
env_var: Optional[str] = None,
) -> None:
"""Register a configure hook.
Args:
context_var (ContextVar[Optional[Any]]): The context variable.
inheritable (bool): Whether the context variable is inheritable.
handle_class (Optional[Type[BaseCallbackHandler]], optional):
The callback handler class. Defaults to None.
env_var (Optional[str], optional): The environment variable. Defaults to None.
Raises:
ValueError: If env_var is set, handle_class must also be set
to a non-None value.
"""
if env_var is not None and handle_class is None:
raise ValueError(
"If env_var is set, handle_class must also be set to a non-None value."

@ -12,6 +12,8 @@ Listener = Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
class RootListenersTracer(BaseTracer):
"""A tracer that calls listeners on run start, end, and error."""
def __init__(
self,
*,

Loading…
Cancel
Save