"git@developer.sourcefind.cn:OpenDAS/openpcdet.git" did not exist on "e15c16a70324db43d8f24c0f05aa18df762e77fc"
Commit 9c7b1ead authored by Graham King's avatar Graham King Committed by GitHub
Browse files

fix(pystr): Output python errors (#99)

If the python file raises an exception we print it like Python would.

```
$ ./target/debug/dynamo-run in=http out=pystr:~/Temp/cn47/1_e.py --model-name test

Traceback (most recent call last):
  File "/home/graham/Temp/cn47/1_e.py", line 17, in generate
    raise MyException("The message")
1_e.MyException: The message
```
parent a954a1c6
...@@ -188,17 +188,33 @@ async fn main_loop( ...@@ -188,17 +188,33 @@ async fn main_loop(
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();
let mut assistant_message = String::new(); let mut assistant_message = String::new();
while let Some(item) = stream.next().await { while let Some(item) = stream.next().await {
let data = item.data.as_ref().unwrap(); match (item.data.as_ref(), item.event.as_deref()) {
let entry = data.inner.choices.first(); (Some(data), _) => {
let chat_comp = entry.as_ref().unwrap(); // Normal case
if let Some(c) = &chat_comp.delta.content { let entry = data.inner.choices.first();
let _ = stdout.write(c.as_bytes()); let chat_comp = entry.as_ref().unwrap();
let _ = stdout.flush(); if let Some(c) = &chat_comp.delta.content {
assistant_message += c; let _ = stdout.write(c.as_bytes());
} let _ = stdout.flush();
if chat_comp.finish_reason.is_some() { assistant_message += c;
tracing::trace!("finish reason: {:?}", chat_comp.finish_reason.unwrap()); }
break; if chat_comp.finish_reason.is_some() {
tracing::trace!("finish reason: {:?}", chat_comp.finish_reason.unwrap());
break;
}
}
(None, Some("error")) => {
// There's only one error but we loop in case that changes
for err in item.comment.unwrap_or_default() {
tracing::error!("Engine error: {err}");
}
}
(None, Some(annotation)) => {
tracing::debug!("Annotation. {annotation}: {:?}", item.comment);
}
_ => {
unreachable!("Event from engine with no data, no error, no annotation.");
}
} }
} }
println!(); println!();
......
...@@ -234,17 +234,14 @@ where ...@@ -234,17 +234,14 @@ where
// todo: add task-local context to the python async generator // todo: add task-local context to the python async generator
ctx.stop_generating(); ctx.stop_generating();
let msg = format!("critical error: invalid response object from python async generator; application-logic-mismatch: {}", e); let msg = format!("critical error: invalid response object from python async generator; application-logic-mismatch: {}", e);
tracing::error!(request_id, "{}", msg);
msg msg
} }
ResponseProcessingError::PythonException(e) => { ResponseProcessingError::PythonException(e) => {
let msg = format!("a python exception was caught while processing the async generator: {}", e); let msg = format!("a python exception was caught while processing the async generator: {}", e);
tracing::warn!(request_id, "{}", msg);
msg msg
} }
ResponseProcessingError::OffloadError(e) => { ResponseProcessingError::OffloadError(e) => {
let msg = format!("critical error: failed to offload the python async generator to a new thread: {}", e); let msg = format!("critical error: failed to offload the python async generator to a new thread: {}", e);
tracing::error!(request_id, "{}", msg);
msg msg
} }
}; };
...@@ -288,8 +285,11 @@ async fn process_item<Resp>( ...@@ -288,8 +285,11 @@ async fn process_item<Resp>(
where where
Resp: Data + for<'de> Deserialize<'de>, Resp: Data + for<'de> Deserialize<'de>,
{ {
let item = item.map_err(|e| ResponseProcessingError::PythonException(e.to_string()))?; let item = item.map_err(|e| {
println!();
Python::with_gil(|py| e.display(py));
ResponseProcessingError::PythonException(e.to_string())
})?;
let response = tokio::task::spawn_blocking(move || { let response = tokio::task::spawn_blocking(move || {
Python::with_gil(|py| depythonize::<Resp>(&item.into_bound(py))) Python::with_gil(|py| depythonize::<Resp>(&item.into_bound(py)))
}) })
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment