"lib/llm/src/key_value_store/mem.rs" did not exist on "16310b269f866e6f4b7968ba6780e54a4f7b76f6"
echo_full.rs 3.56 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.

16
use std::sync::Arc;
17
18
19
20

use async_stream::stream;
use async_trait::async_trait;

Neelay Shah's avatar
Neelay Shah committed
21
use dynamo_llm::protocols::openai::chat_completions::{
22
    NvCreateChatCompletionRequest, NvCreateChatCompletionStreamResponse,
23
};
Neelay Shah's avatar
Neelay Shah committed
24
25
26
27
use dynamo_llm::types::openai::chat_completions::OpenAIChatCompletionsStreamingEngine;
use dynamo_runtime::engine::{AsyncEngine, AsyncEngineContextProvider, ResponseStream};
use dynamo_runtime::pipeline::{Error, ManyOut, SingleIn};
use dynamo_runtime::protocols::annotated::Annotated;
28

29
use super::common::TOKEN_ECHO_DELAY;
30
31
32
33
34
35
36
37
38
39
40

/// Engine that accepts un-preprocessed requests and echos the prompt back as the response
/// Useful for testing ingress such as service-http.
struct EchoEngineFull {}
pub fn make_engine_full() -> OpenAIChatCompletionsStreamingEngine {
    Arc::new(EchoEngineFull {})
}

#[async_trait]
impl
    AsyncEngine<
41
        SingleIn<NvCreateChatCompletionRequest>,
42
        ManyOut<Annotated<NvCreateChatCompletionStreamResponse>>,
43
44
45
46
47
        Error,
    > for EchoEngineFull
{
    async fn generate(
        &self,
48
        incoming_request: SingleIn<NvCreateChatCompletionRequest>,
49
    ) -> Result<ManyOut<Annotated<NvCreateChatCompletionStreamResponse>>, Error> {
50
51
52
        let (request, context) = incoming_request.transfer(());
        let deltas = request.response_generator();
        let ctx = context.context();
Paul Hendricks's avatar
Paul Hendricks committed
53
54
55
56
57
58
59
60
61
62
        let req = request.inner.messages.into_iter().last().unwrap();

        let prompt = match req {
            async_openai::types::ChatCompletionRequestMessage::User(user_msg) => {
                match user_msg.content {
                    async_openai::types::ChatCompletionRequestUserMessageContent::Text(prompt) => {
                        prompt
                    }
                    _ => anyhow::bail!("Invalid request content field, expected Content::Text"),
                }
63
            }
Paul Hendricks's avatar
Paul Hendricks committed
64
            _ => anyhow::bail!("Invalid request type, expected User message"),
65
        };
Paul Hendricks's avatar
Paul Hendricks committed
66

67
68
69
        let output = stream! {
            let mut id = 1;
            for c in prompt.chars() {
70
71
                // we are returning characters not tokens, so there will be some postprocessing overhead
                tokio::time::sleep(*TOKEN_ECHO_DELAY).await;
Paul Hendricks's avatar
Paul Hendricks committed
72
                let inner = deltas.create_choice(0, Some(c.to_string()), None, None);
73
                let response = NvCreateChatCompletionStreamResponse {
Paul Hendricks's avatar
Paul Hendricks committed
74
75
76
                    inner,
                };
                yield Annotated{ id: Some(id.to_string()), data: Some(response), event: None, comment: None };
77
78
                id += 1;
            }
Paul Hendricks's avatar
Paul Hendricks committed
79
80

            let inner = deltas.create_choice(0, None, Some(async_openai::types::FinishReason::Stop), None);
81
            let response = NvCreateChatCompletionStreamResponse {
Paul Hendricks's avatar
Paul Hendricks committed
82
83
84
                inner,
            };
            yield Annotated { id: Some(id.to_string()), data: Some(response), event: None, comment: None };
85
        };
Paul Hendricks's avatar
Paul Hendricks committed
86

87
88
89
        Ok(ResponseStream::new(Box::pin(output), ctx))
    }
}