Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions lib/llm/src/protocols/openai/chat_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::collections::HashMap;

use dynamo_runtime::protocols::annotated::AnnotationsProvider;
use dynamo_runtime::protocols::annotated::{Annotated, AnnotationsProvider};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use validator::Validate;
Expand All @@ -30,8 +30,9 @@ pub use delta::DeltaGenerator;

use dynamo_parsers::tool_calling::{ToolCallResponse, ToolCallResponseChunk};
use dynamo_protocols::types::{
ChatCompletionMessageToolCall, ChatCompletionMessageToolCallChunk, FunctionCall,
FunctionCallStream, FunctionType,
ChatChoiceStream, ChatCompletionMessageContent, ChatCompletionMessageToolCall,
ChatCompletionMessageToolCallChunk, ChatCompletionStreamResponseDelta, FinishReason,
FunctionCall, FunctionCallStream, FunctionType,
};

/// Map a parser-native [`ToolCallResponse`] onto the protocol/wire
Expand Down Expand Up @@ -239,6 +240,45 @@ pub struct NvCreateChatCompletionStreamResponse {
pub llm_metrics: Option<crate::protocols::common::metrics::LLMMetricAnnotation>,
}

/// Build one synthetic stream choice from an existing response template.
///
/// Both streaming tool-call paths use this constructor when an engine omits a
/// terminal choice. Accounting data belongs only on the usage chunk and must
/// not be copied onto the synthetic choice.
pub(super) fn stream_choice_chunk_from_template(
template: &NvCreateChatCompletionStreamResponse,
index: u32,
content: Option<ChatCompletionMessageContent>,
tool_calls: Option<Vec<ChatCompletionMessageToolCallChunk>>,
finish_reason: Option<FinishReason>,
) -> Annotated<NvCreateChatCompletionStreamResponse> {
let mut response = template.clone();
response.inner.usage = None;
response.llm_metrics = None;
#[allow(deprecated)]
let choice = ChatChoiceStream {
index,
delta: ChatCompletionStreamResponseDelta {
role: None,
content,
tool_calls,
function_call: None,
refusal: None,
reasoning_content: None,
},
finish_reason,
logprobs: None,
};
response.inner.choices = vec![choice];
Annotated {
data: Some(response),
id: None,
event: None,
comment: None,
error: None,
}
}

/// Implements `NvExtProvider` for `NvCreateChatCompletionRequest`,
/// providing access to NVIDIA-specific extensions.
impl NvExtProvider for NvCreateChatCompletionRequest {
Expand Down
Loading
Loading