# Chalk Python SDK Reference: Prompt Functions
source: https://docs.chalk.ai/api-docs#section-prompt-functions

Use `chalk.prompts` to define and run prompts.

## `jinja(template)`
source: https://docs.chalk.ai/api-docs#jinja

Renders a Jinja template using feature values as context. Supports a subset of Jinja features, specifically: variables and for loops. Feature FQNs referenced in the template are automatically resolved as context variables.

**Parameters**

- `template`: `str`
  The Jinja template to render. Reference features by their fully-qualified name (e.g. `User.id`).

**Examples**

```python
import chalk.functions as F
from chalk.features import features, DataFrame
@features
class User:
   id: str
   score: float
   transactions: "DataFrame[Transaction]"
   description: str = F.jinja("User {{User.id}}. Score: {{User.score}}. Transactions: {% for txn in User.transactions %}{{txn.description}},{% endfor %}")
```

## `completion(model, messages, timeout_seconds, output_structure, temperature, top_p, max_completion_tokens, max_tokens, stop, presence_penalty, frequency_penalty, logit_bias, seed, user, model_provider, base_url, api_key, num_retries)`
source: https://docs.chalk.ai/api-docs#completion

Generate LLM model completions from a list of messages.

**Parameters**

- `model`: `str`
  The name of the model, e.g. "gpt-4o".
- `messages`: `Sequence[Underscore]`
  The list of messages of the type P.Message. Each message in the array contains the following properties: role and content. The role of the message's author. Roles can be: system, user, or assistant. The contents of the message. It can be a string or a list of objects with the following properties: type and text or image_url.
- `timeout_seconds`: `float | None` = `None`
  The timeout in seconds for completion requests
