Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-palakurthi committed Jun 20, 2024
1 parent 6f7de88 commit b150672
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 97 deletions.
60 changes: 12 additions & 48 deletions engine/baml-runtime/src/internal/llm_client/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use self::{
};
use super::{retry_policy::CallablePolicy, LLMResponse, ModelFeatures};
use crate::{internal::prompt_renderer::PromptRenderer, RuntimeContext};
use baml_types::{BamlMedia, BamlMediaType, BamlValue, MediaBase64, MediaUrl};
use baml_types::{BamlMedia, BamlMediaType, BamlValue, MediaBase64};
use base64::encode;
use futures::stream::{StreamExt, TryStreamExt};
use infer;
Expand Down Expand Up @@ -49,19 +49,13 @@ where
{
#[allow(async_fn_in_trait)]
async fn single_call(&self, ctx: &RuntimeContext, prompt: &RenderedPrompt) -> LLMResponse {
log::info!("Starting stream");
if self.model_features().resolve_media_urls {
log::info!("Resolving media URLs");

if let RenderedPrompt::Chat(ref chat) = prompt {
log::info!("Processing chat prompt");
let messages_result = futures::stream::iter(chat.iter().map(|p| {
log::info!("Processing chat message");
let new_parts = p
.parts
.iter()
.map(|part| async move {
log::info!("Processing chat message part");
match part {
ChatMessagePart::Image(BamlMedia::Url(_, media_url))
| ChatMessagePart::Audio(BamlMedia::Url(_, media_url)) => {
Expand Down Expand Up @@ -120,23 +114,18 @@ where
))
})
}
_ => {
log::info!("Processing text part");
Ok(part.clone())
}
_ => Ok(part.clone()),
}
})
.collect::<Vec<_>>();
async move {
log::info!("Collecting new parts");
let new_parts = futures::stream::iter(new_parts)
.then(|f| f)
.collect::<Vec<_>>()
.await;

let new_parts = new_parts.into_iter().collect::<Result<Vec<_>, _>>()?;

log::info!("Creating rendered chat message");
Ok::<_, anyhow::Error>(RenderedChatMessage {
role: p.role.clone(),
parts: new_parts,
Expand All @@ -152,23 +141,16 @@ where
let messages = match messages_result {
Ok(messages) => messages,
Err(e) => {
log::error!("Error occurred: {}", e);
return LLMResponse::OtherFailure(format!("Error occurred: {}", e));
}
};
return self.chat(ctx, &messages).await;
}
}
log::info!("Processing prompt");

match prompt {
RenderedPrompt::Chat(p) => {
log::info!("Streaming chat");
self.chat(ctx, p).await
}
RenderedPrompt::Completion(p) => {
log::info!("Streaming completion");
self.completion(ctx, p).await
}
RenderedPrompt::Chat(p) => self.chat(ctx, p).await,
RenderedPrompt::Completion(p) => self.completion(ctx, p).await,
}
}
}
Expand All @@ -187,7 +169,6 @@ where
let features = self.model_features();

let prompt = renderer.render_prompt(ir, ctx, params, self.context())?;
log::debug!("WithPrompt.render_prompt => {:#?}", prompt);

let mut prompt = match (features.completion, features.chat) {
(true, false) => {
Expand Down Expand Up @@ -256,22 +237,16 @@ where
{
#[allow(async_fn_in_trait)]
async fn stream(&self, ctx: &RuntimeContext, prompt: &RenderedPrompt) -> StreamResponse {
log::info!("Starting stream");
if self.model_features().resolve_media_urls {
log::info!("Resolving media URLs");

if let RenderedPrompt::Chat(ref chat) = prompt {
log::info!("Processing chat prompt");
let messages = futures::stream::iter(chat.iter().map(|p| {
log::info!("Processing chat message");
let new_parts = p
.parts
.iter()
.map(|part| async move {
log::info!("Processing chat message part");
match part {
ChatMessagePart::Image(BamlMedia::Url(media_type, media_url))
| ChatMessagePart::Audio(BamlMedia::Url(media_type, media_url)) => {
ChatMessagePart::Image(BamlMedia::Url(_, media_url))
| ChatMessagePart::Audio(BamlMedia::Url(_, media_url)) => {
let mut base64 = "".to_string();
let mut mime_type = "".to_string();
if media_url.url.starts_with("data:") {
Expand Down Expand Up @@ -333,23 +308,18 @@ where
))
})
}
_ => {
log::info!("Processing text part");
Ok(part.clone())
}
_ => Ok(part.clone()),
}
})
.collect::<Vec<_>>();
async move {
log::info!("Collecting new parts");
let new_parts = futures::stream::iter(new_parts)
.then(|f| f)
.collect::<Vec<_>>()
.await;

let new_parts = new_parts.into_iter().collect::<Result<Vec<_>, _>>()?;

log::info!("Creating rendered chat message");
Ok(RenderedChatMessage {
role: p.role.clone(),
parts: new_parts,
Expand All @@ -361,20 +331,14 @@ where
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
log::info!("Streaming chat");

return self.stream_chat(ctx, &messages).await;
}
}
log::info!("Processing prompt");

match prompt {
RenderedPrompt::Chat(p) => {
log::info!("Streaming chat");
self.stream_chat(ctx, p).await
}
RenderedPrompt::Completion(p) => {
log::info!("Streaming completion");
self.stream_completion(ctx, p).await
}
RenderedPrompt::Chat(p) => self.stream_chat(ctx, p).await,
RenderedPrompt::Completion(p) => self.stream_completion(ctx, p).await,
}
}
}
2 changes: 1 addition & 1 deletion engine/baml-runtime/src/internal/prompt_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Result;
use baml_types::{BamlValue, FieldType};
use internal_baml_core::{
error_unsupported,
ir::{repr::IntermediateRepr, Expression, FunctionWalker, IRHelper},
ir::{repr::IntermediateRepr, FunctionWalker, IRHelper},
};
use internal_baml_jinja::{
types::OutputFormatContent, RenderContext, RenderContext_Client, RenderedPrompt,
Expand Down
2 changes: 1 addition & 1 deletion engine/baml-schema-wasm/src/runtime_wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn get_dummy_value(
"{ url \"https://imgs.xkcd.com/comics/standards.png\"}".to_string()
}
baml_runtime::TypeValue::Audio => {
"{ url \"https://www.soundjay.com/button/beep-07.wav\"}".to_string()
"{ url \"https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg\"}".to_string()
}
};

Expand Down
1 change: 0 additions & 1 deletion integ-tests/baml_src/clients.baml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ client<llm> Gemini {
options{
model "gemini-1.5-pro-001"
api_key env.GOOGLE_API_KEY
// default_role "user"
}
}

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion integ-tests/python/baml_client/inlinedbaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

file_map = {

"clients.baml": "retry_policy Bar {\n max_retries 3\n strategy {\n type exponential_backoff\n }\n}\n\nretry_policy Foo {\n max_retries 3\n strategy {\n type constant_delay\n delay_ms 100\n }\n}\n\nclient<llm> GPT4 {\n provider baml-openai-chat\n options {\n model gpt-4\n api_key env.OPENAI_API_KEY\n }\n} \n\n\nclient<llm> GPT4o {\n provider baml-openai-chat\n options {\n model gpt-4o\n api_key env.OPENAI_API_KEY\n }\n} \n\n\nclient<llm> GPT4Turbo {\n retry_policy Bar\n provider baml-openai-chat\n options {\n model gpt-4-turbo\n api_key env.OPENAI_API_KEY\n }\n} \n\nclient<llm> GPT35 {\n provider baml-openai-chat\n options {\n model \"gpt-3.5-turbo\"\n api_key env.OPENAI_API_KEY\n }\n}\n\nclient<llm> Ollama {\n provider ollama\n options {\n model llama2\n }\n}\n\nclient<llm> GPT35Azure {\n provider azure-openai\n options {\n resource_name \"west-us-azure-baml\"\n deployment_id \"gpt-35-turbo-default\"\n // base_url \"https://west-us-azure-baml.openai.azure.com/openai/deployments/gpt-35-turbo-default\"\n api_version \"2024-02-01\"\n api_key env.AZURE_OPENAI_API_KEY\n }\n}\n\nclient<llm> Gemini {\n provider google-ai\n options{\n model \"gemini-1.5-pro-001\"\n api_key env.GOOGLE_API_KEY\n // default_role \"user\"\n }\n}\n\n\nclient<llm> Claude {\n provider anthropic\n options {\n model claude-3-haiku-20240307\n api_key env.ANTHROPIC_API_KEY\n max_tokens 1000\n }\n}\n\nclient<llm> Resilient_SimpleSyntax {\n retry_policy Foo\n provider baml-fallback\n options {\n strategy [\n GPT4Turbo\n GPT35\n Lottery_SimpleSyntax\n ]\n }\n} \n \nclient<llm> Lottery_SimpleSyntax {\n provider baml-round-robin\n options {\n start 0\n strategy [\n GPT35\n Claude\n ]\n }\n}\n",
"clients.baml": "retry_policy Bar {\n max_retries 3\n strategy {\n type exponential_backoff\n }\n}\n\nretry_policy Foo {\n max_retries 3\n strategy {\n type constant_delay\n delay_ms 100\n }\n}\n\nclient<llm> GPT4 {\n provider baml-openai-chat\n options {\n model gpt-4\n api_key env.OPENAI_API_KEY\n }\n} \n\n\nclient<llm> GPT4o {\n provider baml-openai-chat\n options {\n model gpt-4o\n api_key env.OPENAI_API_KEY\n }\n} \n\n\nclient<llm> GPT4Turbo {\n retry_policy Bar\n provider baml-openai-chat\n options {\n model gpt-4-turbo\n api_key env.OPENAI_API_KEY\n }\n} \n\nclient<llm> GPT35 {\n provider baml-openai-chat\n options {\n model \"gpt-3.5-turbo\"\n api_key env.OPENAI_API_KEY\n }\n}\n\nclient<llm> Ollama {\n provider ollama\n options {\n model llama2\n }\n}\n\nclient<llm> GPT35Azure {\n provider azure-openai\n options {\n resource_name \"west-us-azure-baml\"\n deployment_id \"gpt-35-turbo-default\"\n // base_url \"https://west-us-azure-baml.openai.azure.com/openai/deployments/gpt-35-turbo-default\"\n api_version \"2024-02-01\"\n api_key env.AZURE_OPENAI_API_KEY\n }\n}\n\nclient<llm> Gemini {\n provider google-ai\n options{\n model \"gemini-1.5-pro-001\"\n api_key env.GOOGLE_API_KEY\n }\n}\n\n\nclient<llm> Claude {\n provider anthropic\n options {\n model claude-3-haiku-20240307\n api_key env.ANTHROPIC_API_KEY\n max_tokens 1000\n }\n}\n\nclient<llm> Resilient_SimpleSyntax {\n retry_policy Foo\n provider baml-fallback\n options {\n strategy [\n GPT4Turbo\n GPT35\n Lottery_SimpleSyntax\n ]\n }\n} \n \nclient<llm> Lottery_SimpleSyntax {\n provider baml-round-robin\n options {\n start 0\n strategy [\n GPT35\n Claude\n ]\n }\n}\n",
"fiddle-examples/chain-of-thought.baml": "class Email {\n subject string\n body string\n from_address string\n}\n\nenum OrderStatus {\n ORDERED\n SHIPPED\n DELIVERED\n CANCELLED\n}\n\nclass OrderInfo {\n order_status OrderStatus\n tracking_number string?\n estimated_arrival_date string?\n}\n\nfunction GetOrderInfo(email: Email) -> OrderInfo {\n client GPT4\n prompt #\"\n Given the email below:\n\n ```\n from: {{email.from_address}}\n Email Subject: {{email.subject}}\n Email Body: {{email.body}}\n ```\n\n Extract this info from the email in JSON format:\n {{ ctx.output_format }}\n\n Before you output the JSON, please explain your\n reasoning step-by-step. Here is an example on how to do this:\n 'If we think step by step we can see that ...\n therefore the output JSON is:\n {\n ... the json schema ...\n }'\n \"#\n}",
"fiddle-examples/chat-roles.baml": "// This will be available as an enum in your Python and Typescript code.\nenum Category2 {\n Refund\n CancelOrder\n TechnicalSupport\n AccountIssue\n Question\n}\n\nfunction ClassifyMessage2(input: string) -> Category {\n client GPT4\n\n prompt #\"\n {{ _.role(\"system\") }}\n // You can use _.role(\"system\") to indicate that this text should be a system message\n\n Classify the following INPUT into ONE\n of the following categories:\n\n {{ ctx.output_format }}\n\n {{ _.role(\"user\") }}\n // And _.role(\"user\") to indicate that this text should be a user message\n\n INPUT: {{ input }}\n\n Response:\n \"#\n}",
"fiddle-examples/classify-message.baml": "// This will be available as an enum in your Python and Typescript code.\nenum Category {\n Refund\n CancelOrder\n TechnicalSupport\n AccountIssue\n Question\n}\n\nfunction ClassifyMessage(input: string) -> Category {\n client GPT4\n\n prompt #\"\n Classify the following INPUT into ONE\n of the following categories:\n\n INPUT: {{ input }}\n\n {{ ctx.output_format }}\n\n Response:\n \"#\n}",
Expand Down
Loading

0 comments on commit b150672

Please sign in to comment.