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

4
use dynamo_async_openai::types::ChatCompletionMessageContent;
Neelay Shah's avatar
Neelay Shah committed
5
use dynamo_llm::protocols::{
6
7
    ContentProvider, DataStream,
    codec::{Message, SseCodecError, create_message_stream},
8
    openai::{
9
        ParsingOptions,
10
11
        chat_completions::{NvCreateChatCompletionResponse, aggregator::ChatCompletionAggregator},
        completions::NvCreateCompletionResponse,
12
    },
13
};
14
use futures::StreamExt;
15

16
17
18
19
20
21
22
fn get_text(content: &ChatCompletionMessageContent) -> &str {
    match content {
        ChatCompletionMessageContent::Text(text) => text.as_str(),
        ChatCompletionMessageContent::Parts(_) => "",
    }
}

23
24
25
26
27
28
29
30
31
32
33
34
35
36
const CMPL_ROOT_PATH: &str = "tests/data/replays/meta/llama-3.1-8b-instruct/completions";
const CHAT_ROOT_PATH: &str = "tests/data/replays/meta/llama-3.1-8b-instruct/chat_completions";

fn create_stream(root_path: &str, file_name: &str) -> DataStream<Result<Message, SseCodecError>> {
    let data = std::fs::read_to_string(format!("{}/{}", root_path, file_name)).unwrap();
    create_message_stream(&data)
}

#[tokio::test]
async fn test_openai_chat_stream() {
    let data = std::fs::read_to_string("tests/data/replays/meta/llama-3.1-8b-instruct/chat_completions/chat-completion.streaming.1").unwrap();

    // note: we are only taking the first 16 messages to keep the size of the response small
    let stream = create_message_stream(&data).take(16);
37
38
39
40
41
42
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
43
44
45

    // todo: provide a cleaner way to extract the content from choices
    assert_eq!(
46
47
48
49
50
51
52
53
54
55
        get_text(
            result
                .choices
                .first()
                .unwrap()
                .message
                .content
                .as_ref()
                .expect("there to be content")
        ),
56
57
58
59
60
61
62
        "Deep learning is a subfield of machine learning that involves the use of artificial"
    );
}

#[tokio::test]
async fn test_openai_chat_edge_case_multi_line_data() {
    let stream = create_stream(CHAT_ROOT_PATH, "edge_cases/valid-multi-line-data");
63
64
65
66
67
68
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
69

Paul Hendricks's avatar
Paul Hendricks committed
70
    assert_eq!(
71
72
73
74
75
76
77
78
79
80
81
        get_text(
            result
                .choices
                .first()
                .unwrap()
                .message
                .content
                .as_ref()
                .expect("there to be content")
        ),
        "Deep learning"
Paul Hendricks's avatar
Paul Hendricks committed
82
    );
83
84
85
86
87
}

#[tokio::test]
async fn test_openai_chat_edge_case_comments_per_response() {
    let stream = create_stream(CHAT_ROOT_PATH, "edge_cases/valid-comments_per_response");
88
89
90
91
92
93
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
94

Paul Hendricks's avatar
Paul Hendricks committed
95
    assert_eq!(
96
97
98
99
100
101
102
103
104
105
106
        get_text(
            result
                .choices
                .first()
                .unwrap()
                .message
                .content
                .as_ref()
                .expect("there to be content")
        ),
        "Deep learning"
Paul Hendricks's avatar
Paul Hendricks committed
107
    );
108
109
110
111
112
}

#[tokio::test]
async fn test_openai_chat_edge_case_invalid_deserialize_error() {
    let stream = create_stream(CHAT_ROOT_PATH, "edge_cases/invalid-deserialize_error");
113
114
115
116
117
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await;
118
119
120
121
122
123
124
125
126
127
128
129

    assert!(result.is_err());
    // insta::assert_debug_snapshot!(result);
}

// =============================
// Completions (/v1/completions)
// =============================

#[tokio::test]
async fn test_openai_cmpl_stream() {
    let stream = create_stream(CMPL_ROOT_PATH, "completion.streaming.1").take(16);
130
131
132
133
    let result =
        NvCreateCompletionResponse::from_sse_stream(Box::pin(stream), ParsingOptions::default())
            .await
            .unwrap();
134
135
136

    // todo: provide a cleaner way to extract the content from choices
    assert_eq!(
137
        result.inner.choices.first().unwrap().content(),
138
139
140
        " This is a question that is often asked by those outside of AI research and development"
    );
}