- `output_structure`: `str | None` = `None`
  The object specifying the format that the model must output. Accepts a Pydantic model or a JSON schema string (see https://docs.pydantic.dev/1.10/usage/schema/).
- `temperature`: `float | Underscore | None` = `None`
  The sampling temperature to be used, between 0 and 2 inclusive. Higher values like 0.8 produce more random outputs, while lower values like 0.2 make outputs more focused and deterministic. Note: This parameter is between 0 and 1 (inclusive) for Anthropic models.
- `top_p`: `float | Underscore | None` = `None`
  The alternative to sampling with temperature. It instructs the model to consider the results of the tokens with top_p probability. For example, 0.1 means only the tokens comprising the top 10% probability mass are considered.
- `max_completion_tokens`: `int | Underscore | None` = `None`
  The upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
- `max_tokens`: `int | Underscore | None` = `None`
  The maximum number of tokens to generate in the chat completion.
- `stop`: `Sequence[str] | None` = `None`
  Custom text sequences that will cause the model to stop generating.
- `presence_penalty`: `float | Underscore | None` = `None`
  It is used to penalize new tokens based on their existence in the text so far.
- `frequency_penalty`: `float | Underscore | None` = `None`
  It is used to penalize new tokens based on their frequency in the text so far.
- `logit_bias`: `Mapping[int, float] | None` = `None`
  Used to modify the probability of specific tokens appearing in the completion.
- `seed`: `int | Underscore | None` = `None`
  If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed.
- `user`: `str | Underscore | None` = `None`
  The unique identifier representing your end-user. This parameter is specific to OpenAI and can help to monitor and detect abuse.
- `model_provider`: `str | Underscore | None` = `None`
  The model provider.
- `base_url`: `str | Underscore | None` = `None`
  The URL of the API endpoint where requests are sent.
- `api_key`: `str | Underscore | None` = `None`
  The API key to use for the completion.
- `num_retries`: `int | Underscore | None` = `None`
  The number of times to retry the API call if an APIError, TimeoutError or ServiceUnavailableError occurs.

**Examples**

```python
import chalk.prompts as P
import chalk.functions as F
from chalk.features import features, _
from pydantic import BaseModel
class EstimatedAge(BaseModel):
    age: float
@features
class User:
   id: str
   description: str
   estimated_age_response: P.PromptResponse = P.completion(
       model="gpt-4o",
       messages=[
           P.message(
               "user",
               F.jinja("Estimate the age of the user based on the description: {{User.description}}"),
           ),
       ],
       output_structure=EstimatedAge,
   )
   estimated_age: float = F.json_value(_.estimated_age_response.response, "$.age")
   image_url: str
   image_response: P.MultimodalPromptResponse = P.completion(
       model="gpt-4o",
       messages=[
           P.message(
               "user",
               [
                   {"type": "input_text", "text": "describe this image"},
                   {"type": "input_image", "image_url": _.image_url},
               ],
           ),
       ],
       max_tokens=100+2*F.length(_.description)
   )
```

## `Prompt`
source: https://docs.chalk.ai/api-docs#Prompt

Class (bases: `BaseModel`)

Chalk Prompts are bundles of a model, a list of messages, and parameters.

**Attributes**

- `model`: `str`
  The name of the model, e.g. "gpt-4o".
- `messages`: `list[Message]`
  The list of messages of the type P.Message. Each message in the array contains the following properties: role and content. The role of the message's author. Roles can be: system, user, or assistant. The contents of the message. It is required for all messages.
- `timeout_seconds`: `float | None`
  The timeout in seconds for completion requests.
- `output_structure`: `str | None`
  The object specifying the format that the model must output. Accepts a Pydantic model or a JSON schema string (see https://docs.pydantic.dev/1.10/usage/schema/).
- `temperature`: `float | None`
  The sampling temperature to be used, between 0 and 2 inclusive. Higher values like 0.8 produce more random outputs, while lower values like 0.2 make outputs more focused and deterministic. Note: This parameter is between 0 and 1 (inclusive) for Anthropic models.
- `top_p`: `float | None`
  The alternative to sampling with temperature. It instructs the model to consider the results of the tokens with top_p probability. For example, 0.1 means only the tokens comprising the top 10% probability mass are considered.
- `max_completion_tokens`: `int | None`
  The upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
- `max_tokens`: `int | None`
  The maximum number of tokens to generate in the chat completion.
- `stop`: `list[str] | None`
  Custom text sequences that will cause the model to stop generating.
- `presence_penalty`: `float | None`
  It is used to penalize new tokens based on their existence in the text so far.
- `frequency_penalty`: `float | None`
  It is used to penalize new tokens based on their frequency in the text so far.
- `logit_bias`: `Mapping[int, float] | None`
  Used to modify the probability of specific tokens appearing in the completion.
- `seed`: `int | None`
  If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed.
- `user`: `str | None`
  The unique identifier representing your end-user. This parameter is specific to OpenAI and can help to monitor and detect abuse.
- `model_provider`: `str | None`
  The model provider.
- `base_url`: `str | None`
  The URL of the API endpoint where requests are sent.
- `num_retries`: `int | None`
  The number of times to retry the API call if an APIError, TimeoutError or ServiceUnavailableError occurs.

## `MultimodalPrompt`
source: https://docs.chalk.ai/api-docs#MultimodalPrompt

Class (bases: `BaseModel`)

Chalk Prompts are bundles of a model, a list of messages, and parameters.

**Attributes**

- `model`: `str`
  The name of the model, e.g. "gpt-4o".
- `messages`: `list[MultimodalMessage]`
  The list of messages of the type P.Message. Each message in the array contains the following properties: role and content. The role of the message's author. Roles can be: system, user, or assistant. The contents of the message. It is a list of objects with the following properties: type and text or image_url.
- `timeout_seconds`: `float | None`
  The timeout in seconds for completion requests.
- `output_structure`: `str | None`
  The object specifying the format that the model must output. Accepts a Pydantic model or a JSON schema string (see https://docs.pydantic.dev/1.10/usage/schema/).
- `temperature`: `float | None`
  The sampling temperature to be used, between 0 and 2 inclusive. Higher values like 0.8 produce more random outputs, while lower values like 0.2 make outputs more focused and deterministic. Note: This parameter is between 0 and 1 (inclusive) for Anthropic models.
- `top_p`: `float | None`
  The alternative to sampling with temperature. It instructs the model to consider the results of the tokens with top_p probability. For example, 0.1 means only the tokens comprising the top 10% probability mass are considered.
- `max_completion_tokens`: `int | None`
  The upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
- `max_tokens`: `int | None`
  The maximum number of tokens to generate in the chat completion.
- `stop`: `list[str] | None`
  Custom text sequences that will cause the model to stop generating.
- `presence_penalty`: `float | None`
  It is used to penalize new tokens based on their existence in the text so far.
- `frequency_penalty`: `float | None`
  It is used to penalize new tokens based on their frequency in the text so far.
- `logit_bias`: `Mapping[int, float] | None`
  Used to modify the probability of specific tokens appearing in the completion.
- `seed`: `int | None`
  If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed.
- `user`: `str | None`
  The unique identifier representing your end-user. This parameter is specific to OpenAI and can help to monitor and detect abuse.
- `model_provider`: `str | None`
  The model provider.
- `base_url`: `str | None`
  The URL of the API endpoint where requests are sent.
- `num_retries`: `int | None`
  The number of times to retry the API call if an APIError, TimeoutError or ServiceUnavailableError occurs.

## `Usage`
source: https://docs.chalk.ai/api-docs#Usage

Class (bases: `BaseModel`)

Usage statistics for the response.

**Attributes**

- `input_tokens`: `int`
  Number of tokens in the request.
- `output_tokens`: `int`
  Number of tokens in the response.
- `total_tokens`: `int`
  Total number of tokens used, equal to input_tokens + output_tokens.

## `RuntimeStats`
source: https://docs.chalk.ai/api-docs#RuntimeStats

Class (bases: `BaseModel`)

Runtime statistics for the response.

**Attributes**

- `total_latency`: `float`
  Total time in seconds to generate the response, including any retries.
- `last_try_latency`: `float | None`
  Time in seconds to generate the response in the last successful try.
- `total_retries`: `int`
  Total number of retries.
- `rate_limit_retries`: `int`
  Number of retries due to rate limiting.

## `PromptResponse`
source: https://docs.chalk.ai/api-docs#PromptResponse

Class (bases: `BaseModel`)

Response from the model.

**Attributes**

- `response`: `str | None`
  Response from the model. Raw string if no output structure specified, json encoded string otherwise. None if the response was not received or incorrectly formatted.
- `prompt`: `Prompt`
  Prompt used to generate the response.
- `usage`: `Usage`
  Usage statistics for the response.
- `runtime_stats`: `RuntimeStats`
  Runtime statistics for the response.

## `MultimodalPromptResponse`
source: https://docs.chalk.ai/api-docs#MultimodalPromptResponse

Class (bases: `BaseModel`)

Response from the model.

**Attributes**

- `response`: `str | None`
  Response from the model. Raw string if no output structure specified, json encoded string otherwise. None if the response was not received or incorrectly formatted.
- `prompt`: `MultimodalPrompt`
  Prompt used to generate the response.
- `usage`: `Usage`
  Usage statistics for the response.
- `runtime_stats`: `RuntimeStats`
  Runtime statistics for the response.
