mirror of
https://github.com/kennethreitz/langchain.git
synced 2026-06-05 23:00:18 +00:00
d3ec00b566
Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Davis Chase <130488702+dev2049@users.noreply.github.com> Co-authored-by: Zander Chase <130414180+vowelparrot@users.noreply.github.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""Chain that runs an arbitrary python function."""
|
|
from typing import Callable, Dict, List, Optional
|
|
|
|
from langchain.callbacks.manager import CallbackManagerForChainRun
|
|
from langchain.chains.base import Chain
|
|
|
|
|
|
class TransformChain(Chain):
|
|
"""Chain transform chain output.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
from langchain import TransformChain
|
|
transform_chain = TransformChain(input_variables=["text"],
|
|
output_variables["entities"], transform=func())
|
|
"""
|
|
|
|
input_variables: List[str]
|
|
output_variables: List[str]
|
|
transform: Callable[[Dict[str, str]], Dict[str, str]]
|
|
|
|
@property
|
|
def input_keys(self) -> List[str]:
|
|
"""Expect input keys.
|
|
|
|
:meta private:
|
|
"""
|
|
return self.input_variables
|
|
|
|
@property
|
|
def output_keys(self) -> List[str]:
|
|
"""Return output keys.
|
|
|
|
:meta private:
|
|
"""
|
|
return self.output_variables
|
|
|
|
def _call(
|
|
self,
|
|
inputs: Dict[str, str],
|
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
) -> Dict[str, str]:
|
|
return self.transform(inputs)
|