Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
dynamo
Commits
c2f0baa4
Unverified
Commit
c2f0baa4
authored
Sep 02, 2025
by
Ayush Agarwal
Committed by
GitHub
Sep 02, 2025
Browse files
fix: compile python regex once (#2810)
Signed-off-by:
ayushag
<
ayushag@nvidia.com
>
parent
42669baa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
4 deletions
+14
-4
lib/parsers/src/tool_calling/pythonic_parser.rs
lib/parsers/src/tool_calling/pythonic_parser.rs
+14
-4
No files found.
lib/parsers/src/tool_calling/pythonic_parser.rs
View file @
c2f0baa4
...
...
@@ -2,13 +2,26 @@
// SPDX-License-Identifier: Apache-2.0
use
super
::
response
::{
CalledFunction
,
ToolCallResponse
,
ToolCallType
};
use
regex
::
Regex
;
use
rustpython_parser
::{
Mode
,
ast
::{
Constant
,
Expr
,
Mod
},
parse
,
};
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
{
// Remove unexpected python tags if any
message
...
...
@@ -17,10 +30,7 @@ fn strip_text(message: &str) -> String {
}
fn
get_regex_matches
(
message
:
&
str
)
->
Vec
<
String
>
{
use
regex
::
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
re
=
get_pythonic_regex
();
let
mut
matches
=
Vec
::
new
();
for
cap
in
re
.find_iter
(
message
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment