API Reference
Reference documentation for the modules and classes in bfast-llm.
Complete Interface
bfast_llm.middleware
BFastLLM
Middleware for transparently compressing LLM contexts using the B-FAST binary protocol.
Source code in bfast_llm/middleware.py
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 | |
__init__(registry=None, threshold_bytes=1024, compress_payloads=True)
Initialize BFastLLM middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[BFastRegistry]
|
A BFastRegistry instance. If None, creates an in-memory registry. |
None
|
threshold_bytes
|
int
|
Content size threshold above which compression is triggered (default: 1KB). |
1024
|
compress_payloads
|
bool
|
Whether to apply compression to BFast payloads. |
True
|
Source code in bfast_llm/middleware.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
compress_messages(messages)
Interprets messages, serializing large payloads into B-FAST binaries and swapping them with references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List[Dict[str, Any]]
|
List of message dictionaries in standard Chat Completion format. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[Dict[str, Any]], bool]
|
A tuple of (modified_messages, was_compressed) |
Source code in bfast_llm/middleware.py
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 | |
get_tools()
Get the retrieve tool definition to inject into the LLM call.
Source code in bfast_llm/middleware.py
156 157 158 | |
handle_tool_call(tool_call)
Handle a tool call if it is 'bfast_retrieve'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_call
|
Any
|
The tool call object (e.g. from OpenAI response, having 'function' attribute with 'name' and 'arguments') |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The string result of the retrieval if handled, otherwise None. |
Source code in bfast_llm/middleware.py
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 | |
bfast_tune(client, threshold_bytes=1024, compress_payloads=True)
Tune an LLM client instance (e.g. OpenAI) to automatically apply B-FAST prompt compression and handle retrieval tool calls transparently.
Source code in bfast_llm/middleware.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
wrap_completion(completion_func, threshold_bytes=1024, compress_payloads=True)
Wrap any OpenAI-compatible chat completion function to automatically apply B-FAST compression and transparently handle retrieval tool calls.
Source code in bfast_llm/middleware.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
bfast_llm.summarizer
BFastSummarizer
Helper to summarize B-FAST data structure and generate token-efficient representation.
Source code in bfast_llm/summarizer.py
7 8 9 10 11 12 13 14 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 | |
summarize(data, max_list_items_preview=3)
classmethod
Generate a concise text summary of a python object decoded from B-FAST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Any Python object |
required |
max_list_items_preview
|
int
|
How many items to preview if listing individual elements |
3
|
Returns:
| Type | Description |
|---|---|
str
|
A string describing the structure and summary of the data. |
Source code in bfast_llm/summarizer.py
10 11 12 13 14 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 | |
bfast_llm.decoder
BFastDecoder
Decoder for the B-FAST binary serialization protocol.
Source code in bfast_llm/decoder.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
decode(data)
classmethod
Decode B-FAST binary data to Python objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[bytes, bytearray]
|
bytes or bytearray containing B-FAST data compressed |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Decoded Python object |
Source code in bfast_llm/decoder.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
BFastError
Bases: Exception
Exception raised for B-FAST parsing and decoding errors.
Source code in bfast_llm/decoder.py
12 13 14 15 | |
bfast_llm.registry
BFastRegistry
Registry for managing and storing B-FAST binary payloads.
Source code in bfast_llm/registry.py
10 11 12 13 14 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 | |
__init__(db_path=None)
Initialize the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Optional[Union[str, Path]]
|
Path to SQLite file for persistent storage. If None, uses in-memory storage. |
None
|
Source code in bfast_llm/registry.py
13 14 15 16 17 18 19 20 21 22 23 24 25 | |
clear()
Clear all payloads in the registry.
Source code in bfast_llm/registry.py
136 137 138 139 140 141 142 143 144 145 146 | |
get(ref_id)
Retrieve the binary payload by reference ID.
Source code in bfast_llm/registry.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
get_decoded(ref_id)
Retrieve and decode the payload by reference ID.
Source code in bfast_llm/registry.py
129 130 131 132 133 134 | |
get_summary(ref_id)
Retrieve the summary of a payload by reference ID.
Source code in bfast_llm/registry.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
register(binary_data, summary=None)
Register a B-FAST binary payload in the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_data
|
bytes
|
The B-FAST binary payload (bytes) |
required |
summary
|
Optional[str]
|
Optional pre-computed summary. If None, it will be automatically generated. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A formatted XML/HTML reference tag that can be sent to the LLM. |
Source code in bfast_llm/registry.py
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 | |
bfast_llm.tool
BFastTool
Implement LLM tools for interacting with B-FAST context.
Source code in bfast_llm/tool.py
9 10 11 12 13 14 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 | |
bfast_retrieve(ref_id, format='markdown')
Retrieve and decode a B-FAST payload from the registry, formatting it as requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_id
|
str
|
The reference ID of the payload (e.g., 'bfast_abc123') |
required |
format
|
str
|
Output format. One of 'markdown', 'json', 'yaml', or 'csv'. Default is 'markdown'. |
'markdown'
|
Returns:
| Type | Description |
|---|---|
str
|
Decoded payload formatted as a string. |
Source code in bfast_llm/tool.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 | |
get_openai_tool_definition()
Get the OpenAI/Anthropic compatible tool schema for bfast_retrieve.
Source code in bfast_llm/tool.py
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 | |