lpgcast.llm.client¶
The model client: one JSON object per request, from any OpenAI-compatible endpoint.
:meth:LLMClient.acomplete_json is the only entry point. It serves a cached
response when there is one, and otherwise calls the endpoint — asking for
strict json_schema output first and falling back to json_object when
the endpoint rejects it — then parses the reply tolerantly, re-asking with the
parse error shown when the reply is not a JSON object.
Without an API key no request is made: only cached responses answer.
Attributes¶
Mode = Literal['json_schema', 'json_object']
module-attribute
¶
OPENAI_BASE_URL = 'https://api.openai.com/v1'
module-attribute
¶
RETRY_BACKOFF_BASE_SECONDS = 0.5
module-attribute
¶
RETRY_BACKOFF_MAX_SECONDS = 8.0
module-attribute
¶
SCHEMA_INSTRUCTION = 'Reply with one JSON object and nothing else. It must conform to this JSON Schema:\n'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
ChatMessage
¶
JsonCompletion
¶
Bases: BaseModel
The parsed reply to one :meth:LLMClient.acomplete_json request.
Source code in lpgcast/llm/client.py
Attributes¶
attempts = Field(description='Requests made, counting re-asks.')
class-attribute
instance-attribute
¶
cached = Field(description='Whether that reply came from the cache.')
class-attribute
instance-attribute
¶
content = Field(description='Raw text of the reply that parsed.')
class-attribute
instance-attribute
¶
data = Field(description='The parsed JSON object.')
class-attribute
instance-attribute
¶
mode = Field(description='Response format that reply was requested in.')
class-attribute
instance-attribute
¶
usage = Field(default_factory=TokenUsage, description='Tokens over all attempts.')
class-attribute
instance-attribute
¶
LLMClient
¶
A JSON-object client over openai.AsyncOpenAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
LLMSettings | None
|
The endpoint and model. Defaults to the environment. |
None
|
cache
|
LLMCache | None
|
The response cache. Defaults to the environment's cache settings. |
None
|
http_client
|
AsyncClient | None
|
An |
None
|
Source code in lpgcast/llm/client.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 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 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 | |
Attributes¶
cache = cache if cache is not None else LLMCache()
instance-attribute
¶
settings = settings or LLMSettings()
instance-attribute
¶
usage = UsageTracker()
instance-attribute
¶
Methods:¶
__init__(settings=None, cache=None, *, http_client=None)
¶
Source code in lpgcast/llm/client.py
acomplete_json(messages, json_schema, *, schema_name='instance')
async
¶
Ask for one JSON object conforming to json_schema.
The reply is parsed, not validated: conformance to the schema is the
caller's check. A reply that is not a JSON object is re-asked up to
parse_retries times with the error shown, and given up on early
when the same syntax error repeats. A timeout is re-issued once. A truncated
or refused reply is not re-asked.
Raises:
| Type | Description |
|---|---|
LLMJsonParseError
|
no attempt produced a JSON object. |
LLMConfigurationError
|
no key or model, or the endpoint refuses the configuration. |
LLMRequestTimeoutError
|
a request timed out twice. |
LLMTruncatedError
|
the reply stopped at the output token limit. |
LLMRefusalError
|
the model refused or the provider filtered the reply. |
CacheMissError
|
a miss while the cache requires hits. |
APIError
|
any other provider failure, after SDK retries. |
Source code in lpgcast/llm/client.py
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 | |
from_settings(settings, *, http_client=None)
classmethod
¶
Build a client and its cache from top-level settings.
Source code in lpgcast/llm/client.py
LLMConfigurationError
¶
Bases: RuntimeError
The deployment, not the request, is at fault: no key, no model, a bad key, an unknown model, or a parameter the model refuses. Every other request would fail the same way, so callers should stop rather than skip.
Source code in lpgcast/llm/client.py
LLMRefusalError
¶
Bases: LLMReplyError
The model refused, or the provider filtered the reply.
Source code in lpgcast/llm/client.py
LLMReplyError
¶
Bases: RuntimeError
A reply that arrived but cannot be used, and would not improve if re-asked.
Source code in lpgcast/llm/client.py
LLMRequestTimeoutError
¶
LLMTruncatedError
¶
Bases: LLMReplyError
The reply stopped at the output token limit (finish_reason == "length").