Wrap a Python callable `(Any) -> None` as a collector for streaming LLM calls. The collector is invoked with each intercepted chunk (after stream response intercepts have been applied). It receives a single JSON-converted Python object argument. If the Python callable raises an exception, it is converted to a `FlowError::Internal` and returned as `Err`, which terminates the stream. If the callabl
(
py_fn: Py<PyAny>,
)
| 780 | /// stream. If the callable returns normally (including `None`), the collector |
| 781 | /// returns `Ok(())`. |
| 782 | pub fn wrap_py_collector_fn( |
| 783 | py_fn: Py<PyAny>, |
| 784 | ) -> Box<dyn FnMut(Json) -> std::result::Result<(), FlowError> + Send> { |
| 785 | Box::new(move |chunk: Json| { |
| 786 | Python::attach(|py| { |
| 787 | let py_chunk = json_to_py(py, &chunk) |
| 788 | .map_err(|e| FlowError::Internal(format!("collector json_to_py failed: {e}")))?; |
| 789 | py_fn |
| 790 | .call1(py, (py_chunk,)) |
| 791 | .map_err(|e| FlowError::Internal(format!("Python collector error: {e}")))?; |
| 792 | Ok(()) |
| 793 | }) |
| 794 | }) |
| 795 | } |
| 796 | |
| 797 | /// Wrap a Python callable `() -> Any` as a finalizer for streaming LLM calls. |
| 798 | /// |