2024-12-09 10:46:46 -08:00
|
|
|
use crate::metrics::Metrics;
|
2024-12-06 17:25:42 -08:00
|
|
|
use common::api::open_ai::{
|
2024-10-28 20:05:06 -04:00
|
|
|
ChatCompletionStreamResponseServerEvents, ChatCompletionsRequest, ChatCompletionsResponse,
|
2024-11-15 10:44:01 -08:00
|
|
|
Message, StreamOptions,
|
2024-10-17 10:16:40 -07:00
|
|
|
};
|
|
|
|
|
use common::configuration::LlmProvider;
|
|
|
|
|
use common::consts::{
|
2024-10-28 20:05:06 -04:00
|
|
|
ARCH_PROVIDER_HINT_HEADER, ARCH_ROUTING_HEADER, CHAT_COMPLETIONS_PATH,
|
2024-11-15 10:44:01 -08:00
|
|
|
RATELIMIT_SELECTOR_HEADER_KEY, REQUEST_ID_HEADER, TRACE_PARENT_HEADER,
|
2024-10-17 10:16:40 -07:00
|
|
|
};
|
2024-10-18 12:53:44 -07:00
|
|
|
use common::errors::ServerError;
|
2024-10-17 10:16:40 -07:00
|
|
|
use common::llm_providers::LlmProviders;
|
|
|
|
|
use common::ratelimit::Header;
|
2024-12-09 10:46:46 -08:00
|
|
|
use common::stats::{IncrementingMetric, RecordingMetric};
|
2024-11-18 17:55:39 -08:00
|
|
|
use common::tracing::{Event, Span, TraceData, Traceparent};
|
2024-10-17 10:16:40 -07:00
|
|
|
use common::{ratelimit, routing, tokenizer};
|
|
|
|
|
use http::StatusCode;
|
2024-10-28 20:05:06 -04:00
|
|
|
use log::{debug, trace, warn};
|
2024-12-09 10:46:46 -08:00
|
|
|
use proxy_wasm::hostcalls::get_current_time;
|
2024-10-17 10:16:40 -07:00
|
|
|
use proxy_wasm::traits::*;
|
|
|
|
|
use proxy_wasm::types::*;
|
2024-11-18 17:55:39 -08:00
|
|
|
use std::collections::VecDeque;
|
2024-10-17 10:16:40 -07:00
|
|
|
use std::num::NonZero;
|
|
|
|
|
use std::rc::Rc;
|
2024-11-18 17:55:39 -08:00
|
|
|
use std::sync::{Arc, Mutex};
|
2024-11-15 10:44:01 -08:00
|
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
2024-10-17 10:16:40 -07:00
|
|
|
|
2024-10-18 12:53:44 -07:00
|
|
|
pub struct StreamContext {
|
2024-10-17 10:16:40 -07:00
|
|
|
context_id: u32,
|
2024-12-09 10:46:46 -08:00
|
|
|
metrics: Rc<Metrics>,
|
2024-10-17 10:16:40 -07:00
|
|
|
ratelimit_selector: Option<Header>,
|
|
|
|
|
streaming_response: bool,
|
|
|
|
|
response_tokens: usize,
|
|
|
|
|
is_chat_completions_request: bool,
|
|
|
|
|
llm_providers: Rc<LlmProviders>,
|
|
|
|
|
llm_provider: Option<Rc<LlmProvider>>,
|
|
|
|
|
request_id: Option<String>,
|
2024-11-18 17:55:39 -08:00
|
|
|
start_time: SystemTime,
|
2024-11-15 10:44:01 -08:00
|
|
|
ttft_duration: Option<Duration>,
|
2024-11-18 17:55:39 -08:00
|
|
|
ttft_time: Option<u128>,
|
|
|
|
|
traceparent: Option<String>,
|
|
|
|
|
request_body_sent_time: Option<u128>,
|
2024-11-15 10:44:01 -08:00
|
|
|
user_message: Option<Message>,
|
2024-11-18 17:55:39 -08:00
|
|
|
traces_queue: Arc<Mutex<VecDeque<TraceData>>>,
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 12:53:44 -07:00
|
|
|
impl StreamContext {
|
2024-11-18 17:55:39 -08:00
|
|
|
pub fn new(
|
|
|
|
|
context_id: u32,
|
2024-12-09 10:46:46 -08:00
|
|
|
metrics: Rc<Metrics>,
|
2024-11-18 17:55:39 -08:00
|
|
|
llm_providers: Rc<LlmProviders>,
|
|
|
|
|
traces_queue: Arc<Mutex<VecDeque<TraceData>>>,
|
|
|
|
|
) -> Self {
|
2024-10-18 12:53:44 -07:00
|
|
|
StreamContext {
|
2024-10-17 10:16:40 -07:00
|
|
|
context_id,
|
|
|
|
|
metrics,
|
|
|
|
|
ratelimit_selector: None,
|
|
|
|
|
streaming_response: false,
|
|
|
|
|
response_tokens: 0,
|
|
|
|
|
is_chat_completions_request: false,
|
|
|
|
|
llm_providers,
|
|
|
|
|
llm_provider: None,
|
|
|
|
|
request_id: None,
|
2024-11-18 17:55:39 -08:00
|
|
|
start_time: SystemTime::now(),
|
2024-11-12 15:03:26 -08:00
|
|
|
ttft_duration: None,
|
2024-11-15 10:44:01 -08:00
|
|
|
traceparent: None,
|
|
|
|
|
ttft_time: None,
|
|
|
|
|
user_message: None,
|
2024-11-18 17:55:39 -08:00
|
|
|
traces_queue,
|
2024-11-17 17:01:19 -08:00
|
|
|
request_body_sent_time: None,
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn llm_provider(&self) -> &LlmProvider {
|
|
|
|
|
self.llm_provider
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("the provider should be set when asked for it")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn select_llm_provider(&mut self) {
|
|
|
|
|
let provider_hint = self
|
|
|
|
|
.get_http_request_header(ARCH_PROVIDER_HINT_HEADER)
|
2025-01-17 18:25:55 -08:00
|
|
|
.map(|llm_name| llm_name.into());
|
2024-10-17 10:16:40 -07:00
|
|
|
|
|
|
|
|
self.llm_provider = Some(routing::get_llm_provider(
|
|
|
|
|
&self.llm_providers,
|
|
|
|
|
provider_hint,
|
|
|
|
|
));
|
2025-01-31 10:37:53 -08:00
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
|
"request received: llm provider hint: {:?}, selected llm: {}",
|
|
|
|
|
self.get_http_request_header(ARCH_PROVIDER_HINT_HEADER),
|
|
|
|
|
self.llm_provider.as_ref().unwrap().name
|
|
|
|
|
);
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn modify_auth_headers(&mut self) -> Result<(), ServerError> {
|
|
|
|
|
let llm_provider_api_key_value =
|
|
|
|
|
self.llm_provider()
|
|
|
|
|
.access_key
|
|
|
|
|
.as_ref()
|
|
|
|
|
.ok_or(ServerError::BadRequest {
|
|
|
|
|
why: format!(
|
|
|
|
|
"No access key configured for selected LLM Provider \"{}\"",
|
|
|
|
|
self.llm_provider()
|
|
|
|
|
),
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let authorization_header_value = format!("Bearer {}", llm_provider_api_key_value);
|
|
|
|
|
|
|
|
|
|
self.set_http_request_header("Authorization", Some(&authorization_header_value));
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn delete_content_length_header(&mut self) {
|
|
|
|
|
// Remove the Content-Length header because further body manipulations in the gateway logic will invalidate it.
|
|
|
|
|
// Server's generally throw away requests whose body length do not match the Content-Length header.
|
|
|
|
|
// However, a missing Content-Length header is not grounds for bad requests given that intermediary hops could
|
|
|
|
|
// manipulate the body in benign ways e.g., compression.
|
|
|
|
|
self.set_http_request_header("content-length", None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn save_ratelimit_header(&mut self) {
|
|
|
|
|
self.ratelimit_selector = self
|
|
|
|
|
.get_http_request_header(RATELIMIT_SELECTOR_HEADER_KEY)
|
|
|
|
|
.and_then(|key| {
|
|
|
|
|
self.get_http_request_header(&key)
|
|
|
|
|
.map(|value| Header { key, value })
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn send_server_error(&self, error: ServerError, override_status_code: Option<StatusCode>) {
|
|
|
|
|
debug!("server error occurred: {}", error);
|
|
|
|
|
self.send_http_response(
|
|
|
|
|
override_status_code
|
|
|
|
|
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
|
.as_u16()
|
|
|
|
|
.into(),
|
|
|
|
|
vec![],
|
|
|
|
|
Some(format!("{error}").as_bytes()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn enforce_ratelimits(
|
|
|
|
|
&mut self,
|
|
|
|
|
model: &str,
|
|
|
|
|
json_string: &str,
|
|
|
|
|
) -> Result<(), ratelimit::Error> {
|
2024-11-12 15:03:26 -08:00
|
|
|
// Tokenize and record token count.
|
|
|
|
|
let token_count = tokenizer::token_count(model, json_string).unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
// Record the token count to metrics.
|
|
|
|
|
self.metrics
|
|
|
|
|
.input_sequence_length
|
|
|
|
|
.record(token_count as u64);
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!("Recorded input token count: {}", token_count);
|
2024-11-12 15:03:26 -08:00
|
|
|
|
|
|
|
|
// Check if rate limiting needs to be applied.
|
2024-10-17 10:16:40 -07:00
|
|
|
if let Some(selector) = self.ratelimit_selector.take() {
|
2024-11-12 15:03:26 -08:00
|
|
|
log::debug!("Applying ratelimit for model: {}", model);
|
|
|
|
|
ratelimit::ratelimits(None).read().unwrap().check_limit(
|
|
|
|
|
model.to_owned(),
|
|
|
|
|
selector,
|
|
|
|
|
NonZero::new(token_count as u32).unwrap(),
|
|
|
|
|
)?;
|
|
|
|
|
} else {
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!("No rate limit applied for model: {}", model);
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
2024-11-12 15:03:26 -08:00
|
|
|
|
2024-10-17 10:16:40 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HttpContext is the trait that allows the Rust code to interact with HTTP objects.
|
2024-10-18 12:53:44 -07:00
|
|
|
impl HttpContext for StreamContext {
|
2024-10-17 10:16:40 -07:00
|
|
|
// Envoy's HTTP model is event driven. The WASM ABI has given implementors events to hook onto
|
|
|
|
|
// the lifecycle of the http request and response.
|
|
|
|
|
fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
|
|
|
|
|
self.select_llm_provider();
|
2025-01-17 18:25:55 -08:00
|
|
|
|
|
|
|
|
// if endpoint is not set then use provider name as routing header so envoy can resolve the cluster name
|
|
|
|
|
if self.llm_provider().endpoint.is_none() {
|
|
|
|
|
self.add_http_request_header(
|
|
|
|
|
ARCH_ROUTING_HEADER,
|
|
|
|
|
&self.llm_provider().provider_interface.to_string(),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
self.add_http_request_header(ARCH_ROUTING_HEADER, &self.llm_provider().name);
|
|
|
|
|
}
|
2024-10-17 10:16:40 -07:00
|
|
|
|
|
|
|
|
if let Err(error) = self.modify_auth_headers() {
|
2025-01-17 18:25:55 -08:00
|
|
|
// ensure that the provider has an endpoint if the access key is missing else return a bad request
|
|
|
|
|
if self.llm_provider.as_ref().unwrap().endpoint.is_none() {
|
|
|
|
|
self.send_server_error(error, Some(StatusCode::BAD_REQUEST));
|
|
|
|
|
}
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
self.delete_content_length_header();
|
|
|
|
|
self.save_ratelimit_header();
|
|
|
|
|
|
|
|
|
|
self.is_chat_completions_request =
|
|
|
|
|
self.get_http_request_header(":path").unwrap_or_default() == CHAT_COMPLETIONS_PATH;
|
|
|
|
|
|
|
|
|
|
self.request_id = self.get_http_request_header(REQUEST_ID_HEADER);
|
2024-11-15 10:44:01 -08:00
|
|
|
self.traceparent = self.get_http_request_header(TRACE_PARENT_HEADER);
|
2024-11-12 15:03:26 -08:00
|
|
|
|
2024-10-17 10:16:40 -07:00
|
|
|
Action::Continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_http_request_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
|
|
|
|
|
// Let the client send the gateway all the data before sending to the LLM_provider.
|
|
|
|
|
// TODO: consider a streaming API.
|
2024-11-17 17:01:19 -08:00
|
|
|
|
|
|
|
|
if self.request_body_sent_time.is_none() {
|
2024-11-18 17:55:39 -08:00
|
|
|
self.request_body_sent_time = Some(current_time_ns());
|
2024-11-17 17:01:19 -08:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 10:16:40 -07:00
|
|
|
if !end_of_stream {
|
|
|
|
|
return Action::Pause;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if body_size == 0 {
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deserialize body into spec.
|
|
|
|
|
// Currently OpenAI API.
|
|
|
|
|
let mut deserialized_body: ChatCompletionsRequest =
|
|
|
|
|
match self.get_http_request_body(0, body_size) {
|
|
|
|
|
Some(body_bytes) => match serde_json::from_slice(&body_bytes) {
|
|
|
|
|
Ok(deserialized) => deserialized,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
self.send_server_error(
|
|
|
|
|
ServerError::Deserialization(e),
|
|
|
|
|
Some(StatusCode::BAD_REQUEST),
|
|
|
|
|
);
|
|
|
|
|
return Action::Pause;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
self.send_server_error(
|
|
|
|
|
ServerError::LogicError(format!(
|
|
|
|
|
"Failed to obtain body bytes even though body_size is {}",
|
|
|
|
|
body_size
|
|
|
|
|
)),
|
|
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
return Action::Pause;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// remove metadata from the request body
|
|
|
|
|
deserialized_body.metadata = None;
|
|
|
|
|
// delete model key from message array
|
|
|
|
|
for message in deserialized_body.messages.iter_mut() {
|
|
|
|
|
message.model = None;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 10:44:01 -08:00
|
|
|
self.user_message = deserialized_body
|
|
|
|
|
.messages
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|m| m.role == "user")
|
|
|
|
|
.last()
|
|
|
|
|
.cloned();
|
|
|
|
|
|
2024-10-17 10:16:40 -07:00
|
|
|
// override model name from the llm provider
|
|
|
|
|
deserialized_body
|
|
|
|
|
.model
|
|
|
|
|
.clone_from(&self.llm_provider.as_ref().unwrap().model);
|
|
|
|
|
let chat_completion_request_str = serde_json::to_string(&deserialized_body).unwrap();
|
|
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
trace!(
|
|
|
|
|
"arch => {:?}, body: {}",
|
|
|
|
|
deserialized_body.model,
|
|
|
|
|
chat_completion_request_str
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if deserialized_body.stream {
|
|
|
|
|
self.streaming_response = true;
|
|
|
|
|
}
|
|
|
|
|
if deserialized_body.stream && deserialized_body.stream_options.is_none() {
|
|
|
|
|
deserialized_body.stream_options = Some(StreamOptions {
|
|
|
|
|
include_usage: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 15:03:26 -08:00
|
|
|
// only use the tokens from the messages, excluding the metadata and json tags
|
|
|
|
|
let input_tokens_str = deserialized_body
|
|
|
|
|
.messages
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(String::new(), |acc, m| {
|
|
|
|
|
acc + " " + m.content.as_ref().unwrap_or(&String::new())
|
|
|
|
|
});
|
2024-10-17 10:16:40 -07:00
|
|
|
// enforce ratelimits on ingress
|
2024-11-12 15:03:26 -08:00
|
|
|
if let Err(e) = self.enforce_ratelimits(&deserialized_body.model, input_tokens_str.as_str())
|
2024-10-17 10:16:40 -07:00
|
|
|
{
|
|
|
|
|
self.send_server_error(
|
|
|
|
|
ServerError::ExceededRatelimit(e),
|
|
|
|
|
Some(StatusCode::TOO_MANY_REQUESTS),
|
|
|
|
|
);
|
|
|
|
|
self.metrics.ratelimited_rq.increment(1);
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.set_http_request_body(0, body_size, chat_completion_request_str.as_bytes());
|
|
|
|
|
|
|
|
|
|
Action::Continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 17:55:39 -08:00
|
|
|
fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!(
|
2024-11-18 17:55:39 -08:00
|
|
|
"on_http_response_headers [S={}] end_stream={}",
|
2025-01-31 10:37:53 -08:00
|
|
|
self.context_id,
|
|
|
|
|
_end_of_stream
|
2024-11-18 17:55:39 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.set_property(
|
|
|
|
|
vec!["metadata", "filter_metadata", "llm_filter", "user_prompt"],
|
|
|
|
|
Some("hello world from filter".as_bytes()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Action::Continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 10:16:40 -07:00
|
|
|
fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!(
|
2024-10-28 20:05:06 -04:00
|
|
|
"on_http_response_body [S={}] bytes={} end_stream={}",
|
2025-01-31 10:37:53 -08:00
|
|
|
self.context_id,
|
|
|
|
|
body_size,
|
|
|
|
|
end_of_stream
|
2024-10-17 10:16:40 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if !self.is_chat_completions_request {
|
2024-10-28 20:05:06 -04:00
|
|
|
debug!("non-chatcompletion request");
|
2024-10-17 10:16:40 -07:00
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 15:03:26 -08:00
|
|
|
let current_time = get_current_time().unwrap();
|
|
|
|
|
if end_of_stream && body_size == 0 {
|
|
|
|
|
// All streaming responses end with bytes=0 and end_stream=true
|
|
|
|
|
// Record the latency for the request
|
2024-11-18 17:55:39 -08:00
|
|
|
match current_time.duration_since(self.start_time) {
|
|
|
|
|
Ok(duration) => {
|
|
|
|
|
// Convert the duration to milliseconds
|
|
|
|
|
let duration_ms = duration.as_millis();
|
2025-01-31 10:37:53 -08:00
|
|
|
debug!("request latency: {}ms", duration_ms);
|
2024-11-18 17:55:39 -08:00
|
|
|
// Record the latency to the latency histogram
|
|
|
|
|
self.metrics.request_latency.record(duration_ms as u64);
|
|
|
|
|
|
2025-01-17 18:25:55 -08:00
|
|
|
if self.response_tokens > 0 {
|
|
|
|
|
// Compute the time per output token
|
|
|
|
|
let tpot = duration_ms as u64 / self.response_tokens as u64;
|
2024-11-18 17:55:39 -08:00
|
|
|
|
2025-01-17 18:25:55 -08:00
|
|
|
// Record the time per output token
|
|
|
|
|
self.metrics.time_per_output_token.record(tpot);
|
2024-11-18 17:55:39 -08:00
|
|
|
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!(
|
|
|
|
|
"time per token: {}ms, tokens per second: {}",
|
|
|
|
|
tpot,
|
|
|
|
|
1000 / tpot
|
|
|
|
|
);
|
2025-01-17 18:25:55 -08:00
|
|
|
// Record the tokens per second
|
|
|
|
|
self.metrics.tokens_per_second.record(1000 / tpot);
|
|
|
|
|
}
|
2024-11-18 17:55:39 -08:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("SystemTime error: {:?}", e);
|
2024-11-12 15:03:26 -08:00
|
|
|
}
|
2024-10-28 20:05:06 -04:00
|
|
|
}
|
2024-11-12 15:03:26 -08:00
|
|
|
// Record the output sequence length
|
|
|
|
|
self.metrics
|
|
|
|
|
.output_sequence_length
|
|
|
|
|
.record(self.response_tokens as u64);
|
|
|
|
|
|
2024-11-15 10:44:01 -08:00
|
|
|
if let Some(traceparent) = self.traceparent.as_ref() {
|
2024-11-18 17:55:39 -08:00
|
|
|
let current_time_ns = current_time_ns();
|
2024-11-15 10:44:01 -08:00
|
|
|
|
2024-11-18 17:55:39 -08:00
|
|
|
match Traceparent::try_from(traceparent.to_string()) {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("traceparent header is invalid: {}", e);
|
2024-11-15 10:44:01 -08:00
|
|
|
}
|
2024-11-18 17:55:39 -08:00
|
|
|
Ok(traceparent) => {
|
|
|
|
|
let mut trace_data = common::tracing::TraceData::new();
|
|
|
|
|
let mut llm_span = Span::new(
|
2025-02-14 19:28:10 -08:00
|
|
|
"egress_traffic".to_string(),
|
2024-11-18 17:55:39 -08:00
|
|
|
Some(traceparent.trace_id),
|
|
|
|
|
Some(traceparent.parent_id),
|
|
|
|
|
self.request_body_sent_time.unwrap(),
|
|
|
|
|
current_time_ns,
|
|
|
|
|
);
|
|
|
|
|
if let Some(user_message) = self.user_message.as_ref() {
|
|
|
|
|
if let Some(prompt) = user_message.content.as_ref() {
|
|
|
|
|
llm_span
|
|
|
|
|
.add_attribute("user_prompt".to_string(), prompt.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
llm_span.add_attribute(
|
|
|
|
|
"model".to_string(),
|
|
|
|
|
self.llm_provider().name.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-17 18:25:55 -08:00
|
|
|
if self.ttft_time.is_some() {
|
|
|
|
|
llm_span.add_event(Event::new(
|
|
|
|
|
"time_to_first_token".to_string(),
|
|
|
|
|
self.ttft_time.unwrap(),
|
|
|
|
|
));
|
|
|
|
|
trace_data.add_span(llm_span);
|
|
|
|
|
}
|
2024-11-18 17:55:39 -08:00
|
|
|
|
|
|
|
|
self.traces_queue.lock().unwrap().push_back(trace_data);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-15 10:44:01 -08:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 15:03:26 -08:00
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let body = if self.streaming_response {
|
2024-10-28 20:05:06 -04:00
|
|
|
let chunk_start = 0;
|
|
|
|
|
let chunk_size = body_size;
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!(
|
2024-10-28 20:05:06 -04:00
|
|
|
"streaming response reading, {}..{}",
|
2025-01-31 10:37:53 -08:00
|
|
|
chunk_start,
|
|
|
|
|
chunk_size
|
2024-10-28 20:05:06 -04:00
|
|
|
);
|
|
|
|
|
let streaming_chunk = match self.get_http_response_body(0, chunk_size) {
|
|
|
|
|
Some(chunk) => chunk,
|
2024-10-17 10:16:40 -07:00
|
|
|
None => {
|
2024-10-28 20:05:06 -04:00
|
|
|
warn!(
|
|
|
|
|
"response body empty, chunk_start: {}, chunk_size: {}",
|
|
|
|
|
chunk_start, chunk_size
|
2024-10-17 10:16:40 -07:00
|
|
|
);
|
2024-10-28 20:05:06 -04:00
|
|
|
return Action::Continue;
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
if streaming_chunk.len() != chunk_size {
|
|
|
|
|
warn!(
|
|
|
|
|
"chunk size mismatch: read: {} != requested: {}",
|
|
|
|
|
streaming_chunk.len(),
|
|
|
|
|
chunk_size
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
streaming_chunk
|
|
|
|
|
} else {
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!("non streaming response bytes read: 0:{}", body_size);
|
2024-10-28 20:05:06 -04:00
|
|
|
match self.get_http_response_body(0, body_size) {
|
|
|
|
|
Some(body) => body,
|
|
|
|
|
None => {
|
|
|
|
|
warn!("non streaming response body empty");
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let body_utf8 = match String::from_utf8(body) {
|
|
|
|
|
Ok(body_utf8) => body_utf8,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!("could not convert to utf8: {}", e);
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if self.streaming_response {
|
|
|
|
|
let chat_completions_chunk_response_events =
|
|
|
|
|
match ChatCompletionStreamResponseServerEvents::try_from(body_utf8.as_str()) {
|
|
|
|
|
Ok(response) => response,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!(
|
|
|
|
|
"invalid streaming response: body str: {}, {:?}",
|
|
|
|
|
body_utf8, e
|
|
|
|
|
);
|
2024-10-17 10:16:40 -07:00
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
if chat_completions_chunk_response_events.events.is_empty() {
|
|
|
|
|
debug!("empty streaming response");
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut model = chat_completions_chunk_response_events
|
|
|
|
|
.events
|
2024-10-17 10:16:40 -07:00
|
|
|
.first()
|
|
|
|
|
.unwrap()
|
2024-10-28 20:05:06 -04:00
|
|
|
.model
|
|
|
|
|
.clone();
|
|
|
|
|
let tokens_str = chat_completions_chunk_response_events.to_string();
|
|
|
|
|
//HACK: add support for tokenizing mistral and other models
|
|
|
|
|
//filed issue https://github.com/katanemo/arch/issues/222
|
2025-01-21 18:01:56 -08:00
|
|
|
if !model.as_ref().unwrap().starts_with("gpt") {
|
|
|
|
|
warn!(
|
|
|
|
|
"tiktoken_rs: unsupported model: {}, using gpt-4 to compute token count",
|
|
|
|
|
model.as_ref().unwrap()
|
|
|
|
|
);
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
2025-01-21 18:01:56 -08:00
|
|
|
model = Some("gpt-4".to_string());
|
|
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
let token_count =
|
|
|
|
|
match tokenizer::token_count(model.as_ref().unwrap().as_str(), tokens_str.as_str())
|
|
|
|
|
{
|
|
|
|
|
Ok(token_count) => token_count,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!("could not get token count: {:?}", e);
|
|
|
|
|
return Action::Continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.response_tokens += token_count;
|
2024-11-12 15:03:26 -08:00
|
|
|
|
|
|
|
|
// Compute TTFT if not already recorded
|
|
|
|
|
if self.ttft_duration.is_none() {
|
2024-11-18 17:55:39 -08:00
|
|
|
// if let Some(start_time) = self.start_time {
|
|
|
|
|
let current_time = get_current_time().unwrap();
|
|
|
|
|
self.ttft_time = Some(current_time_ns());
|
|
|
|
|
match current_time.duration_since(self.start_time) {
|
|
|
|
|
Ok(duration) => {
|
|
|
|
|
let duration_ms = duration.as_millis();
|
2025-01-31 10:37:53 -08:00
|
|
|
debug!("time to first token: {}ms", duration_ms);
|
2024-11-18 17:55:39 -08:00
|
|
|
self.ttft_duration = Some(duration);
|
|
|
|
|
self.metrics.time_to_first_token.record(duration_ms as u64);
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("SystemTime error: {:?}", e);
|
2024-11-12 15:03:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-17 10:16:40 -07:00
|
|
|
} else {
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!("non streaming response");
|
2024-10-17 10:16:40 -07:00
|
|
|
let chat_completions_response: ChatCompletionsResponse =
|
2024-10-28 20:05:06 -04:00
|
|
|
match serde_json::from_str(body_utf8.as_str()) {
|
2024-10-17 10:16:40 -07:00
|
|
|
Ok(de) => de,
|
2025-01-31 10:37:53 -08:00
|
|
|
Err(err) => {
|
|
|
|
|
debug!(
|
|
|
|
|
"non chat-completion compliant response received err: {}, body: {}",
|
|
|
|
|
err, body_utf8
|
|
|
|
|
);
|
2024-10-18 13:14:18 -07:00
|
|
|
return Action::Continue;
|
2024-10-17 10:16:40 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if chat_completions_response.usage.is_some() {
|
|
|
|
|
self.response_tokens += chat_completions_response
|
|
|
|
|
.usage
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.completion_tokens;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 10:37:53 -08:00
|
|
|
trace!(
|
2024-10-17 10:16:40 -07:00
|
|
|
"recv [S={}] total_tokens={} end_stream={}",
|
2025-01-31 10:37:53 -08:00
|
|
|
self.context_id,
|
|
|
|
|
self.response_tokens,
|
|
|
|
|
end_of_stream
|
2024-10-17 10:16:40 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Action::Continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 17:55:39 -08:00
|
|
|
fn current_time_ns() -> u128 {
|
|
|
|
|
SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_nanos()
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 12:53:44 -07:00
|
|
|
impl Context for StreamContext {}
|