mirascope.core.base.prompt¶
The BasePrompt
class for better prompt engineering.
BasePrompt
¶
Bases: BaseModel
The base class for engineering prompts.
This class is implemented as the base for all prompting needs. It is intended to work across various providers by providing a common prompt interface.
Example:
from mirascope.core import BasePrompt, metadata, prompt_template
@prompt_template("Recommend a {genre} book")
@metadata({"tags": {"version:0001", "books"}})
class BookRecommendationPrompt(BasePrompt):
genre: str
prompt = BookRecommendationPrompt(genre="fantasy")
print(prompt)
# > Recommend a fantasy book
print(prompt.message_params())
# > [BaseMessageParam(role="user", content="Recommend a fantasy book")]
print(prompt.dump()["metadata"])
# > {"metadata": {"version:0001", "books"}}
message_params
¶
message_params() -> list[BaseMessageParam]
Returns the list of parsed message parameters.
Source code in mirascope/core/base/prompt.py
dynamic_config
¶
dynamic_config() -> BaseDynamicConfig
dump
¶
Dumps the contents of the prompt into a dictionary.
Source code in mirascope/core/base/prompt.py
run
¶
run(
call_decorator: (
Callable[
[Callable[..., BaseDynamicConfig]],
Callable[..., _BaseCallResponseT],
]
| Callable[
[Callable[..., BaseDynamicConfig]],
Callable[..., _BaseStreamT],
]
| Callable[
[Callable[..., BaseDynamicConfig]],
Callable[..., _ResponseModelT],
]
| Callable[
[Callable[..., BaseDynamicConfig]],
Callable[..., Iterable[_ResponseModelT]],
]
),
*additional_decorators: Callable[[_T], _T]
) -> (
_BaseCallResponseT
| _BaseStreamT
| _ResponseModelT
| Iterable[_ResponseModelT]
)
Returns the response of calling the API of the provided decorator.
Example:
from mirascope.core import BasePrompt, openai, prompt_template
@prompt_template("Recommend a {genre} book")
class BookRecommendationPrompt(BasePrompt):
genre: str
prompt = BookRecommendationPrompt(genre="fantasy")
response = prompt.run(openai.call("gpt-4o-mini"))
print(response.content)
Source code in mirascope/core/base/prompt.py
run_async
¶
run_async(
call_decorator: (
Callable[
[Callable[..., Awaitable[BaseDynamicConfig]]],
Callable[..., Awaitable[_BaseCallResponseT]],
]
| Callable[
[Callable[..., Awaitable[BaseDynamicConfig]]],
Callable[..., Awaitable[_BaseStreamT]],
]
| Callable[
[Callable[..., Awaitable[BaseDynamicConfig]]],
Callable[..., Awaitable[_ResponseModelT]],
]
| Callable[
[Callable[..., Awaitable[BaseDynamicConfig]]],
Callable[
...,
Awaitable[AsyncIterable[_ResponseModelT]],
],
]
),
*additional_decorators: Callable[[_T], _T]
) -> (
Awaitable[_BaseCallResponseT]
| Awaitable[_BaseStreamT]
| Awaitable[_ResponseModelT]
| Awaitable[AsyncIterable[_ResponseModelT]]
)
Returns the response of calling the API of the provided decorator.
Example:
import asyncio
from mirascope.core import BasePrompt, openai, prompt_template
@prompt_template("Recommend a {genre} book")
class BookRecommendationPrompt(BasePrompt):
genre: str
async def run():
prompt = BookRecommendationPrompt(genre="fantasy")
response = await prompt.run_async(openai.call("gpt-4o-mini"))
print(response.content)
asyncio.run(run())
Source code in mirascope/core/base/prompt.py
prompt_template
¶
prompt_template(
template: str | None = None,
) -> PromptDecorator | MessagesDecorator
A decorator for setting the prompt_template
of a BasePrompt
or call
.
Usage Documentation
Example:
from mirascope.core import openai, prompt_template
@prompt_template()
def recommend_book(genre: str) -> str:
return f"Recommend a {genre} book"
print(recommend_book("fantasy"))
# Output: [BaseMessageParam(role='user', content='Recommend a fantasy book')]
Returns:
Name | Type | Description |
---|---|---|
decorator |
Callable
|
The decorator function that turns the decorated function into a prompt template. |
Source code in mirascope/core/base/prompt.py
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 |
|
metadata
¶
metadata(metadata: Metadata) -> MetadataDecorator
A decorator for adding metadata to a BasePrompt
or call
.
Adding this decorator to a BasePrompt
or call
updates the metadata
annotation
to the given value. This is useful for adding metadata to a BasePrompt
or call
that can be used for logging or filtering.
Example:
from mirascope.core import metadata, openai, prompt_template
@openai.call("gpt-4o-mini")
@prompt_template("Recommend a {genre} book")
@metadata({"tags": {"version:0001", "books"}})
def recommend_book(genre: str):
...
response = recommend_book("fantasy")
print(response.metadata)
Returns:
Name | Type | Description |
---|---|---|
decorator |
Callable
|
The decorator function that updates the |