Skip to content

Commit

Permalink
Merge branch 'main' into blog-post
Browse files Browse the repository at this point in the history
  • Loading branch information
willbakst authored Jan 14, 2025
2 parents d9bd4d1 + a4c6e85 commit a1e913e
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 227 deletions.
20 changes: 9 additions & 11 deletions docs/learn/prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,21 @@ Since Mirascope prompt templates are just formatted strings, standard Python for
```
{% endfor %}

When writing string templates, we also offer additional `list` and `lists` format specifiers for convenience around formatting lists:
When writing string templates, we also offer additional format specifiers for convenience around formatting more dynamic content:

!!! mira ""

{% for method, method_title in zip(prompt_writing_methods, prompt_writing_method_titles) %}
=== "{{ method_title }}"
{% for title, filename in [("List(s)", "lists"), ("Text(s)", "texts"), ("Part(s)", "parts")] %}
=== "{{ title }}"

{% if method == "shorthand" %}
```python hl_lines="8-9 13 16 32 38-48"
{% elif method == "messages" %}
```python hl_lines="10-11 16 19 36 42-52"
{% elif method == "string_template" %}
```python hl_lines="7 10 27 33-43"
{% if filename == "lists" %}
```python hl_lines="7 10 17-21 26-36"
{% elif filename == "texts" %}
```python hl_lines="7 10 17-21 26-32"
{% else %}
```python hl_lines="10-11 18 21 39 45-55"
```python hl_lines="8 11 18-22 27-33"
{% endif %}
--8<-- "examples/learn/prompts/format_specifiers/lists_format/{{ method }}.py"
--8<-- "examples/learn/prompts/format_specifiers/{{ filename }}.py"
```
{% endfor %}

Expand Down
36 changes: 36 additions & 0 deletions examples/learn/prompts/format_specifiers/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from mirascope.core import prompt_template


@prompt_template(
"""
Book themes:
{themes:list}
Character analysis:
{characters:lists}
"""
)
def analyze_book(themes: list[str], characters: list[list[str]]): ...


prompt = analyze_book(
themes=["redemption", "power", "friendship"],
characters=[
["Name: Frodo", "Role: Protagonist"],
["Name: Gandalf", "Role: Mentor"],
],
)

print(prompt[0].content)
# Output:
# Book themes:
# redemption
# power
# friendship

# Character analysis:
# Name: Frodo
# Role: Protagonist

# Name: Gandalf
# Role: Mentor

This file was deleted.

52 changes: 0 additions & 52 deletions examples/learn/prompts/format_specifiers/lists_format/messages.py

This file was deleted.

48 changes: 0 additions & 48 deletions examples/learn/prompts/format_specifiers/lists_format/shorthand.py

This file was deleted.

This file was deleted.

33 changes: 33 additions & 0 deletions examples/learn/prompts/format_specifiers/parts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from mirascope.core import prompt_template
from mirascope.core.base import TextPart


@prompt_template(
"""
Book themes:
{themes:text}
Character analysis:
{characters:texts}
"""
)
def analyze_book(themes: TextPart, characters: list[TextPart]): ...


prompt = analyze_book(
themes=TextPart(type="text", text="redemption, power, friendship"),
characters=[
TextPart(type="text", text="Name: Frodo, Role: Protagonist"),
TextPart(type="text", text="Name: Gandalf, Role: Mentor"),
],
)

print(prompt[0].content)
# Output:
# [
# TextPart(type="text", text="Book themes:"),
# TextPart(type="text", text="redemption, power, friendship"),
# TextPart(type="text", text="Character analysis:"),
# TextPart(type="text", text="Name: Frodo, Role: Protagonist"),
# TextPart(type="text", text="Name: Gandalf, Role: Mentor"),
# ]
32 changes: 32 additions & 0 deletions examples/learn/prompts/format_specifiers/texts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from mirascope.core import prompt_template


@prompt_template(
"""
Book themes:
{themes:text}
Character analysis:
{characters:texts}
"""
)
def analyze_book(themes: str, characters: list[str]): ...


prompt = analyze_book(
themes="redemption, power, friendship",
characters=[
"Name: Frodo, Role: Protagonist",
"Name: Gandalf, Role: Mentor",
],
)

print(prompt[0].content)
# Output:
# [
# TextPart(type="text", text="Book themes:"),
# TextPart(type="text", text="redemption, power, friendship"),
# TextPart(type="text", text="Character analysis:"),
# TextPart(type="text", text="Name: Frodo, Role: Protagonist"),
# TextPart(type="text", text="Name: Gandalf, Role: Mentor"),
# ]
Loading

0 comments on commit a1e913e

Please sign in to comment.