Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
400 changes: 0 additions & 400 deletions libs/langchain/langchain_classic/chains/api/base.py

This file was deleted.

274 changes: 1 addition & 273 deletions libs/langchain/langchain_classic/chains/base.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Base interface that all chains should implement."""

import builtins
import contextlib
import inspect
import json
import logging
import warnings
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, cast
from typing import Any

import yaml
from langchain_core._api import deprecated
from langchain_core.callbacks import (
AsyncCallbackManager,
AsyncCallbackManagerForChainRun,
Expand Down Expand Up @@ -365,109 +363,6 @@ async def _acall(
run_manager.get_sync() if run_manager else None,
)

@deprecated("0.1.0", alternative="invoke", removal="1.0")
def __call__(
self,
inputs: dict[str, Any] | Any,
return_only_outputs: bool = False, # noqa: FBT001,FBT002
callbacks: Callbacks = None,
*,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
run_name: str | None = None,
include_run_info: bool = False,
) -> dict[str, Any]:
"""Execute the chain.

Args:
inputs: Dictionary of inputs, or single input if chain expects
only one param. Should contain all inputs specified in
`Chain.input_keys` except for inputs that will be set by the chain's
memory.
return_only_outputs: Whether to return only outputs in the
response. If `True`, only new keys generated by this chain will be
returned. If `False`, both input keys and new keys generated by this
chain will be returned. Defaults to `False`.
callbacks: Callbacks to use for this chain run. These will be called in
addition to callbacks passed to the chain during construction, but only
these runtime callbacks will propagate to calls to other objects.
tags: List of string tags to pass to all callbacks. These will be passed in
addition to tags passed to the chain during construction, but only
these runtime tags will propagate to calls to other objects.
metadata: Optional metadata associated with the chain. Defaults to `None`.
run_name: Optional name for this run of the chain.
include_run_info: Whether to include run info in the response. Defaults
to False.

Returns:
A dict of named outputs. Should contain all outputs specified in
`Chain.output_keys`.
"""
config = {
"callbacks": callbacks,
"tags": tags,
"metadata": metadata,
"run_name": run_name,
}

return self.invoke(
inputs,
cast("RunnableConfig", {k: v for k, v in config.items() if v is not None}),
return_only_outputs=return_only_outputs,
include_run_info=include_run_info,
)

@deprecated("0.1.0", alternative="ainvoke", removal="1.0")
async def acall(
self,
inputs: dict[str, Any] | Any,
return_only_outputs: bool = False, # noqa: FBT001,FBT002
callbacks: Callbacks = None,
*,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
run_name: str | None = None,
include_run_info: bool = False,
) -> dict[str, Any]:
"""Asynchronously execute the chain.

Args:
inputs: Dictionary of inputs, or single input if chain expects
only one param. Should contain all inputs specified in
`Chain.input_keys` except for inputs that will be set by the chain's
memory.
return_only_outputs: Whether to return only outputs in the
response. If `True`, only new keys generated by this chain will be
returned. If `False`, both input keys and new keys generated by this
chain will be returned. Defaults to `False`.
callbacks: Callbacks to use for this chain run. These will be called in
addition to callbacks passed to the chain during construction, but only
these runtime callbacks will propagate to calls to other objects.
tags: List of string tags to pass to all callbacks. These will be passed in
addition to tags passed to the chain during construction, but only
these runtime tags will propagate to calls to other objects.
metadata: Optional metadata associated with the chain. Defaults to `None`.
run_name: Optional name for this run of the chain.
include_run_info: Whether to include run info in the response. Defaults
to False.

Returns:
A dict of named outputs. Should contain all outputs specified in
`Chain.output_keys`.
"""
config = {
"callbacks": callbacks,
"tags": tags,
"metadata": metadata,
"run_name": run_name,
}
return await self.ainvoke(
inputs,
cast("RunnableConfig", {k: v for k, v in config.items() if k is not None}),
return_only_outputs=return_only_outputs,
include_run_info=include_run_info,
)

def prep_outputs(
self,
inputs: dict[str, str],
Expand Down Expand Up @@ -576,164 +471,6 @@ def _run_output_key(self) -> str:
raise ValueError(msg)
return self.output_keys[0]

@deprecated("0.1.0", alternative="invoke", removal="1.0")
def run(
self,
*args: Any,
callbacks: Callbacks = None,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> Any:
"""Convenience method for executing chain.

