Skip to content

Commit

Permalink
Add unstable_internal_repr on FunctionResult in python (#1068)
Browse files Browse the repository at this point in the history
This exposes the very last web request with some stats. Retries and such
are not available.

<!-- ELLIPSIS_HIDDEN -->


----

> [!IMPORTANT]
> Add `unstable_internal_repr` method to `FunctionResult` for debugging
and serialize related Rust structs.
> 
>   - **Functionality**:
> - Add `unstable_internal_repr()` method to `FunctionResult` in
`baml_py.pyi` and `function_results.rs` for debugging internal response
representation.
>   - **Serialization**:
>     - Add `Serialize` derive to `RenderedPrompt` in `lib.rs`.
> - Add `Serialize` derive to `LLMResponse`, `LLMErrorResponse`,
`ErrorCode`, and `LLMCompleteResponse` in `mod.rs`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 0d85cd9. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
hellovai authored Oct 21, 2024
1 parent a131336 commit 00082e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion engine/baml-lib/jinja-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl ImageBase64 {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum RenderedPrompt {
Completion(String),
Chat(Vec<RenderedChatMessage>),
Expand Down
10 changes: 6 additions & 4 deletions engine/baml-runtime/src/internal/llm_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct RetryLLMResponse {
pub failed: Vec<LLMResponse>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub enum LLMResponse {
/// BAML was able to successfully make the HTTP request and got a 2xx
/// response from the model provider
Expand Down Expand Up @@ -148,12 +148,13 @@ impl LLMResponse {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct LLMErrorResponse {
pub client: String,
pub model: Option<String>,
pub prompt: RenderedPrompt,
pub request_options: HashMap<String, serde_json::Value>,
#[cfg_attr(target_arch = "wasm32", serde(skip_serializing))]
pub start_time: web_time::SystemTime,
pub latency: web_time::Duration,

Expand All @@ -162,7 +163,7 @@ pub struct LLMErrorResponse {
pub code: ErrorCode,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub enum ErrorCode {
InvalidAuthentication, // 401
NotSupported, // 403
Expand Down Expand Up @@ -225,13 +226,14 @@ impl ErrorCode {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct LLMCompleteResponse {
pub client: String,
pub model: String,
pub prompt: RenderedPrompt,
pub request_options: HashMap<String, serde_json::Value>,
pub content: String,
#[cfg_attr(target_arch = "wasm32", serde(skip_serializing))]
pub start_time: web_time::SystemTime,
pub latency: web_time::Duration,
pub metadata: LLMCompleteResponseMetadata,
Expand Down
8 changes: 8 additions & 0 deletions engine/language_client_python/python_src/baml_py/baml_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class FunctionResult:
def is_ok(self) -> bool: ...
def cast_to(self, enum_module: Any, class_module: Any) -> Any: ...

# This is a debug function that returns the internal representation of the response
# This is not to be relied upon and is subject to change
# Usage:
# result = await runtime.call_function(...)
# val = json.loads(result.unstable_internal_repr())
# print(val)
def unstable_internal_repr(self) -> str: ...

class FunctionResultStream:
"""The result of a BAML function stream.
Expand Down
6 changes: 6 additions & 0 deletions engine/language_client_python/src/types/function_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl FunctionResult {
self.inner.parsed_content().is_ok()
}

/// This is a debug function that returns the internal representation of the response
/// This is not to be relied upon and is subject to change
fn unstable_internal_repr(&self) -> String {
serde_json::json!(self.inner.llm_response()).to_string()
}

// Cast the parsed value to a specific type
// the module is the module that the type is defined in
fn cast_to(
Expand Down

0 comments on commit 00082e8

Please sign in to comment.