Wrap a Python callable `(LlmRequest) -> dict` for LLM execution. Supports both sync and async Python callables.
(
py_fn: Py<PyAny>,
)
| 717 | /// Wrap a Python callable `(LlmRequest) -> dict` for LLM execution. |
| 718 | /// Supports both sync and async Python callables. |
| 719 | pub fn wrap_py_llm_exec_fn( |
| 720 | py_fn: Py<PyAny>, |
| 721 | ) -> Box<dyn Fn(LlmRequest) -> Pin<Box<dyn Future<Output = FlowResult<Json>> + Send>> + Send + Sync> |
| 722 | { |
| 723 | let py_fn = std::sync::Arc::new(py_fn); |
| 724 | Box::new(move |request: LlmRequest| { |
| 725 | let py_fn = py_fn.clone(); |
| 726 | Box::pin(async move { |
| 727 | resolve_json_or_future(Python::attach(|py| { |
| 728 | let py_req = PyLLMRequest { inner: request }; |
| 729 | let result = py_fn |
| 730 | .call1(py, (py_req,)) |
| 731 | .map_err(|e: PyErr| FlowError::Internal(e.to_string()))?; |
| 732 | split_json_or_future(py, result) |
| 733 | })) |
| 734 | .await |
| 735 | }) |
| 736 | }) |
| 737 | } |
| 738 | |
| 739 | /// Wrap a Python async generator `(LlmRequest) -> AsyncIterator[Any]` for LLM |
| 740 | /// stream execution. |