Skip to content

Commit

Permalink
stash everything
Browse files Browse the repository at this point in the history
  • Loading branch information
sxlijin committed Dec 19, 2024
1 parent 5dd25f7 commit a6bcd67
Show file tree
Hide file tree
Showing 30 changed files with 1,023 additions and 1,580 deletions.
2 changes: 1 addition & 1 deletion .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
node = "20.14"
ruby = "3.1"
pnpm = "9.9"
poetry = "1.8.4"
poetry = "1.8.5"
28 changes: 28 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"baml-runtime",
"baml-schema-wasm",
"bstd",
"btrace",
"cli",
"language_client_codegen",
"language_client_python",
Expand All @@ -21,6 +22,7 @@ default-members = [
# also have something to do with resolver=2 and how duplicate dep builds are handled)
# "baml-schema-wasm",
"bstd",
"btrace",
"cli",
"language_client_codegen",
"language_client_python",
Expand Down
1 change: 1 addition & 0 deletions engine/baml-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal-baml-core = { path = "../baml-lib/baml-core" }
internal-baml-jinja = { path = "../baml-lib/jinja-runtime" }
log.workspace = true
minijinja.workspace = true
nonempty = "0.11.0"
pin-project-lite.workspace = true
reqwest-eventsource = "0.6.0"
scopeguard.workspace = true
Expand Down
145 changes: 0 additions & 145 deletions engine/baml-runtime/src/btrace/mod.rs

This file was deleted.

12 changes: 11 additions & 1 deletion engine/baml-runtime/src/internal/llm_client/orchestrator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod call;
mod stream;

use serde_json::json;
use web_time::Duration; // Add this line

use crate::btrace::WithTraceContext;
use crate::RenderCurlSettings;
use crate::{
internal::prompt_renderer::PromptRenderer, runtime_interface::InternalClientLookup,
Expand Down Expand Up @@ -216,7 +218,15 @@ impl WithStreamable for OrchestratorNode {
})
.map(|a| a.increment_index())
.for_each(drop);
self.provider.stream(ctx, prompt).await
self.provider
.stream(ctx, prompt)
.btrace(
tracing::Level::INFO,
format!("stream_init::{}", self.provider.name()),
json!({}),
|_| serde_json::Value::Null,
)
.await
}
}

Expand Down
42 changes: 34 additions & 8 deletions engine/baml-runtime/src/internal/llm_client/orchestrator/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use async_std::stream::StreamExt;
use baml_types::BamlValue;
use internal_baml_core::ir::repr::IntermediateRepr;
use jsonish::BamlValueWithFlags;
use serde_json::json;
use web_time::Duration;

use crate::{
btrace::{self, WithTraceContext},
internal::{
llm_client::{
parsed_value_to_response,
Expand Down Expand Up @@ -83,6 +85,16 @@ where
stream_part
})
.fold(None, |_, current| Some(current))
.btrace(
tracing::Level::INFO,
format!("stream_run::{}", node.provider.name()),
json!({
"prompt": prompt,
"params": params,
"node": node.scope.name(),
}),
|_| serde_json::Value::Null,
)
.await
.unwrap_or_else(|| {
LLMResponse::LLMFailure(LLMErrorResponse {
Expand All @@ -105,16 +117,18 @@ where
.finish_reason_filter()
.is_allowed(s.metadata.finish_reason.as_ref())
{
Some(Err(anyhow::anyhow!(crate::errors::ExposedError::FinishReasonError {
prompt: s.prompt.to_string(),
raw_output: s.content.clone(),
message: "Finish reason not allowed".to_string(),
finish_reason: s.metadata.finish_reason.clone(),
})))
Some(Err(anyhow::anyhow!(
crate::errors::ExposedError::FinishReasonError {
prompt: s.prompt.to_string(),
raw_output: s.content.clone(),
message: "Finish reason not allowed".to_string(),
finish_reason: s.metadata.finish_reason.clone(),
}
)))
} else {
Some(parse_fn(&s.content))
}
},
}
_ => None,
};
let (parsed_response, response_value) = match parsed_response {
Expand All @@ -123,6 +137,7 @@ where
None => (None, None),
};
// parsed_response.map(|r| r.and_then(|v| parsed_value_to_response(v)));
let node_name = node.scope.name();
let sleep_duration = node.error_sleep_duration().cloned();
results.push((node.scope, final_response, parsed_response, response_value));

Expand All @@ -132,7 +147,18 @@ where
.map_or(false, |(_, r, _, _)| matches!(r, LLMResponse::Success(_)))
{
break;
} else if let Some(duration) = sleep_duration {
}
btrace::log(
tracing_core::Level::INFO,
format!("baml_src::{}", node.provider.name()),
"Failed to start stream".to_string(),
json!({
"sleep_duration": sleep_duration,
"baml.llm_client": node_name,
}),
);

if let Some(duration) = sleep_duration {
total_sleep_duration += duration;
async_std::task::sleep(duration).await;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::internal::llm_client::{
traits::{ToProviderMessage, ToProviderMessageExt, WithClientProperties},
ResolveMediaUrls,
use crate::{
btrace::{self, WithTraceContext},
internal::llm_client::{
traits::{ToProviderMessage, ToProviderMessageExt, WithClientProperties},
ResolveMediaUrls,
},
};
use std::collections::HashMap;

Expand Down Expand Up @@ -124,10 +127,8 @@ impl SseResponseTrait for AnthropicClient {

Ok(Box::pin(
resp.bytes_stream()
.inspect(|event| log::trace!("anthropic event bytes: {:#?}", event))
.eventsource()
.map(|event| -> Result<MessageChunk> { Ok(serde_json::from_str(&event?.data)?) })
.inspect(|event| log::trace!("anthropic eventsource: {:#?}", event))
.scan(
Ok(LLMCompleteResponse {
client: client_name.clone(),
Expand Down Expand Up @@ -189,6 +190,14 @@ impl SseResponseTrait for AnthropicClient {
Some(body.usage.input_tokens + body.usage.output_tokens);
}
MessageChunk::ContentBlockDelta(event) => {
btrace::log(
tracing::Level::INFO,
"anthropic_client".to_string(),
"event".to_string(),
json!({
"delta.content": event.delta.text,
}),
);
inner.content += &event.delta.text;
}
MessageChunk::ContentBlockStart(_) => (),
Expand Down Expand Up @@ -376,6 +385,21 @@ impl WithChat for AnthropicClient {
>(
self, either::Either::Right(prompt), false
)
.btrace(
tracing_core::Level::INFO,
"anthropic_chat",
json!({
"prompt": prompt,
}),
|v| match v {
Ok((response, ..)) => json!({
"llm.response": response,
}),
Err(e) => json!({
"exception": e,
}),
},
)
.await
{
Ok(v) => v,
Expand Down
Loading

0 comments on commit a6bcd67

Please sign in to comment.