MCPcopy Create free account
hub / github.com/NVIDIA/NeMo-Relay / wrap_py_llm_exec_fn

Function wrap_py_llm_exec_fn

crates/python/src/py_callable.rs:719–737  ·  view source on GitHub ↗

Wrap a Python callable `(LlmRequest) -> dict` for LLM execution. Supports both sync and async Python callables.

(
    py_fn: Py<PyAny>,
)

Source from the content-addressed store, hash-verified

717/// Wrap a Python callable `(LlmRequest) -> dict` for LLM execution.
718/// Supports both sync and async Python callables.
719pub 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.

Calls 2

resolve_json_or_futureFunction · 0.85
split_json_or_futureFunction · 0.85