The main difference between this method and `Chain.__call__` is that this
method expects inputs to be passed directly in as positional arguments or
keyword arguments, whereas `Chain.__call__` expects a single input dictionary
with all the inputs

Args:
*args: If the chain expects a single input, it can be passed in as the
sole positional argument.
callbacks: Callbacks to use for this chain run. These will be called in
addition to callbacks passed to the chain during construction, but only
these runtime callbacks will propagate to calls to other objects.
tags: List of string tags to pass to all callbacks. These will be passed in
addition to tags passed to the chain during construction, but only
these runtime tags will propagate to calls to other objects.
metadata: Optional metadata associated with the chain.
**kwargs: If the chain expects multiple inputs, they can be passed in
directly as keyword arguments.

Returns:
The chain output.

Example:
.. code-block:: python

# Suppose we have a single-input chain that takes a 'question' string:
chain.run("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
chain.run(question=question, context=context)
# -> "The temperature in Boise is..."

"""
# Run at start to make sure this is possible/defined
_output_key = self._run_output_key

if args and not kwargs:
if len(args) != 1:
msg = "`run` supports only one positional argument."
raise ValueError(msg)
return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[
_output_key
]

if kwargs and not args:
return self(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)[
_output_key
]

if not kwargs and not args:
msg = (
"`run` supported with either positional arguments or keyword arguments,"
" but none were provided."
)
raise ValueError(msg)
msg = (
f"`run` supported with either positional arguments or keyword arguments"
f" but not both. Got args: {args} and kwargs: {kwargs}."
)
raise ValueError(msg)

@deprecated("0.1.0", alternative="ainvoke", removal="1.0")
async def arun(
self,
*args: Any,
callbacks: Callbacks = None,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> Any:
"""Convenience method for executing chain.

The main difference between this method and `Chain.__call__` is that this
method expects inputs to be passed directly in as positional arguments or
keyword arguments, whereas `Chain.__call__` expects a single input dictionary
with all the inputs


Args:
*args: If the chain expects a single input, it can be passed in as the
sole positional argument.
callbacks: Callbacks to use for this chain run. These will be called in
addition to callbacks passed to the chain during construction, but only
these runtime callbacks will propagate to calls to other objects.
tags: List of string tags to pass to all callbacks. These will be passed in
addition to tags passed to the chain during construction, but only
these runtime tags will propagate to calls to other objects.
metadata: Optional metadata associated with the chain.
**kwargs: If the chain expects multiple inputs, they can be passed in
directly as keyword arguments.

Returns:
The chain output.

Example:
.. code-block:: python

# Suppose we have a single-input chain that takes a 'question' string:
await chain.arun("What's the temperature in Boise, Idaho?")
# -> "The temperature in Boise is..."

# Suppose we have a multi-input chain that takes a 'question' string
# and 'context' string:
question = "What's the temperature in Boise, Idaho?"
context = "Weather report for Boise, Idaho on 07/03/23..."
await chain.arun(question=question, context=context)
# -> "The temperature in Boise is..."

"""
if len(self.output_keys) != 1:
msg = (
f"`run` not supported when there is not exactly "
f"one output key. Got {self.output_keys}."
)
raise ValueError(msg)
if args and not kwargs:
if len(args) != 1:
msg = "`run` supports only one positional argument."
raise ValueError(msg)
return (
await self.acall(
args[0],
callbacks=callbacks,
tags=tags,
metadata=metadata,
)
)[self.output_keys[0]]

if kwargs and not args:
return (
await self.acall(
kwargs,
callbacks=callbacks,
tags=tags,
metadata=metadata,
)
)[self.output_keys[0]]

msg = (
f"`run` supported with either positional arguments or keyword arguments"
f" but not both. Got args: {args} and kwargs: {kwargs}."
)
raise ValueError(msg)

def dict(self, **kwargs: Any) -> dict:
"""Dictionary representation of chain.

Expand Down Expand Up @@ -799,12 +536,3 @@ def save(self, file_path: Path | str) -> None:
else:
msg = f"{save_path} must be json or yaml"
raise ValueError(msg)

@deprecated("0.1.0", alternative="batch", removal="1.0")
def apply(
self,
input_list: list[builtins.dict[str, Any]],
callbacks: Callbacks = None,
) -> list[builtins.dict[str, str]]:
"""Call the chain on all inputs in the list."""
return [self(inputs, callbacks=callbacks) for inputs in input_list]
Loading
Loading