Wrap a Python callable `() -> Any` as a finalizer for streaming LLM calls. The finalizer is called once when the stream is fully consumed. Its return value is converted from a Python object to `serde_json::Value` (Json) and used as the aggregated response.
(py_fn: Py<PyAny>)
| 800 | /// value is converted from a Python object to `serde_json::Value` (Json) and |
| 801 | /// used as the aggregated response. |
| 802 | pub fn wrap_py_finalizer_fn(py_fn: Py<PyAny>) -> Box<dyn FnOnce() -> Json + Send> { |
| 803 | Box::new(move || { |
| 804 | Python::attach(|py| { |
| 805 | let result = match py_fn.call0(py) { |
| 806 | Ok(v) => v, |
| 807 | Err(e) => { |
| 808 | eprintln!("nemo_relay: Python finalizer callable failed: {e}"); |
| 809 | return Json::Null; |
| 810 | } |
| 811 | }; |
| 812 | py_to_json(result.bind(py)).unwrap_or_else(|e| { |
| 813 | eprintln!("nemo_relay: py_to_json failed in finalizer: {e}"); |
| 814 | Json::Null |
| 815 | }) |
| 816 | }) |
| 817 | }) |
| 818 | } |
| 819 | |
| 820 | /// Wrap a Python callable `(dict) -> dict` for LLM sanitize response guardrails. |
| 821 | pub fn wrap_py_llm_sanitize_response_fn(py_fn: Py<PyAny>) -> LlmSanitizeResponseFn { |