"tests/entrypoints/instrumentator/test_metrics.py" did not exist on "2942970d4462c1d938db4f142008e106702c0dc5"
openai_completions.rs 3.52 KB
Newer Older
1
2
3
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

4
use dynamo_async_openai::types::CreateCompletionRequestArgs;
5
use dynamo_llm::protocols::openai::{completions::NvCreateCompletionRequest, validate};
6
7
8
9
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
struct CompletionSample {
10
    request: NvCreateCompletionRequest,
11
12
13
14
15
16
    description: String,
}

impl CompletionSample {
    fn new<F>(description: impl Into<String>, configure: F) -> Result<Self, String>
    where
17
        F: FnOnce(&mut CreateCompletionRequestArgs) -> &mut CreateCompletionRequestArgs,
18
    {
19
        let mut builder = CreateCompletionRequestArgs::default();
20
21
22
23
        builder
            .model("gpt-3.5-turbo")
            .prompt("What is the meaning of life?");
        configure(&mut builder);
24
25
26

        let inner = builder.build().unwrap();

27
28
29
30
        let request = NvCreateCompletionRequest {
            inner,
            common: Default::default(),
            nvext: None,
31
            metadata: None,
32
        };
33

34
        Ok(Self {
35
            request,
36
37
38
39
40
41
42
            description: description.into(),
        })
    }
}

#[test]
fn minimum_viable_request() {
43
    let request = CreateCompletionRequestArgs::default()
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        .prompt("What is the meaning of life?")
        .model("gpt-3.5-turbo")
        .build()
        .expect("error building request");

    insta::assert_json_snapshot!(request);
}

#[test]
fn valid_samples() {
    let mut settings = insta::Settings::clone_current();
    settings.set_sort_maps(true);
    let _guard = settings.bind_to_scope();

    let samples = build_samples().expect("error building samples");

    // iteration on all sample and call validate and expect it to be ok
    for sample in &samples {
        insta::with_settings!({
            description => &sample.description,
        }, {
        insta::assert_json_snapshot!(sample.request);
        });
    }
}
#[allow(clippy::vec_init_then_push)]
fn build_samples() -> Result<Vec<CompletionSample>, String> {
    let mut samples = Vec::new();

    samples.push(CompletionSample::new(
        "should have only prompt and model fields",
        |builder| builder,
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and max_tokens fields",
Paul Hendricks's avatar
Paul Hendricks committed
80
        |builder| builder.max_tokens(10_u32),
81
82
83
84
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and temperature fields",
85
        |builder| builder.temperature(validate::MIN_TEMPERATURE),
86
87
88
89
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and top_p fields",
90
        |builder| builder.top_p(validate::MIN_TOP_P),
91
92
93
94
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and frequency_penalty fields",
95
        |builder| builder.frequency_penalty(validate::MIN_FREQUENCY_PENALTY),
96
97
98
99
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and presence_penalty fields",
100
        |builder| builder.presence_penalty(validate::MIN_PRESENCE_PENALTY),
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and stop fields",
        |builder| builder.stop(vec!["\n".to_string()]),
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and echo fields",
        |builder| builder.echo(true),
    )?);

    samples.push(CompletionSample::new(
        "should have prompt, model, and stream fields",
        |builder| builder.stream(true),
    )?);

    Ok(samples)
}