Wrap a Python callable `(dict) -> dict` for LLM sanitize response guardrails.
(py_fn: Py<PyAny>)
| 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 { |
| 822 | Arc::new(move |response: Json| { |
| 823 | Python::attach(|py| { |
| 824 | let py_resp = match json_to_py(py, &response) { |
| 825 | Ok(v) => v, |
| 826 | Err(e) => { |
| 827 | eprintln!( |
| 828 | "nemo_relay: json_to_py failed in LLM sanitize response guardrail: {e}" |
| 829 | ); |
| 830 | return response.clone(); |
| 831 | } |
| 832 | }; |
| 833 | let result = match py_fn.call1(py, (py_resp,)) { |
| 834 | Ok(v) => v, |
| 835 | Err(e) => { |
| 836 | eprintln!("nemo_relay: LLM sanitize response guardrail callable failed: {e}"); |
| 837 | return response.clone(); |
| 838 | } |
| 839 | }; |
| 840 | py_to_json(result.bind(py)).unwrap_or_else(|e| { |
| 841 | eprintln!("nemo_relay: py_to_json failed in LLM sanitize response guardrail: {e}"); |
| 842 | response.clone() |
| 843 | }) |
| 844 | }) |
| 845 | }) |
| 846 | } |
| 847 | |
| 848 | /// Wrap a Python callable `(Event) -> None` for event subscribers. |
| 849 | pub fn wrap_py_event_subscriber(py_fn: Py<PyAny>) -> EventSubscriberFn { |