outlines
Outlines is a Generative Model Programming Framework.
Anthropic
Bases: Model
Thin wrapper around the anthropic.Anthropic client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the anthropic.Anthropic client.
Source code in outlines/models/anthropic.py
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 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 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Anthropic
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/anthropic.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using Anthropic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
As structured generation is not supported by Anthropic, the value
of this argument must be |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The response generated by the model. |
Source code in outlines/models/anthropic.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using Anthropic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
As structured generation is not supported by Anthropic, the value
of this argument must be |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/anthropic.py
AsyncLMStudio
Bases: AsyncModel
Thin wrapper around a lmstudio.AsyncClient client.
This wrapper is used to convert the input and output types specified by the users at a higher level to arguments to the LMStudio async client.
Source code in outlines/models/lmstudio.py
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient
|
A LMStudio AsyncClient instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. If not provided, uses the default loaded model in LMStudio. |
None
|
Source code in outlines/models/lmstudio.py
close()
async
generate(model_input, output_type=None, **kwargs)
async
Generate text using LMStudio asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/lmstudio.py
generate_stream(model_input, output_type=None, **kwargs)
async
Stream text using LMStudio asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/lmstudio.py
AsyncMistral
Bases: AsyncModel
Async thin wrapper around the mistralai.Mistral client.
Converts input and output types to arguments for the mistralai.Mistral
client's async methods (chat.complete_async or chat.stream_async).
Source code in outlines/models/mistral.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mistral
|
A mistralai.Mistral client instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/mistral.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate a response from the model asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt or chat messages to generate a response from. |
required |
output_type
|
Optional[Any]
|
The desired format of the response (e.g., JSON schema). |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The response generated by the model as text. |
Source code in outlines/models/mistral.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Generate text from the model as an async stream of chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str, list, or chat input to generate from. |
required | |
output_type
|
Optional type for structured output. |
None
|
|
**inference_kwargs
|
Extra kwargs like "model" name. |
{}
|
Yields:
| Type | Description |
|---|---|
str
|
Chunks of text as they are streamed. |
Source code in outlines/models/mistral.py
AsyncOllama
Bases: AsyncModel
Thin wrapper around the ollama.AsyncClient client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the ollama.AsyncClient client.
Source code in outlines/models/ollama.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/ollama.py
generate(model_input, output_type=None, **kwargs)
async
Generate text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/ollama.py
generate_stream(model_input, output_type=None, **kwargs)
async
Stream text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/ollama.py
AsyncOpenAI
Bases: AsyncModel
Thin wrapper around the openai.AsyncOpenAI client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.AsyncOpenAI client.
Source code in outlines/models/openai.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[AsyncOpenAI, AsyncAzureOpenAI]
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/openai.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/openai.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Stream text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/openai.py
AsyncSGLang
Bases: AsyncModel
Thin async wrapper around the openai.OpenAI client used to communicate
with an SGLang server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client for the
SGLang server.
Source code in outlines/models/sglang.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
An |
required | |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
An |
required |
Source code in outlines/models/sglang.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using sglang.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/sglang.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Return a text generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/sglang.py
AsyncTGI
Bases: AsyncModel
Thin async wrapper around a huggingface_hub.AsyncInferenceClient
client used to communicate with a TGI server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the
huggingface_hub.AsyncInferenceClient client.
Source code in outlines/models/tgi.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 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 | |
__init__(client)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
A huggingface |
required |
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/tgi.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Stream text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/tgi.py
AsyncVLLM
Bases: AsyncModel
Thin async wrapper around the openai.OpenAI client used to communicate
with a vllm server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client for the
vllm server.
Source code in outlines/models/vllm.py
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 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 335 336 337 338 339 340 341 342 343 344 345 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncOpenAI
|
An |
required |
Source code in outlines/models/vllm.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using vLLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/vllm.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Stream text using vLLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/vllm.py
Dottxt
Bases: Model
Thin wrapper around the dottxt.client.Dottxt client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the dottxt.client.Dottxt client.
Source code in outlines/models/dottxt.py
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 | |
__init__(client, model_name=None, model_revision=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Dottxt
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
model_revision
|
Optional[str]
|
The revision of the model to use. |
None
|
Source code in outlines/models/dottxt.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using Dottxt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/dottxt.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Not available for Dottxt.
Source code in outlines/models/dottxt.py
Gemini
Bases: Model
Thin wrapper around the google.genai.Client client.
This wrapper is used to convert the input and output types specified by
the users at a higher level to arguments to the google.genai.Client
client.
Source code in outlines/models/gemini.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/gemini.py
generate(model_input, output_type=None, **inference_kwargs)
Generate a response from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema, a list of such types, or a multiple choice type. |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The response generated by the model. |
Source code in outlines/models/gemini.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Generate a stream of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema, a list of such types, or a multiple choice type. |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/gemini.py
LMStudio
Bases: Model
Thin wrapper around a lmstudio.Client client.
This wrapper is used to convert the input and output types specified by the users at a higher level to arguments to the LMStudio client.
Source code in outlines/models/lmstudio.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 203 204 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 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
A LMStudio Client instance obtained via |
required |
model_name
|
Optional[str]
|
The name of the model to use. If not provided, uses the default loaded model in LMStudio. |
None
|
Source code in outlines/models/lmstudio.py
generate(model_input, output_type=None, **kwargs)
Generate text using LMStudio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/lmstudio.py
generate_stream(model_input, output_type=None, **kwargs)
Stream text using LMStudio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/lmstudio.py
LlamaCpp
Bases: Model
Thin wrapper around the llama_cpp.Llama model.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the llama_cpp.Llama model.
Source code in outlines/models/llamacpp.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
__init__(model, chat_mode=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Llama
|
A |
required |
chat_mode
|
bool
|
Whether to enable chat mode. If |
True
|
Source code in outlines/models/llamacpp.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using llama-cpp-python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/llamacpp.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using llama-cpp-python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/llamacpp.py
MLXLM
Bases: Model
Thin wrapper around an mlx_lm model.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the mlx_lm library.
Source code in outlines/models/mlxlm.py
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 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 | |
__init__(model, tokenizer)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
An instance of an |
required |
tokenizer
|
PreTrainedTokenizer
|
An instance of an |
required |
Source code in outlines/models/mlxlm.py
generate(model_input, output_type=None, **kwargs)
Generate text using mlx-lm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
kwargs
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/mlxlm.py
generate_batch(model_input, output_type=None, **kwargs)
Generate a batch of text using mlx-lm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
list[str]
|
The list of prompts based on which the model will generate a response. |
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
kwargs
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The list of text generated by the model. |
Source code in outlines/models/mlxlm.py
generate_stream(model_input, output_type=None, **kwargs)
Stream text using mlx-lm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
kwargs
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/mlxlm.py
Mistral
Bases: Model
Thin wrapper around the mistralai.Mistral client.
Converts input and output types to arguments for the mistralai.Mistral
client's chat.complete or chat.stream methods.
Source code in outlines/models/mistral.py
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mistral
|
A mistralai.Mistral client instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/mistral.py
generate(model_input, output_type=None, **inference_kwargs)
Generate a response from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt or chat messages to generate a response from. |
required |
output_type
|
Optional[Any]
|
The desired format of the response (e.g., JSON schema). |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The response generated by the model as text. |
Source code in outlines/models/mistral.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Generate a stream of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt or chat messages to generate a response from. |
required |
output_type
|
Optional[Any]
|
The desired format of the response (e.g., JSON schema). |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text chunks generated by the model. |
Source code in outlines/models/mistral.py
Model
Bases: ABC
Base class for all synchronous models.
This class defines shared __call__, batch and stream methods that can
be used to call the model directly. The generate, generate_batch, and
generate_stream methods must be implemented by the subclasses.
All models inheriting from this class must define a type_adapter
attribute of type ModelTypeAdapter. The methods of the type_adapter
attribute are used in the generate, generate_batch, and
generate_stream methods to format the input and output types received by
the model.
Additionally, steerable models must define a tensor_library_name
attribute.
Source code in outlines/models/base.py
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 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
__call__(model_input, output_type=None, backend=None, **inference_kwargs)
Call the model.
Users can call the model directly, in which case we will create a generator instance with the output type provided and call it. Thus, those commands are equivalent:
andParameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Any
|
The input provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
backend
|
Optional[str]
|
The name of the backend to use to create the logits processor that
will be used to generate the response. Only used for steerable
models if |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/models/base.py
batch(model_input, output_type=None, backend=None, **inference_kwargs)
Make a batch call to the model (several inputs at once).
Users can use the batch method from the model directly, in which
case we will create a generator instance with the output type provided
and then invoke its batch method.
Thus, those commands are equivalent:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
List[Any]
|
The list of inputs provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
backend
|
Optional[str]
|
The name of the backend to use to create the logits processor that
will be used to generate the response. Only used for steerable
models if |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
The list of responses generated by the model. |
Source code in outlines/models/base.py
generate(model_input, output_type=None, **inference_kwargs)
abstractmethod
Generate a response from the model.
The output_type argument contains a logits processor for steerable models while it contains a type (Json, Enum...) for black-box models. This method is not intended to be used directly by end users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Any
|
The input provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/models/base.py
generate_batch(model_input, output_type=None, **inference_kwargs)
abstractmethod
Generate a batch of responses from the model.
The output_type argument contains a logits processor for steerable models while it contains a type (Json, Enum...) for black-box models. This method is not intended to be used directly by end users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
List[Any]
|
The list of inputs provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
The list of responses generated by the model. |
Source code in outlines/models/base.py
generate_stream(model_input, output_type=None, **inference_kwargs)
abstractmethod
Generate a stream of responses from the model.
The output_type argument contains a logits processor for steerable models while it contains a type (Json, Enum...) for black-box models. This method is not intended to be used directly by end users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Any
|
The input provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[Any]
|
A stream of responses from the model. |
Source code in outlines/models/base.py
stream(model_input, output_type=None, backend=None, **inference_kwargs)
Stream a response from the model.
Users can use the stream method from the model directly, in which
case we will create a generator instance with the output type provided
and then invoke its stream method.
Thus, those commands are equivalent:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Any
|
The input provided by the user. |
required |
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
backend
|
Optional[str]
|
The name of the backend to use to create the logits processor that
will be used to generate the response. Only used for steerable
models if |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[Any]
|
A stream of responses from the model. |
Source code in outlines/models/base.py
ModelTypeAdapter
Bases: ABC
Base class for all model type adapters.
A type adapter instance must be given as a value to the type_adapter
attribute when instantiating a model.
The type adapter is responsible for formatting the input and output types
passed to the model to match the specific format expected by the
associated model.
Source code in outlines/models/base.py
format_input(model_input)
abstractmethod
Format the user input to the expected format of the model.
For API-based models, it typically means creating the messages
argument passed to the client. For local models, it can mean casting
the input from str to list for instance.
This method is also used to validate that the input type provided by
the user is supported by the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Any
|
The input provided by the user. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The formatted input to be passed to the model. |
Source code in outlines/models/base.py
format_output_type(output_type=None)
abstractmethod
Format the output type to the expected format of the model.
For black-box models, this typically means creating a response_format
argument. For steerable models, it means formatting the logits processor
to create the object type expected by the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_type
|
Optional[Any]
|
The output type provided by the user. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The formatted output type to be passed to the model. |
Source code in outlines/models/base.py
Ollama
Bases: Model
Thin wrapper around the ollama.Client client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the ollama.Client client.
Source code in outlines/models/ollama.py
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 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 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/ollama.py
generate(model_input, output_type=None, **kwargs)
Generate text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/ollama.py
generate_stream(model_input, output_type=None, **kwargs)
Stream text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/ollama.py
OpenAI
Bases: Model
Thin wrapper around the openai.OpenAI client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client.
Source code in outlines/models/openai.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[OpenAI, AzureOpenAI]
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/openai.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/openai.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/openai.py
SGLang
Bases: Model
Thin wrapper around the openai.OpenAI client used to communicate with
an SGLang server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client for the
SGLang server.
Source code in outlines/models/sglang.py
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 207 208 209 210 211 212 213 214 215 216 217 218 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
An |
required | |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/sglang.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using SGLang.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/sglang.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using SGLang.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/sglang.py
TGI
Bases: Model
Thin wrapper around a huggingface_hub.InferenceClient client used to
communicate with a TGI server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the
huggingface_hub.InferenceClient client.
Source code in outlines/models/tgi.py
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__(client)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
A huggingface |
required |
generate(model_input, output_type=None, **inference_kwargs)
Generate text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/tgi.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/tgi.py
TransformerTokenizer
Bases: Tokenizer
Represents a tokenizer for models in the transformers library.
Source code in outlines/models/transformers.py
Transformers
Bases: Model
Thin wrapper around a transformers model and a transformers
tokenizer.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the transformers model and
tokenizer.
Source code in outlines/models/transformers.py
199 200 201 202 203 204 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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
__init__(model, tokenizer, *, device_dtype=None)
Parameters:
model
A PreTrainedModel, or any model that is compatible with the
transformers API for models.
tokenizer
A PreTrainedTokenizer, or any tokenizer that is compatible with
the transformers API for tokenizers.
device_dtype
The dtype to use for the model. If not provided, the model will use
the default dtype.
Source code in outlines/models/transformers.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using transformers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[str, dict, Chat]
|
The prompt based on which the model will generate a response. For
multi-modal models, the input should be a dictionary containing the
|
required |
output_type
|
Optional[OutlinesLogitsProcessor]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, List[str]]
|
The text generated by the model. |
Source code in outlines/models/transformers.py
generate_batch(model_input, output_type=None, **inference_kwargs)
Source code in outlines/models/transformers.py
generate_stream(model_input, output_type, **inference_kwargs)
Not available for transformers models.
TODO: implement following completion of https://github.com/huggingface/transformers/issues/30810
Source code in outlines/models/transformers.py
TransformersMultiModal
Bases: Transformers
Thin wrapper around a transformers model and a transformers
processor.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the transformers model and
processor.
Source code in outlines/models/transformers.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
__init__(model, processor, *, device_dtype=None)
Create a TransformersMultiModal model instance
We rely on the __init__ method of the Transformers class to handle
most of the initialization and then add elements specific to multimodal
models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PreTrainedModel
|
A |
required |
processor
|
A |
required | |
device_dtype
|
Optional[dtype]
|
The dtype to use for the model. If not provided, the model will use the default dtype. |
None
|
Source code in outlines/models/transformers.py
VLLM
Bases: Model
Thin wrapper around the openai.OpenAI client used to communicate with
a vllm server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client for the
vllm server.
Source code in outlines/models/vllm.py
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 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
OpenAI
|
An |
required |
Source code in outlines/models/vllm.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using vLLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/vllm.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using vLLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/vllm.py
VLLMOffline
Bases: Model
Thin wrapper around a vllm.LLM model.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the vllm.LLM model.
Source code in outlines/models/vllm_offline.py
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 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 | |
__init__(model)
Create a VLLM model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LLM
|
A |
required |
Source code in outlines/models/vllm_offline.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using vLLM offline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
The prompt based on which the model will generate a response. |
required | |
output_type
|
Optional[Any]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, List[str]]
|
The text generated by the model. |
Source code in outlines/models/vllm_offline.py
generate_batch(model_input, output_type=None, **inference_kwargs)
Generate a batch of completions using vLLM offline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
The list of prompts based on which the model will generate a response. |
required | |
output_type
|
Optional[Any]
|
The logits processor the model will use to constrain the format of the generated text. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the |
{}
|
Returns:
| Type | Description |
|---|---|
Union[List[str], List[List[str]]]
|
The text generated by the model. |
Source code in outlines/models/vllm_offline.py
generate_stream(model_input, output_type, **inference_kwargs)
Not available for vllm.LLM.
TODO: Implement the streaming functionality ourselves.
Source code in outlines/models/vllm_offline.py
from_anthropic(client, model_name=None)
Create an Outlines Anthropic model instance from an
anthropic.Anthropic client instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Anthropic
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Anthropic
|
An Outlines |
Source code in outlines/models/anthropic.py
from_dottxt(client, model_name=None, model_revision=None)
Create an Outlines Dottxt model instance from a dottxt.Dottxt
client instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Dottxt
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
model_revision
|
Optional[str]
|
The revision of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Dottxt
|
An Outlines |
Source code in outlines/models/dottxt.py
from_gemini(client, model_name=None)
Create an Outlines Gemini model instance from a
google.genai.Client instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Gemini
|
An Outlines |
Source code in outlines/models/gemini.py
from_llamacpp(model, chat_mode=True)
Create an Outlines LlamaCpp model instance from a
llama_cpp.Llama instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Llama
|
A |
required |
chat_mode
|
bool
|
Whether to enable chat mode. If |
True
|
Returns:
| Type | Description |
|---|---|
LlamaCpp
|
An Outlines |
Source code in outlines/models/llamacpp.py
from_lmstudio(client, model_name=None)
Create an Outlines LMStudio model instance from a
lmstudio.Client or lmstudio.AsyncClient instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[Client, AsyncClient]
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Union[LMStudio, AsyncLMStudio]
|
An Outlines |
Source code in outlines/models/lmstudio.py
from_mistral(client, model_name=None, async_client=False)
Create an Outlines Mistral model instance from a mistralai.Mistral client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mistral
|
A mistralai.Mistral client instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
async_client
|
bool
|
If True, return an AsyncMistral instance; otherwise, return a Mistral instance. |
False
|
Returns:
| Type | Description |
|---|---|
Union[Mistral, AsyncMistral]
|
An Outlines Mistral or AsyncMistral model instance. |
Source code in outlines/models/mistral.py
from_mlxlm(model, tokenizer)
Create an Outlines MLXLM model instance from an mlx_lm model and a
tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
An instance of an |
required |
tokenizer
|
PreTrainedTokenizer
|
An instance of an |
required |
Returns:
| Type | Description |
|---|---|
MLXLM
|
An Outlines |
Source code in outlines/models/mlxlm.py
from_ollama(client, model_name=None)
Create an Outlines Ollama model instance from an ollama.Client
or ollama.AsyncClient instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[Client, AsyncClient]
|
A |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Union[Ollama, AsyncOllama]
|
An Outlines |
Source code in outlines/models/ollama.py
from_openai(client, model_name=None)
Create an Outlines OpenAI or AsyncOpenAI model instance from an
openai.OpenAI or openai.AsyncOpenAI client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[OpenAI, AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI]
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
OpenAI
|
An Outlines |
Source code in outlines/models/openai.py
from_sglang(client, model_name=None)
Create a SGLang or AsyncSGLang instance from an openai.OpenAI or
openai.AsyncOpenAI instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[OpenAI, AsyncOpenAI]
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Union[SGLang, AsyncSGLang]
|
An Outlines |
Source code in outlines/models/sglang.py
from_tgi(client)
Create an Outlines TGI or AsyncTGI model instance from an
huggingface_hub.InferenceClient or huggingface_hub.AsyncInferenceClient
instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[InferenceClient, AsyncInferenceClient]
|
An |
required |
Returns:
| Type | Description |
|---|---|
Union[TGI, AsyncTGI]
|
An Outlines |
Source code in outlines/models/tgi.py
from_transformers(model, tokenizer_or_processor, *, device_dtype=None)
Create an Outlines Transformers or TransformersMultiModal model
instance from a PreTrainedModel instance and a PreTrainedTokenizer or
ProcessorMixin instance.
outlines supports PreTrainedModelForCausalLM,
PreTrainedMambaForCausalLM, PreTrainedModelForSeq2Seq and any model
that implements the transformers model API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PreTrainedModel
|
A |
required |
tokenizer_or_processor
|
Union[PreTrainedTokenizer, ProcessorMixin]
|
A |
required |
device_dtype
|
Optional[dtype]
|
The dtype to use for the model. If not provided, the model will use the default dtype. |
None
|
Returns:
| Type | Description |
|---|---|
Union[Transformers, TransformersMultiModal]
|
An Outlines |
Source code in outlines/models/transformers.py
from_vllm(client, model_name=None)
Create an Outlines VLLM or AsyncVLLM model instance from an
openai.OpenAI or openai.AsyncOpenAI instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[OpenAI, AsyncOpenAI]
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Returns:
| Type | Description |
|---|---|
Union[VLLM, AsyncVLLM]
|
An Outlines |
Source code in outlines/models/vllm.py
from_vllm_offline(model)
Create an Outlines VLLMOffline model instance from a vllm.LLM
instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LLM
|
A |
required |
Returns:
| Type | Description |
|---|---|
VLLMOffline
|
An Outlines |
Source code in outlines/models/vllm_offline.py
applications
Encapsulate a prompt template and an output type into a reusable object.
Application
Application is a class that encapsulates a prompt template and an output type. It can be called to generate a response by providing a model, the values to be substituted in the template in a dictionary and optional inference parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
Union[Template, Callable]
|
A callable that takes arguments and returns a prompt string. |
required |
output_type
|
Any
|
The expected output type of the generated response. |
None
|
Examples:
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from outlines import models, Application
from outlines.types import JsonType
from outlines.templates import Template
class OutputModel(BaseModel):
result: int
model = models.from_transformers(
AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"),
AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
)
template_string = "What is 2 times {{ num }}?"
template = Template.from_string(template_string)
application = Application(template, JsonType(OutputModel))
result = application(model, {"num": 3}, max_new_tokens=20)
print(result) # Expected output: { "result" : 6 }
Source code in outlines/applications.py
__call__(model, template_vars, **inference_kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model to use to generate the response. |
required |
template_vars
|
Dict[str, Any]
|
The variables to be substituted in the template. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The generated response. |
Source code in outlines/applications.py
__init__(template, output_type=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
Union[Template, Callable]
|
The template to use to build the prompt. |
required |
output_type
|
Optional[Any]
|
The output type provided to the generator. |
None
|
Source code in outlines/applications.py
backends
Module to define the backends in charge of creating logits processors.
BaseBackend
Bases: ABC
Base class for all backends.
The subclasses must implement methods that create a logits processor from a JSON schema, regex or CFG.
Source code in outlines/backends/base.py
get_cfg_logits_processor(grammar)
abstractmethod
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
get_json_schema_logits_processor(json_schema)
abstractmethod
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
get_regex_logits_processor(regex)
abstractmethod
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
LLGuidanceBackend
Bases: BaseBackend
Backend for LLGuidance.
Source code in outlines/backends/llguidance.py
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 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/llguidance.py
get_cfg_logits_processor(grammar)
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
OutlinesCoreBackend
Bases: BaseBackend
Backend for Outlines Core.
Source code in outlines/backends/outlines_core.py
196 197 198 199 200 201 202 203 204 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/outlines_core.py
create_outlines_core_vocabulary(vocab, eos_token_id, eos_token, token_to_str)
staticmethod
Create an Outlines Core Vocabulary instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab
|
Dict[str, int]
|
The vocabulary to create an Outlines Core vocabulary from. |
required |
eos_token_id
|
int
|
The EOS token ID. |
required |
eos_token
|
str
|
The EOS token. |
required |
token_to_str
|
Callable[[str], str]
|
The function to convert a token to a string. |
required |
Returns:
| Type | Description |
|---|---|
Vocabulary
|
The Outlines Core Vocabulary instance. |
Source code in outlines/backends/outlines_core.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/outlines_core.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/outlines_core.py
XGrammarBackend
Bases: BaseBackend
Backend for XGrammar.
Source code in outlines/backends/xgrammar.py
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 207 208 209 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/xgrammar.py
get_cfg_logits_processor(grammar)
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_cfg_logits_processor(backend_name, model, grammar)
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend_name
|
str | None
|
The name of the backend to use. |
required |
model
|
SteerableModel
|
The Outlines model of the user. |
required |
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/__init__.py
get_json_schema_logits_processor(backend_name, model, json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend_name
|
str | None
|
The name of the backend to use. |
required |
model
|
SteerableModel
|
The Outlines model of the user. |
required |
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/__init__.py
get_regex_logits_processor(backend_name, model, regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend_name
|
str | None
|
The name of the backend to use. |
required |
model
|
SteerableModel
|
The Outlines model of the user. |
required |
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/__init__.py
base
Base class for all backends.
BaseBackend
Bases: ABC
Base class for all backends.
The subclasses must implement methods that create a logits processor from a JSON schema, regex or CFG.
Source code in outlines/backends/base.py
get_cfg_logits_processor(grammar)
abstractmethod
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
get_json_schema_logits_processor(json_schema)
abstractmethod
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
get_regex_logits_processor(regex)
abstractmethod
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessorType
|
The logits processor. |
Source code in outlines/backends/base.py
llguidance
Backend class for LLGuidance.
LLGuidanceBackend
Bases: BaseBackend
Backend for LLGuidance.
Source code in outlines/backends/llguidance.py
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 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/llguidance.py
get_cfg_logits_processor(grammar)
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/llguidance.py
LLGuidanceLogitsProcessor
Bases: OutlinesLogitsProcessor
Logits Processor for the LLGuidance backend.
Source code in outlines/backends/llguidance.py
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 | |
__init__(grammar, llg_tokenizer, tensor_library_name)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The grammar spec to use to create the LLMatcher |
required |
llg_tokenizer
|
The LLGuidance tokenizer |
required | |
tensor_library_name
|
str
|
The name of the tensor library used by the model |
required |
Source code in outlines/backends/llguidance.py
process_logits(input_ids, logits)
Use the instances of LLMatcher to bias the logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences. |
required |
logits
|
TensorType
|
The logits for the current generation step. |
required |
Returns:
| Type | Description |
|---|---|
TensorType
|
The biased logits. |
Source code in outlines/backends/llguidance.py
outlines_core
Backend class for Outlines Core.
OutlinesCoreBackend
Bases: BaseBackend
Backend for Outlines Core.
Source code in outlines/backends/outlines_core.py
196 197 198 199 200 201 202 203 204 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/outlines_core.py
create_outlines_core_vocabulary(vocab, eos_token_id, eos_token, token_to_str)
staticmethod
Create an Outlines Core Vocabulary instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab
|
Dict[str, int]
|
The vocabulary to create an Outlines Core vocabulary from. |
required |
eos_token_id
|
int
|
The EOS token ID. |
required |
eos_token
|
str
|
The EOS token. |
required |
token_to_str
|
Callable[[str], str]
|
The function to convert a token to a string. |
required |
Returns:
| Type | Description |
|---|---|
Vocabulary
|
The Outlines Core Vocabulary instance. |
Source code in outlines/backends/outlines_core.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/outlines_core.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/outlines_core.py
OutlinesCoreLogitsProcessor
Bases: OutlinesLogitsProcessor
Logits processor for Outlines Core.
Source code in outlines/backends/outlines_core.py
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 | |
__init__(index, tensor_library_name)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Index
|
The Outlines Core |
required |
tensor_library_name
|
str
|
The tensor library name to use for the logits processor. |
required |
Source code in outlines/backends/outlines_core.py
process_logits(input_ids, logits)
Use the guides to bias the logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences. |
required |
logits
|
TensorType
|
The logits for the current generation step. |
required |
Returns:
| Type | Description |
|---|---|
TensorType
|
The biased logits. |
Source code in outlines/backends/outlines_core.py
xgrammar
Backend class for XGrammar.
XGrammarBackend
Bases: BaseBackend
Backend for XGrammar.
Source code in outlines/backends/xgrammar.py
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 207 208 209 | |
__init__(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/xgrammar.py
get_cfg_logits_processor(grammar)
Create a logits processor from a context-free grammar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
XGrammarLogitsProcessor
Bases: OutlinesLogitsProcessor
Logits processor for XGrammar.
Source code in outlines/backends/xgrammar.py
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 | |
__init__(compiled_grammar, tensor_library_name)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiled_grammar
|
str
|
The compiled grammar to use to create the logits processor. |
required |
tensor_library_name
|
str
|
The name of the tensor library used by the model |
required |
Source code in outlines/backends/xgrammar.py
process_logits(input_ids, logits)
Use the XGrammar matchers to bias the logits.
Source code in outlines/backends/xgrammar.py
caching
Caching and memoization of function calls.
cache(expire=None, typed=False, ignore=())
Caching decorator for memoizing function calls.
The cache key is created based on the values returned by the key_function callable if provided or based on the arguments of the decorated function directly otherwise
This is based on diskcache's memoize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expire
|
Optional[float]
|
Seconds until arguments expire. |
None
|
typed
|
Cache different types separately. |
False
|
|
ignore
|
Positional or keyword arguments to ignore. |
()
|
Returns:
| Type | Description |
|---|---|
A decorator function that can be applied to other functions.
|
|
Source code in outlines/caching.py
clear_cache()
disable_cache()
Disable the cache for this session.
Generative models output different results each time they are called when
sampling. This can be a desirable property for some workflows, in which case
one can call outlines.call.disable to disable the cache for the session.
This function does not delete the cache, call outlines.cache.clear
instead. It also does not overwrite the cache with the values returned
during the session.
Example
outlines.cache.disable should be called right after importing outlines:
import outlines.caching as cache cache.disable_cache()
Source code in outlines/caching.py
get_cache()
cached
Get the context object that contains previously-computed return values.
The cache is used to avoid unnecessary computations and API calls, which can be long and expensive for large models.
The cache directory defaults to HOMEDIR/.cache/outlines, but this choice
can be overridden by the user by setting the value of the OUTLINES_CACHE_DIR
environment variable.
Source code in outlines/caching.py
generator
Encapsulate a model and an output type into a reusable object.
AsyncBlackBoxGenerator
Asynchronous generator for which we don't control constrained generation.
The output type provided is not compiled into a logits processor, but is instead directly passed on to the model.
Source code in outlines/generator.py
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 | |
__call__(prompt, **inference_kwargs)
async
Generate a response from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
__init__(model, output_type)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
AsyncBlackBoxModel
|
An instance of an Outlines model. |
required |
output_type
|
Optional[Any]
|
The output type that will be used to constrain the generation. |
required |
Source code in outlines/generator.py
batch(prompts, **inference_kwargs)
async
Generate a batch of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
List[Any]
|
The list of prompts to use to generate a batch of responses. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
The list of responses generated by the model. |
Source code in outlines/generator.py
stream(prompt, **inference_kwargs)
async
Generate a stream of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
BlackBoxGenerator
Synchronous generator for which we don't control constrained generation.
The output type provided is not compiled into a logits processor, but is instead directly passed on to the model.
Source code in outlines/generator.py
__call__(prompt, **inference_kwargs)
Generate a response from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
__init__(model, output_type)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BlackBoxModel
|
An instance of an Outlines model. |
required |
output_type
|
Optional[Any]
|
The output type that will be used to constrain the generation. |
required |
Source code in outlines/generator.py
batch(prompts, **inference_kwargs)
Generate a batch of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
List[Any]
|
The list of prompts to use to generate a batch of responses. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
The list of responses generated by the model. |
Source code in outlines/generator.py
stream(prompt, **inference_kwargs)
Generate a stream of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
SteerableGenerator
Represents a generator for which we control constrained generation.
The generator is responsible for building and storing the logits processor (which can be quite expensive to build), and then passing it to the model when the generator is called.
The argument defining constrained generation can be of 2 types associated
to different methods to create an instance of the generator:
- output_type (through __init__): an output type as defined in the
outlines.types module
- processor (through from_processor): an already built logits processor
as defined in the outlines.processors module
The 2 parameters are mutually exclusive.
Source code in outlines/generator.py
197 198 199 200 201 202 203 204 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 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 335 336 337 338 339 340 341 342 343 | |
__call__(prompt, **inference_kwargs)
Generate a response from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
__init__(model, output_type, backend_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
An instance of an Outlines model. |
required |
output_type
|
Optional[Any]
|
The output type expressed as a Python type |
required |
backend_name
|
Optional[str]
|
The name of the backend to use to create the logits processor. |
None
|
Source code in outlines/generator.py
batch(prompts, **inference_kwargs)
Generate a batch of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
List[Any]
|
The list of prompts to use to generate a batch of responses. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
The list of responses generated by the model. |
Source code in outlines/generator.py
from_processor(model, processor)
classmethod
Create a generator from a logits processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SteerableModel
|
An instance of an Outlines model. |
required |
processor
|
LogitsProcessorType
|
An instance of a logits processor. |
required |
Source code in outlines/generator.py
stream(prompt, **inference_kwargs)
Generate a stream of responses from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Any
|
The prompt to use to generate a response. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The response generated by the model. |
Source code in outlines/generator.py
Generator(model, output_type=None, backend=None, *, processor=None)
Create a generator for the given model and output parameters.
The 2 parameters output_type and processor are mutually exclusive. The parameters processor is only supported for SteerableModel instances (typically local models) and is intended to be only used by advanced users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Union[Model, AsyncModel]
|
An instance of an Outlines model. |
required |
output_type
|
Optional[Any]
|
The output type expressed as a Python type or a type defined in the outlines.types.dsl module. |
None
|
backend
|
Optional[str]
|
The name of the backend to use to create the logits processor. Only
used for steerable models if there is an output type and |
None
|
processor
|
Optional[LogitsProcessorType]
|
An instance of a logits processor. |
None
|
Returns:
| Type | Description |
|---|---|
Union[SteerableGenerator, BlackBoxGenerator, AsyncBlackBoxGenerator]
|
A generator instance. |
Source code in outlines/generator.py
grammars
A few common Lark grammars.
read_grammar(grammar_file_name, base_grammar_path=GRAMMAR_PATH)
Read grammar file from default grammar path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grammar_file_name
|
str
|
The name of the grammar file to read. |
required |
base_grammar_path
|
Path
|
The path to the directory containing the grammar file. |
GRAMMAR_PATH
|
Returns:
| Type | Description |
|---|---|
str
|
The contents of the grammar file. |
Source code in outlines/grammars.py
inputs
Contain classes used to define the inputs of a model.
Audio
dataclass
Contains an audio that can be passed to a multimodal model.
Provide one or several instances of this class along with a text prompt
in a list as the model_input argument to a model that supports audio
processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Any
|
The audio to use in the text generation. |
required |
Source code in outlines/inputs.py
Chat
dataclass
Contains the input for a chat model.
Provide an instance of this class as the model_input argument to a model
that supports chat.
Each message contained in the messages list must be a dict with 'role' and 'content' keys. The role can be 'user', 'assistant', or 'system'. The content supports either: - a text string, - a list containing text and assets (e.g., ["Describe...", Image(...)]), - only for HuggingFace transformers models, a list of dict items with explicit types (e.g., [{"type": "text", "text": "Describe..."}, {"type": "image", "image": Image(...)}])
Examples:
# Initialize the chat with a system message.
chat_prompt = Chat([
{"role": "system", "content": "You are a helpful assistant."},
])
# Add a user message with an image and call the model (not shown here).
chat_prompt.add_user_message(["Describe the image below", Image(image)])
# Add as an assistant message the response from the model.
chat_prompt.add_assistant_message("There is a black cat sitting on a couch.")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List[Dict[str, Any]]
|
The list of messages that will be provided to the model. |
None
|
Source code in outlines/inputs.py
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 | |
add_assistant_message(content)
Add an assistant message to the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str | List[Any]
|
The content of the assistant message. |
required |
Source code in outlines/inputs.py
add_system_message(content)
Add a system message to the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str | List[Any]
|
The content of the system message. |
required |
add_user_message(content)
Add a user message to the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str | List[Any]
|
The content of the user message. |
required |
append(message)
Add a message to the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Dict[str, Any]
|
The message to add to the chat. |
required |
extend(messages)
Add a list of messages to the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List[Dict[str, Any]]
|
The list of messages to add to the chat. |
required |
pop()
Remove the last message from the chat.
Returns:
| Type | Description |
|---|---|
message
|
The removed message. |
Image
dataclass
Contains an image that can be passed to a multimodal model.
Provide one or several instances of this class along with a text prompt
in a list as the model_input argument to a model that supports vision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
The image to use in the text generation. |
required |
Source code in outlines/inputs.py
Video
dataclass
Contains a video that can be passed to a multimodal model.
Provide one or several instances of this class along with a text prompt
in a list as the model_input argument to a model that supports video
processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Any
|
The video to use in the text generation. |
required |
Source code in outlines/inputs.py
models
Module that contains all the models integrated in outlines.
We group the models in submodules by provider instead of theme (completion, chat completion, diffusers, etc.) and use routing functions everywhere else in the codebase.
Anthropic
Bases: Model
Thin wrapper around the anthropic.Anthropic client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the anthropic.Anthropic client.
Source code in outlines/models/anthropic.py
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 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 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Anthropic
|
An |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/anthropic.py
generate(model_input, output_type=None, **inference_kwargs)
Generate text using Anthropic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
As structured generation is not supported by Anthropic, the value
of this argument must be |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The response generated by the model. |
Source code in outlines/models/anthropic.py
generate_stream(model_input, output_type=None, **inference_kwargs)
Stream text using Anthropic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
As structured generation is not supported by Anthropic, the value
of this argument must be |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/anthropic.py
AsyncLMStudio
Bases: AsyncModel
Thin wrapper around a lmstudio.AsyncClient client.
This wrapper is used to convert the input and output types specified by the users at a higher level to arguments to the LMStudio async client.
Source code in outlines/models/lmstudio.py
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient
|
A LMStudio AsyncClient instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. If not provided, uses the default loaded model in LMStudio. |
None
|
Source code in outlines/models/lmstudio.py
close()
async
generate(model_input, output_type=None, **kwargs)
async
Generate text using LMStudio asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/lmstudio.py
generate_stream(model_input, output_type=None, **kwargs)
async
Stream text using LMStudio asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/lmstudio.py
AsyncMistral
Bases: AsyncModel
Async thin wrapper around the mistralai.Mistral client.
Converts input and output types to arguments for the mistralai.Mistral
client's async methods (chat.complete_async or chat.stream_async).
Source code in outlines/models/mistral.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mistral
|
A mistralai.Mistral client instance. |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/mistral.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate a response from the model asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt or chat messages to generate a response from. |
required |
output_type
|
Optional[Any]
|
The desired format of the response (e.g., JSON schema). |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The response generated by the model as text. |
Source code in outlines/models/mistral.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Generate text from the model as an async stream of chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str, list, or chat input to generate from. |
required | |
output_type
|
Optional type for structured output. |
None
|
|
**inference_kwargs
|
Extra kwargs like "model" name. |
{}
|
Yields:
| Type | Description |
|---|---|
str
|
Chunks of text as they are streamed. |
Source code in outlines/models/mistral.py
AsyncOllama
Bases: AsyncModel
Thin wrapper around the ollama.AsyncClient client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the ollama.AsyncClient client.
Source code in outlines/models/ollama.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/ollama.py
generate(model_input, output_type=None, **kwargs)
async
Generate text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/ollama.py
generate_stream(model_input, output_type=None, **kwargs)
async
Stream text using Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Chat | str | list
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/ollama.py
AsyncOpenAI
Bases: AsyncModel
Thin wrapper around the openai.AsyncOpenAI client.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.AsyncOpenAI client.
Source code in outlines/models/openai.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Union[AsyncOpenAI, AsyncAzureOpenAI]
|
The |
required |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Source code in outlines/models/openai.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/openai.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Stream text using OpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, list, str]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Union[type[BaseModel], str]]
|
The desired format of the response generated by the model. The output type must be of a type that can be converted to a JSON schema or an empty dictionary. |
None
|
**inference_kwargs
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
An iterator that yields the text generated by the model. |
Source code in outlines/models/openai.py
AsyncSGLang
Bases: AsyncModel
Thin async wrapper around the openai.OpenAI client used to communicate
with an SGLang server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the openai.OpenAI client for the
SGLang server.
Source code in outlines/models/sglang.py
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
__init__(client, model_name=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
An |
required | |
model_name
|
Optional[str]
|
The name of the model to use. |
None
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
An |
required |
Source code in outlines/models/sglang.py
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using sglang.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, list[str]]
|
The text generated by the model. |
Source code in outlines/models/sglang.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Return a text generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
Union[Chat, str, list]
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All output types available in Outlines are supported provided your server uses a structured generation backend that supports them. |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |
Source code in outlines/models/sglang.py
AsyncTGI
Bases: AsyncModel
Thin async wrapper around a huggingface_hub.AsyncInferenceClient
client used to communicate with a TGI server.
This wrapper is used to convert the input and output types specified by the
users at a higher level to arguments to the
huggingface_hub.AsyncInferenceClient client.
Source code in outlines/models/tgi.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 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 | |
__init__(client)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
A huggingface |
required |
generate(model_input, output_type=None, **inference_kwargs)
async
Generate text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The text generated by the model. |
Source code in outlines/models/tgi.py
generate_stream(model_input, output_type=None, **inference_kwargs)
async
Stream text using TGI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_input
|
str
|
The prompt based on which the model will generate a response. |
required |
output_type
|
Optional[Any]
|
The desired format of the response generated by the model. All
output types except |
None
|
inference_kwargs
|
Any
|
Additional keyword arguments to pass to the client. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[str]
|
An async iterator that yields the text generated by the model. |