Unverified Commit c2f0baa4 authored by Ayush Agarwal's avatar Ayush Agarwal Committed by GitHub
Browse files

fix: compile python regex once (#2810)


Signed-off-by: default avatarayushag <ayushag@nvidia.com>
parent 42669baa
...@@ -2,13 +2,26 @@ ...@@ -2,13 +2,26 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use super::response::{CalledFunction, ToolCallResponse, ToolCallType}; use super::response::{CalledFunction, ToolCallResponse, ToolCallType};
use regex::Regex;
use rustpython_parser::{ use rustpython_parser::{
Mode, Mode,
ast::{Constant, Expr, Mod}, ast::{Constant, Expr, Mod},
parse, parse,
}; };
use serde_json::{Number, Value, json}; use serde_json::{Number, Value, json};
use std::sync::OnceLock;
static PYTHONIC_REGEX: OnceLock<Regex> = OnceLock::new();
/// Get the compiled regex pattern for pythonic tool calls
/// Initialize the regex pattern once, no need to compile it everytime
fn get_pythonic_regex() -> &'static Regex {
PYTHONIC_REGEX.get_or_init(|| {
// Format Structure: [tool1(arg1=val1, arg2=val2), tool2(arg1=val3)]
let pattern = r"\[([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*?,\s*)*([a-zA-Z]+\w*=.*?\s?)?\),\s*)*([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*?,\s*)*([a-zA-Z]+\w*=.*?\s*)?\)\s*)+\]";
Regex::new(pattern).expect("Failed to compile pythonic regex pattern")
})
}
fn strip_text(message: &str) -> String { fn strip_text(message: &str) -> String {
// Remove unexpected python tags if any // Remove unexpected python tags if any
message message
...@@ -17,10 +30,7 @@ fn strip_text(message: &str) -> String { ...@@ -17,10 +30,7 @@ fn strip_text(message: &str) -> String {
} }
fn get_regex_matches(message: &str) -> Vec<String> { fn get_regex_matches(message: &str) -> Vec<String> {
use regex::Regex; let re = get_pythonic_regex();
// Format Structure: [tool1(arg1=val1, arg2=val2), tool2(arg1=val3)]
let pattern = r"\[([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*?,\s*)*([a-zA-Z]+\w*=.*?\s?)?\),\s*)*([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*?,\s*)*([a-zA-Z]+\w*=.*?\s*)?\)\s*)+\]";
let re = Regex::new(pattern).unwrap();
let mut matches = Vec::new(); let mut matches = Vec::new();
for cap in re.find_iter(message) { for cap in re.find_iter(message) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment