structure
Class info¶
Classes¶
Name | Children | Inherits |
---|---|---|
Agent llmling_agent.agent.agent Agent for AI-powered interaction with LLMling resources and tools. |
🛈 DocStrings¶
High-level pipeline functions for agent execution.
get_structured
async
¶
get_structured(
prompt: str,
response_type: type[T],
model: ModelType,
*,
system_prompt: str | None = None,
max_retries: int = 3,
error_handler: Callable[[Exception], T | None] | None = None,
) -> T
Get structured output from LLM using function calling.
This function creates a temporary agent that uses the class constructor as a tool to generate structured output. It handles: - Type conversion from Python types to JSON schema - Constructor parameter validation - Error handling with optional recovery
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt to send to the LLM |
required |
response_type
|
type[T]
|
The type to create (class with typed constructor) |
required |
model
|
ModelType
|
model to use |
required |
system_prompt
|
str | None
|
Optional system instructions |
None
|
max_retries
|
int
|
Max attempts for parsing (default: 3) |
3
|
error_handler
|
Callable[[Exception], T | None] | None
|
Optional error handler for recovery |
None
|
Returns:
Type | Description |
---|---|
T
|
Instance of response_type |
Example
class TaskResult:
'''Analysis result for a task.'''
def __init__(
self,
success: bool,
message: str,
due_date: datetime | None = None
):
self.success = success
self.message = message
self.due_date = due_date
result = await get_structured(
"Analyze task: Deploy monitoring",
TaskResult,
system_prompt="You analyze task success"
)
print(f"Success: {result.success}")
Raises:
Type | Description |
---|---|
TypeError
|
If response_type is not a valid type |
ValueError
|
If constructor schema cannot be created |
Exception
|
If LLM call fails and no error_handler recovers |
Source code in src/llmling_agent/functional/structure.py
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 |
|
get_structured_multiple
async
¶
Extract multiple structured instances from text.
Source code in src/llmling_agent/functional/structure.py
91 92 93 94 95 96 97 98 |
|
pick_one
async
¶
Pick one option from a list of choices.
Source code in src/llmling_agent/functional/structure.py
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 |
|