src.llm.tool_detection.manual_detection_strategy.ManualToolCallDetectionStrategy
Bases: BaseToolCallDetectionStrategy
A strategy for detecting tool calls in streaming LLM output using pattern matching.
This class implements manual detection of tool calls by processing streaming chunks of text using the Aho-Corasick algorithm for pattern matching. It maintains internal state to track tool call boundaries and accumulate content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser
|
BaseToolCallParser
|
Parser instance for processing detected tool calls. |
required |
pattern_config_path
|
str
|
Path to YAML config file containing tool call patterns. Defaults to "src/configs/tool_call_patterns.yaml". |
'src/configs/tool_call_patterns.yaml'
|
Attributes:
Name | Type | Description |
---|---|---|
tool_call_parser |
BaseToolCallParser
|
Parser for processing tool calls. |
pattern_detector |
AhoCorasickBufferedProcessor
|
Pattern matching processor. |
pre_tool_call_content |
List[str]
|
Buffer for content before tool call. |
tool_call_buffer |
str
|
Buffer for accumulating tool call content. |
in_tool_call |
bool
|
Flag indicating if currently processing a tool call. |
accumulation_mode |
bool
|
Flag for content accumulation mode. |
Example
parser = JSONToolCallParser()
detector = ManualToolCallDetectionStrategy(parser)
# Process streaming chunks
async for chunk in stream:
result = await detector.detect_chunk(chunk, context)
if result.state == DetectionState.COMPLETE_MATCH:
# Handle detected tool call
process_tool_calls(result.tool_calls)
Source code in src/llm/tool_detection/manual_detection_strategy.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
detect_chunk(sse_chunk, context)
async
Process a single chunk of streaming content for tool call detection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sse_chunk
|
SSEChunk
|
The chunk of streaming content to process. |
required |
context
|
StreamContext
|
Context information for the current stream. |
required |
Returns:
Name | Type | Description |
---|---|---|
DetectionResult |
DetectionResult
|
Result of processing the chunk, including detection state and any content or tool calls found. |
Source code in src/llm/tool_detection/manual_detection_strategy.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
finalize_detection(context)
async
Finalize the detection process and handle any accumulated content.
This method is called at the end of a stream to process any remaining content in the buffers and return final detection results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
StreamContext
|
Context information for the current stream. |
required |
Returns:
Name | Type | Description |
---|---|---|
DetectionResult |
DetectionResult
|
Final result of the detection process, including any complete tool calls or remaining content. |
Source code in src/llm/tool_detection/manual_detection_strategy.py
129 130 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
reset()
Reset all internal state of the detection strategy.
This method clears all buffers and resets flags to their initial state. Should be called between processing different streams or after errors.
Source code in src/llm/tool_detection/manual_detection_strategy.py
60 61 62 63 64 65 66 67 68 69 70 71 |
|