tools.rs 2.33 KB
Newer Older
1
2
3
4
5
6
7
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

pub use super::response::*;

// Import json_parser from postprocessor module
pub use super::json_parser::*;
8
pub use super::parsers::{detect_and_parse_tool_call, ToolCallConfig};
9
10
11
12
13
14

/// Try parsing a string as a structured tool call, for aggregation usage.
///
/// If successful, returns a `ChatCompletionMessageToolCall`.
pub fn try_tool_call_parse_aggregate(
    message: &str,
15
    parser_str: Option<&str>,
16
) -> anyhow::Result<Vec<dynamo_async_openai::types::ChatCompletionMessageToolCall>> {
17
    let parsed = detect_and_parse_tool_call(message, parser_str)?;
18
19
    if parsed.is_empty() {
        return Ok(vec![]);
20
    }
21
22
23
    Ok(parsed
        .into_iter()
        .map(
24
            |parsed| dynamo_async_openai::types::ChatCompletionMessageToolCall {
25
                id: parsed.id,
26
27
                r#type: dynamo_async_openai::types::ChatCompletionToolType::Function,
                function: dynamo_async_openai::types::FunctionCall {
28
29
30
31
32
33
                    name: parsed.function.name,
                    arguments: parsed.function.arguments,
                },
            },
        )
        .collect())
34
35
36
37
38
39
40
}

/// Try parsing a string as a structured tool call, for streaming (delta) usage.
///
/// If successful, returns a `ChatCompletionMessageToolCallChunk`.
pub fn try_tool_call_parse_stream(
    message: &str,
41
    parser_str: Option<&str>,
42
) -> anyhow::Result<Vec<dynamo_async_openai::types::ChatCompletionMessageToolCallChunk>> {
43
    let parsed = detect_and_parse_tool_call(message, parser_str)?;
44
45
46
47
48
49
50
    if parsed.is_empty() {
        return Ok(vec![]);
    }
    Ok(parsed
        .into_iter()
        .enumerate()
        .map(
51
            |(idx, parsed)| dynamo_async_openai::types::ChatCompletionMessageToolCallChunk {
52
                index: idx as u32,
53
                id: Some(parsed.id),
54
55
                r#type: Some(dynamo_async_openai::types::ChatCompletionToolType::Function),
                function: Some(dynamo_async_openai::types::FunctionCallStream {
56
57
58
                    name: Some(parsed.function.name),
                    arguments: Some(parsed.function.arguments),
                }),
59
                // Add other fields as needed if required by the struct definition
60
            },
61
62
        )
        .collect())
63
}