| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | // This module is shared by multiple benchmark targets. |
| 5 | // There could be false positives for unused code or imports, and fixing them would lead to some other benchmarks failing to compile. |
| 6 | #![allow(dead_code)] |
| 7 | #![allow(unused_imports)] |
| 8 | |
| 9 | mod unified; |
| 10 | |
| 11 | use futures::FutureExt as _; |
| 12 | use openai_protocol::common::{Function as OpenAiFunction, Tool as OpenAiTool}; |
| 13 | use tool_parser::traits::ToolParser as ExternalToolParser; |
| 14 | pub(super) use unified::{attributed_chunks, feed_unified_parser}; |
| 15 | use vllm_parser::tool::test_utils::collect_stream; |
| 16 | use vllm_parser::tool::{Tool, ToolParser}; |
| 17 | |
| 18 | pub(super) fn openai_tools(tools: &[Tool]) -> Vec<OpenAiTool> { |
| 19 | tools |
| 20 | .iter() |
| 21 | .map(|tool| OpenAiTool { |
| 22 | tool_type: "function".to_string(), |
| 23 | function: OpenAiFunction { |
| 24 | name: tool.name.clone(), |
| 25 | description: tool.description.clone(), |
| 26 | parameters: tool.parameters.clone(), |
| 27 | strict: tool.strict, |
| 28 | }, |
| 29 | }) |
| 30 | .collect() |
| 31 | } |
| 32 | |
| 33 | pub(super) fn feed_parser(parser: &mut dyn ToolParser, chunks: &[&str]) -> (String, usize) { |
| 34 | let result = collect_stream(parser, chunks); |
| 35 | (result.normal_text(), result.calls().len()) |
| 36 | } |
| 37 | |
| 38 | pub(super) fn feed_external_parser( |
| 39 | parser: &mut impl ExternalToolParser, |
| 40 | tools: &[OpenAiTool], |
| 41 | chunks: &[&str], |
| 42 | ) -> (String, usize) { |
| 43 | ExternalToolParser::reset(parser); |
| 44 | |
| 45 | let mut normal_text = String::new(); |
| 46 | let mut calls_len = 0; |
| 47 | for chunk in chunks { |
| 48 | let delta = parser |
| 49 | .parse_incremental(chunk, tools) |
| 50 | .now_or_never() |
| 51 | .expect("external parser should not suspend") |
| 52 | .expect("chunk should parse"); |
| 53 | normal_text.push_str(&delta.normal_text); |
| 54 | calls_len += delta.calls.len(); |
| 55 | } |
| 56 | calls_len += parser.get_unstreamed_tool_args().unwrap_or_default().len(); |
| 57 | (normal_text, calls_len) |
| 58 | } |