Skip to content

Commit cd7f027

Browse files
CopilotVinciGit00
andcommitted
Add generate_structure functionality for LLM prompt templates
Co-authored-by: VinciGit00 <[email protected]>
1 parent 0c2c400 commit cd7f027

File tree

5 files changed

+1027
-0
lines changed

5 files changed

+1027
-0
lines changed

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,79 @@ assert all(isinstance(u, User) for u in decoded_users)
130130

131131
See [examples/pydantic_usage.py](examples/pydantic_usage.py) for more examples.
132132

133+
### Response Structure Templates for LLM Prompts
134+
135+
TOON provides a powerful feature to generate response structure templates that can be included in LLM prompts. This tells the model exactly what format to return data in, without needing to provide examples with actual data.
136+
137+
```python
138+
from toon import generate_structure
139+
140+
# Define the expected response structure
141+
schema = {
142+
"name": "name of the person",
143+
"age": "age of the person",
144+
"occupation": "job description of the person"
145+
}
146+
147+
# Generate the structure template
148+
structure = generate_structure(schema)
149+
print(structure)
150+
# Output:
151+
# name: <name of the person>
152+
# age: <age of the person>
153+
# occupation: <job description of the person>
154+
155+
# Use in your LLM prompt
156+
prompt = f"""Extract person information from the text and return it in this format:
157+
{structure}
158+
159+
Text: [your text here...]"""
160+
```
161+
162+
**For arrays and complex structures:**
163+
164+
```python
165+
schema = {
166+
"products": [{
167+
"name": "product name",
168+
"price": "price in USD",
169+
"rating": "rating from 1-5"
170+
}]
171+
}
172+
173+
structure = generate_structure(schema)
174+
print(structure)
175+
# Output:
176+
# products[N]{name,price,rating}:
177+
# <product name>,<price in USD>,<rating from 1-5>
178+
# ...
179+
```
180+
181+
**With Pydantic models:**
182+
183+
```python
184+
from pydantic import BaseModel, Field
185+
from toon import generate_structure_from_pydantic
186+
187+
class Product(BaseModel):
188+
name: str = Field(description="product name")
189+
price: float = Field(description="price in USD")
190+
in_stock: bool = Field(description="availability status")
191+
192+
# Generate structure from model
193+
structure = generate_structure_from_pydantic(Product)
194+
# Use in LLM prompts without providing examples
195+
```
196+
197+
**Benefits:**
198+
- ✅ No need to include example data in prompts (saves tokens)
199+
- ✅ Clear, unambiguous format specification
200+
- ✅ Works with nested objects and arrays
201+
- ✅ Supports custom delimiters
202+
- ✅ Type-safe with Pydantic models
203+
204+
See [examples/structure_template_usage.py](examples/structure_template_usage.py) for comprehensive examples.
205+
133206
## TOON Format Specification
134207

135208
### Basic Syntax
@@ -282,6 +355,73 @@ toon = "id: 1\nname: Alice"
282355
user = decode_to_pydantic(toon, User)
283356
```
284357

358+
### `generate_structure(schema, options=None)`
359+
360+
Generate a TOON structure template from a schema definition for use in LLM prompts.
361+
362+
**Parameters:**
363+
- `schema`: Schema definition as dict or list
364+
- Simple fields: `{"field_name": "description"}`
365+
- Nested objects: `{"field": {"nested": "description"}}`
366+
- Arrays: `{"field": [{"item_field": "description"}]}`
367+
- `options`: Optional dict with:
368+
- `delimiter`: `'comma'` (default), `'tab'`, or `'pipe'`
369+
- `indent`: Number of spaces per level (default: 2)
370+
371+
**Returns:**
372+
- TOON formatted structure template string
373+
374+
**Example:**
375+
```python
376+
from toon import generate_structure
377+
378+
schema = {
379+
"name": "name of the person",
380+
"age": "age of the person",
381+
"occupation": "job description"
382+
}
383+
384+
structure = generate_structure(schema)
385+
print(structure)
386+
# Output:
387+
# name: <name of the person>
388+
# age: <age of the person>
389+
# occupation: <job description>
390+
391+
# Use in LLM prompt:
392+
prompt = f"Extract person info in this format:\n{structure}"
393+
```
394+
395+
### `generate_structure_from_pydantic(model_class, options=None, include_descriptions=True)`
396+
397+
Generate a TOON structure template from a Pydantic model for use in LLM prompts.
398+
399+
**Parameters:**
400+
- `model_class`: Pydantic model class (BaseModel subclass)
401+
- `options`: Same as `generate_structure()`
402+
- `include_descriptions`: If True, use field descriptions from model
403+
404+
**Returns:**
405+
- TOON formatted structure template string
406+
407+
**Example:**
408+
```python
409+
from pydantic import BaseModel, Field
410+
from toon import generate_structure_from_pydantic
411+
412+
class User(BaseModel):
413+
id: int = Field(description="user identifier")
414+
name: str = Field(description="full name")
415+
email: str = Field(description="email address")
416+
417+
structure = generate_structure_from_pydantic(User)
418+
print(structure)
419+
# Output:
420+
# id: <user identifier>
421+
# name: <full name>
422+
# email: <email address>
423+
```
424+
285425
## CLI Usage
286426

287427
```

0 commit comments

Comments
 (0)