aggregators.rs 4.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Neelay Shah's avatar
Neelay Shah committed
16
use dynamo_llm::protocols::{
17
18
    ContentProvider, DataStream,
    codec::{Message, SseCodecError, create_message_stream},
19
    openai::{
20
        ParsingOptions,
21
22
        chat_completions::{NvCreateChatCompletionResponse, aggregator::ChatCompletionAggregator},
        completions::NvCreateCompletionResponse,
23
    },
24
};
25
use futures::StreamExt;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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);
41
42
43
44
45
46
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
47
48
49

    // todo: provide a cleaner way to extract the content from choices
    assert_eq!(
Paul Hendricks's avatar
Paul Hendricks committed
50
51
52
53
54
55
56
57
        result
            .choices
            .first()
            .unwrap()
            .message
            .content
            .clone()
            .expect("there to be content"),
58
        "Deep learning is a subfield of machine learning that involves the use of artificial"
Paul Hendricks's avatar
Paul Hendricks committed
59
            .to_string()
60
61
62
63
64
65
    );
}

#[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");
66
67
68
69
70
71
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
72

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

#[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");
89
90
91
92
93
94
    let result = NvCreateChatCompletionResponse::from_sse_stream(
        Box::pin(stream),
        ParsingOptions::default(),
    )
    .await
    .unwrap();
95

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

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

    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);
129
130
131
132
    let result =
        NvCreateCompletionResponse::from_sse_stream(Box::pin(stream), ParsingOptions::default())
            .await
            .unwrap();
133
134
135

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