text.rs 6.44 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
17
use dynamo_llm::types::openai::chat_completions::{
    NvCreateChatCompletionRequest, OpenAIChatCompletionsStreamingEngine,
18
};
19
use dynamo_runtime::{pipeline::Context, runtime::CancellationToken, Runtime};
20
use futures::StreamExt;
21
use std::io::{ErrorKind, Write};
22

23
use crate::input::common;
24
25
26
use crate::EngineConfig;

/// Max response tokens for each single query. Must be less than model context size.
27
/// TODO: Cmd line flag to overwrite this
Paul Hendricks's avatar
Paul Hendricks committed
28
const MAX_TOKENS: u32 = 8192;
29
30

pub async fn run(
31
    runtime: Runtime,
32
    cancel_token: CancellationToken,
33
    single_prompt: Option<String>,
34
35
36
37
38
39
    engine_config: EngineConfig,
) -> anyhow::Result<()> {
    let (service_name, engine, inspect_template): (
        String,
        OpenAIChatCompletionsStreamingEngine,
        bool,
40
    ) = common::prepare_engine(runtime.clone(), engine_config).await?;
41
42
43
44
45
46
47
48
    main_loop(
        cancel_token,
        &service_name,
        engine,
        single_prompt,
        inspect_template,
    )
    .await
49
50
51
52
53
54
}

async fn main_loop(
    cancel_token: CancellationToken,
    service_name: &str,
    engine: OpenAIChatCompletionsStreamingEngine,
55
    mut initial_prompt: Option<String>,
Paul Hendricks's avatar
Paul Hendricks committed
56
    _inspect_template: bool,
57
) -> anyhow::Result<()> {
58
59
60
    if initial_prompt.is_none() {
        tracing::info!("Ctrl-c to exit");
    }
61
62
    let theme = dialoguer::theme::ColorfulTheme::default();

63
64
65
    // Initial prompt is the pipe case: `echo "Hello" | dynamo-run ..`
    // We run that single prompt and exit
    let single = initial_prompt.is_some();
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    let mut history = dialoguer::BasicHistory::default();
    let mut messages = vec![];
    while !cancel_token.is_cancelled() {
        // User input
        let prompt = match initial_prompt.take() {
            Some(p) => p,
            None => {
                let input_ui = dialoguer::Input::<String>::with_theme(&theme)
                    .history_with(&mut history)
                    .with_prompt("User");
                match input_ui.interact_text() {
                    Ok(prompt) => prompt,
                    Err(dialoguer::Error::IO(err)) => {
                        match err.kind() {
                            ErrorKind::Interrupted => {
                                // Ctrl-C
                                // Unfortunately I could not make dialoguer handle Ctrl-d
                            }
                            k => {
                                tracing::info!("IO error: {k}");
                            }
                        }
                        break;
                    }
                }
            }
        };
Paul Hendricks's avatar
Paul Hendricks committed
93
94
95
96
97
98
99
100
101

        // Construct messages
        let user_message = async_openai::types::ChatCompletionRequestMessage::User(
            async_openai::types::ChatCompletionRequestUserMessage {
                content: async_openai::types::ChatCompletionRequestUserMessageContent::Text(prompt),
                name: None,
            },
        );
        messages.push(user_message);
102
103

        // Request
Paul Hendricks's avatar
Paul Hendricks committed
104
105
        let inner = async_openai::types::CreateChatCompletionRequestArgs::default()
            .messages(messages.clone())
106
107
            .model(service_name)
            .stream(true)
108
109
110
            .max_completion_tokens(MAX_TOKENS)
            .temperature(0.7)
            .n(1) // only generate one response
Paul Hendricks's avatar
Paul Hendricks committed
111
112
113
114
115
116
117
118
            .build()?;

        // TODO We cannot set min_tokens with async-openai
        // if inspect_template {
        //     // This makes the pre-processor ignore stop tokens
        //     req_builder.min_tokens(8192);
        // }

119
        let req = NvCreateChatCompletionRequest { inner, nvext: None };
120
121
122
123
124
125
126
127

        // Call the model
        let mut stream = engine.generate(Context::new(req)).await?;

        // Stream the output to stdout
        let mut stdout = std::io::stdout();
        let mut assistant_message = String::new();
        while let Some(item) = stream.next().await {
128
129
130
            if cancel_token.is_cancelled() {
                break;
            }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            match (item.data.as_ref(), item.event.as_deref()) {
                (Some(data), _) => {
                    // Normal case
                    let entry = data.inner.choices.first();
                    let chat_comp = entry.as_ref().unwrap();
                    if let Some(c) = &chat_comp.delta.content {
                        let _ = stdout.write(c.as_bytes());
                        let _ = stdout.flush();
                        assistant_message += c;
                    }
                    if chat_comp.finish_reason.is_some() {
                        tracing::trace!("finish reason: {:?}", chat_comp.finish_reason.unwrap());
                        break;
                    }
                }
                (None, Some("error")) => {
                    // There's only one error but we loop in case that changes
                    for err in item.comment.unwrap_or_default() {
                        tracing::error!("Engine error: {err}");
                    }
                }
                (None, Some(annotation)) => {
                    tracing::debug!("Annotation. {annotation}: {:?}", item.comment);
                }
                _ => {
                    unreachable!("Event from engine with no data, no error, no annotation.");
                }
158
159
160
161
            }
        }
        println!();

Paul Hendricks's avatar
Paul Hendricks committed
162
163
164
165
166
167
168
169
        let assistant_content =
            async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(
                assistant_message,
            );

        let assistant_message = async_openai::types::ChatCompletionRequestMessage::Assistant(
            async_openai::types::ChatCompletionRequestAssistantMessage {
                content: Some(assistant_content),
170
                ..Default::default()
Paul Hendricks's avatar
Paul Hendricks committed
171
172
173
            },
        );
        messages.push(assistant_message);
174
175
176
177

        if single {
            break;
        }
178
179
180
181
    }
    println!();
    Ok(())
}