Abstract base class for creating structured sequences of calls to components. Chains should be used to encode a sequence of calls to components like models, document retrievers, other chains, etc., and provide a simple interface to this sequence. Copied from langchain v0.0.283.
| 36 | |
| 37 | |
| 38 | class Chain(Serializable, Runnable[Dict[str, Any], Dict[str, Any]], ABC): |
| 39 | """Abstract base class for creating structured sequences of calls to components. |
| 40 | |
| 41 | Chains should be used to encode a sequence of calls to components like |
| 42 | models, document retrievers, other chains, etc., and provide a simple interface |
| 43 | to this sequence. |
| 44 | |
| 45 | Copied from langchain v0.0.283. |
| 46 | |
| 47 | The Chain interface makes it easy to create apps that are: |
| 48 | - Stateful: add Memory to any Chain to give it state, |
| 49 | - Observable: pass Callbacks to a Chain to execute additional functionality, |
| 50 | like logging, outside the main sequence of component calls, |
| 51 | - Composable: the Chain API is flexible enough that it is easy to combine |
| 52 | Chains with other components, including other Chains. |
| 53 | |
| 54 | The main methods exposed by chains are: |
| 55 | - `__call__`: Chains are callable. The `__call__` method is the primary way to |
| 56 | execute a Chain. This takes inputs as a dictionary and returns a |
| 57 | dictionary output. |
| 58 | - `run`: A convenience method that takes inputs as args/kwargs and returns the |
| 59 | output as a string or object. This method can only be used for a subset of |
| 60 | chains and cannot return as rich of an output as `__call__`. |
| 61 | """ |
| 62 | |
| 63 | def invoke( |
| 64 | self, |
| 65 | input: Dict[str, Any], |
| 66 | config: Optional[RunnableConfig] = None, |
| 67 | **kwargs: Any, |
| 68 | ) -> Dict[str, Any]: |
| 69 | config = config or {} |
| 70 | return self( |
| 71 | input, |
| 72 | callbacks=config.get("callbacks"), |
| 73 | tags=config.get("tags"), |
| 74 | metadata=config.get("metadata"), |
| 75 | run_name=config.get("run_name"), |
| 76 | **kwargs, |
| 77 | ) |
| 78 | |
| 79 | async def ainvoke( |
| 80 | self, |
| 81 | input: Dict[str, Any], |
| 82 | config: Optional[RunnableConfig] = None, |
| 83 | **kwargs: Any, |
| 84 | ) -> Dict[str, Any]: |
| 85 | if type(self)._acall == Chain._acall: |
| 86 | # If the chain does not implement async, fall back to default implementation |
| 87 | return await asyncio.get_running_loop().run_in_executor( |
| 88 | None, partial(self.invoke, input, config, **kwargs) |
| 89 | ) |
| 90 | |
| 91 | config = config or {} |
| 92 | return await self.acall( |
| 93 | input, |
| 94 | callbacks=config.get("callbacks"), |
| 95 | tags=config.get("tags"), |
nothing calls this directly
no outgoing calls
no test coverage detected