protocols.rs 2.02 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
// SPDX-License-Identifier: Apache-2.0

Neelay Shah's avatar
Neelay Shah committed
4
//! # Dynamo LLM Protocols
5
6
//!
//! This module contains the protocols, i.e. messages formats, used to exchange requests and responses
Neelay Shah's avatar
Neelay Shah committed
7
//! both publicly via the HTTP API and internally between Dynamo components.
8
9
//!

Ryan Olson's avatar
Ryan Olson committed
10
use futures::{Stream, StreamExt};
11
12
use serde::{Deserialize, Serialize};

13
pub mod anthropic;
14
15
16
pub mod codec;
pub mod common;
pub mod openai;
17
pub mod tensor;
18
pub(crate) mod unified;
19
20
21

/// The token ID type
pub type TokenIdType = u32;
22
pub use dynamo_runtime::engine::DataStream;
23
24
25

// TODO: This is an awkward dependency that we need to address
// Originally, all the Annotated/SSE Codec bits where in the LLM protocol module; however, [Annotated]
Neelay Shah's avatar
Neelay Shah committed
26
// has become the common response envelope for dynamo.
27
// We may want to move the original Annotated back here and has a Infallible conversion to the the
Neelay Shah's avatar
Neelay Shah committed
28
29
// ResponseEnvelop in dynamo.
pub use dynamo_runtime::protocols::annotated::Annotated;
30
31
32
33
34
35
36
37
38
39
40
41
42

/// The LLM responses have multiple different fields and nests of objects to get to the actual
/// text completion returned. This trait can be applied to the `choice` level objects to extract
/// the completion text.
///
/// To avoid an optional, if no completion text is found, the [`ContentProvider::content`] should
/// return an empty string.
pub trait ContentProvider {
    fn content(&self) -> String;
}

/// Converts of a stream of [codec::Message]s into a stream of [Annotated]s.
pub fn convert_sse_stream<R>(
Ryan Olson's avatar
Ryan Olson committed
43
44
    stream: impl Stream<Item = Result<codec::Message, codec::SseCodecError>>,
) -> impl Stream<Item = Annotated<R>>
45
46
47
where
    R: for<'de> Deserialize<'de> + Serialize,
{
Ryan Olson's avatar
Ryan Olson committed
48
    stream.map(|message| match message {
49
50
51
52
53
54
55
56
        Ok(message) => {
            let delta = Annotated::<R>::try_from(message);
            match delta {
                Ok(delta) => delta,
                Err(e) => Annotated::from_error(e.to_string()),
            }
        }
        Err(e) => Annotated::from_error(e.to_string()),
Ryan Olson's avatar
Ryan Olson committed
57
    })
58
}