API Reference
This page is generated from source code docstrings via mkdocstrings.
Package
gimkit
Core Modules
gimkit.guides
FormMixin
Source code in src/gimkit/guides.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 | |
datetime(name=None, require_date=True, require_time=True)
A date and/or time string, e.g., 2023-10-05, 14:30:00, 2023-10-05 14:30:00, etc.
Source code in src/gimkit/guides.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
select(name=None, choices=None)
Choose one from the given options.
Source code in src/gimkit/guides.py
22 23 24 25 26 27 28 | |
single_word(name=None)
A single word without spaces.
Source code in src/gimkit/guides.py
18 19 20 | |
PersonalInfoMixin
Source code in src/gimkit/guides.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
e_mail(name=None)
An email address, e.g., john.doe@example.com, alice@example.com, etc.
Source code in src/gimkit/guides.py
66 67 68 69 70 71 | |
person_name(name=None)
A person's name, e.g., John Doe, Alice, Bob, Charlie Brown, 张三, etc.
Source code in src/gimkit/guides.py
53 54 55 | |
phone_number(name=None)
A phone number, e.g., +1-123-456-7890, (123) 456-7890, 123-456-7890, etc.
Source code in src/gimkit/guides.py
57 58 59 60 61 62 63 64 | |
gimkit.schemas
Defines the schema for GIM.
MaskedTag dataclass
Represents a masked tag in the GIM schema.
A masked tag consists of three main types of components: 1. Tag ID: An integer identifier for the tag, represented as m_{id} in the tag attributes. 2. Tag content: The content located between the opening and closing masked tag markers. 3. Tag common attributes: All other tag attributes aside from the ID (e.g., name, desc, regex).
Example of a masked tag
<|MASKED id="m_0" name="xxx" desc="xxx" regex="xxx"|>content here<|/MASKED|>
Source code in src/gimkit/schemas.py
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 | |
parse_parts(s)
Parse a string into a list of ContextParts (str or MaskedTag).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s | str | The string to be parsed. Note it only contains masked tags or plain texts. Tag id may start from any non-negative integer, but must be in order 0, 1, 2, ... | required |
Returns:
| Type | Description |
|---|---|
list[ContextPart] | list[ContextPart]: A list of ContextParts (str or MaskedTag). |
Source code in src/gimkit/schemas.py
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 | |
parse_tags(s, prefix=None, suffix=None)
Parse a string into a list of MaskedTags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s | str | The string to be parsed. It may be wrapped with a prefix and suffix. Tag id may start from any non-negative integer, but must be in order 0, 1, 2, ... | required |
prefix | str | None | The prefix tag that the string should start with. Default is None. | None |
suffix | str | None | The suffix tag that the string should end with. Default is None. | None |
Returns:
| Type | Description |
|---|---|
list[MaskedTag] | list[MaskedTag]: A list of MaskedTags. |
Source code in src/gimkit/schemas.py
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 288 289 290 291 292 293 294 295 296 297 298 | |
validate(query, response)
Validate the GIM query or/and GIM response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query | str | Wrapped with query prefix and suffix. | required |
response | str | Wrapped with response prefix and suffix. | required |
Raises:
| Type | Description |
|---|---|
ValueError | If both query and response are None. |
InvalidFormatError | If the format of query or response is invalid, or if the number of masked tags or their ids do not match between query and response. |
Source code in src/gimkit/schemas.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
gimkit.contexts
Query
Bases: Context
Source code in src/gimkit/contexts.py
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 207 208 | |
infill(response)
Fills tags in this query (self) with content from the provided response.
Source code in src/gimkit/contexts.py
200 201 202 | |
Response
Bases: Context
Source code in src/gimkit/contexts.py
211 212 213 214 215 216 217 218 219 220 | |
infill(query)
Fills the tags in the provided query with content from this response (self).
Source code in src/gimkit/contexts.py
215 216 217 | |
infill(query, response, strict=False)
Combines query and response by infilling missing content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query | Query | ContextInput | The query containing masked tags to be filled | required |
response | Response | ContextInput | The response containing content to fill the tags | required |
strict | bool | If True, raises errors on format mismatches. If False, attempts to repair missing ending tags in a best-effort manner. | False |
Returns:
| Type | Description |
|---|---|
Result | A Result object with tags filled from the response |
Raises:
| Type | Description |
|---|---|
InvalidFormatError | If strict=True and there are format mismatches |
Source code in src/gimkit/contexts.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
gimkit.dsls
Define DSL builders for various output types.
build_cfgconstructs a context-free grammar (CFG) using LLGuidance syntaxbuild_json_schemaconstructs a JSON schema representing the response structure.
build_cfg(query)
Build an LLGuidance context-free grammar (CFG) string based on the query object.
Constructs a flattened grammar structure compatible with LLGuidance's suffix/capture logic.
Ref:
- https://github.com/guidance-ai/llguidance/blob/main/docs/syntax.md: Incomplete documentation of llguidance grammar syntax
- https://github.com/guidance-ai/guidance/blob/main/guidance/_ast.py: LarkSerializer implementation
- https://github.com/guidance-ai/llguidance: Source code
Real-World Example:
```python
query = '<|GIM_QUERY|>The capital of <|MASKED desc="single word" regex="中国|法国"|><|/MASKED|> is Beijing<|MASKED desc="punctuation mark" regex="\."|><|/MASKED|><|/GIM_QUERY|>'
print(repr(build_cfg(Query(query))))
>>> '%llguidance {}
start: "<|GIM_RESPONSE|>" REGEX "<|MASKED id=\"m_0\"|>" m_0 REGEX "<|MASKED id=\"m_1\"|>" m_1 REGEX "<|/GIM_RESPONSE|>" REGEX: /\s*/ m_0[capture, suffix="<|/MASKED|>"]: T_0 m_1[capture, suffix="<|/MASKED|>"]: T_1 T_0: /中国|法国/ T_1: /./ ' ```
Source code in src/gimkit/dsls.py
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 | |
build_json_schema(query)
Build a JSON schema dictionary based on the query object.
The JSON schema represents the response structure where each masked tag becomes a field in the JSON object. The field name is "m_{id}" to match the tag id, and patterns are applied when regex is specified.
Source code in src/gimkit/dsls.py
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 | |
gimkit.prompts
For models that weren't trained using the Guided Infilling Modeling method, we may be able to achieve functionality similar to GIM by using a system prompt and few-shot prompting.
gimkit.log
gimkit.exceptions
GIMError
Bases: Exception
Base exception class for GIM-related errors.
Source code in src/gimkit/exceptions.py
1 2 | |
InvalidFormatError
Bases: GIMError
Exception raised for invalid GIM query/response format.
Source code in src/gimkit/exceptions.py
5 6 | |
Model Backends
gimkit.models.base
gimkit.models.openai
gimkit.models.vllm
gimkit.models.vllm_offline
gimkit.models.utils
get_outlines_model_input(model_input, output_type, use_gim_prompt, include_grammar=False, force_chat_input=False)
Transform the model input to an Outlines-compatible format.
Source code in src/gimkit/models/utils.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 | |
get_outlines_output_type(model_input, output_type)
Transform the output type to an Outlines-compatible format.
Source code in src/gimkit/models/utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
infill_responses(query, responses, json_responses=False)
infill_responses(query: ContextInput | Query, responses: str, json_responses: bool = False) -> Result
infill_responses(query: ContextInput | Query, responses: list[str], json_responses: bool = False) -> list[Result]
Infill the provided query with content from the GIM responses or JSON responses.
Source code in src/gimkit/models/utils.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
json_responses_to_gim_response(json_response)
Convert a JSON response string to a GIM response string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_response | str | A JSON string representing the response. | required |
Returns:
| Type | Description |
|---|---|
str | A properly formatted GIM response string. |
Raises:
| Type | Description |
|---|---|
ValueError | If any key does not follow the "m_X" format where X is an integer. |
Source code in src/gimkit/models/utils.py
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 | |