Skip to content

Data Models

This section covers the core data models used to structure your prompts, inputs, and instances.


Prompts

PromptTemplate

A template containing a sequence of messages with placeholders for string formatting.

Bases: BaseModel

A template containing a sequence of messages, where the content can contain placeholders for string formatting.

Attributes:

Name Type Description
messages List[Message]

A list of Message objects that form the template.

Source code in openbatch/model.py
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
class PromptTemplate(BaseModel):
    """
    A template containing a sequence of messages, where the content can contain
    placeholders for string formatting.

    Attributes:
        messages (List[Message]): A list of Message objects that form the template.
    """
    messages: List[Message]

    def format(self, **kwargs) -> List[Message]:
        """
        Formats the content of each message in the template using the provided keyword arguments.

        Args:
            **kwargs: Keyword arguments used to substitute placeholders in message content.

        Returns:
            List[Message]: A new list of Message objects with formatted content.
        """
        formatted_messages = []
        for message in self.messages:
            formatted_content = message.content.format(**kwargs)
            formatted_messages.append(Message(role=message.role, content=formatted_content))
        return formatted_messages

format(**kwargs)

Formats the content of each message in the template using the provided keyword arguments.

Parameters:

Name Type Description Default
**kwargs

Keyword arguments used to substitute placeholders in message content.

{}

Returns:

Type Description
List[Message]

List[Message]: A new list of Message objects with formatted content.

Source code in openbatch/model.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def format(self, **kwargs) -> List[Message]:
    """
    Formats the content of each message in the template using the provided keyword arguments.

    Args:
        **kwargs: Keyword arguments used to substitute placeholders in message content.

    Returns:
        List[Message]: A new list of Message objects with formatted content.
    """
    formatted_messages = []
    for message in self.messages:
        formatted_content = message.content.format(**kwargs)
        formatted_messages.append(Message(role=message.role, content=formatted_content))
    return formatted_messages

ReusablePrompt

A reference to a reusable prompt on the OpenAI server (see Official Documentation).

Bases: BaseModel

References a reusable prompt template and its associated variables.

Attributes:

Name Type Description
id str

The unique identifier of the reusable prompt template.

version str

The specific version of the prompt template to use.

variables Dict[str, Any]

A dictionary of variable names and their values to be used when formatting the prompt.

Source code in openbatch/model.py
57
58
59
60
61
62
63
64
65
66
67
68
69
class ReusablePrompt(BaseModel):
    """
    References a reusable prompt template and its associated variables.

    Attributes:
        id (str): The unique identifier of the reusable prompt template.
        version (str): The specific version of the prompt template to use.
        variables (Dict[str, Any]): A dictionary of variable names and their values
                                     to be used when formatting the prompt.
    """
    id: str
    version: str
    variables: Dict[str, Any]

Input Instances

Input instances package the unique data for each request in a batch.

PromptTemplateInputInstance

Used with BatchJobManager to provide values for a PromptTemplate or ReusablePrompt.

Bases: InputInstance

An input instance defined by mapping values to variables in a prompt template.

Attributes:

Name Type Description
id str

Unique identifier of the input instance.

prompt_value_mapping Dict[str, str]

Mapping of prompt variable names to their values for this instance.

instance_request_options Optional[Dict[str, Any]]

Options specific to the input instance that can be set in the API request. Optional.

Source code in openbatch/model.py
108
109
110
111
112
113
114
115
116
117
118
119
class PromptTemplateInputInstance(InputInstance):
    """
    An input instance defined by mapping values to variables in a prompt template.

    Attributes:
        id (str): Unique identifier of the input instance.
        prompt_value_mapping (Dict[str, str]): Mapping of prompt variable names
            to their values for this instance.
        instance_request_options (Optional[Dict[str, Any]]): Options specific to the
            input instance that can be set in the API request. Optional.
    """
    prompt_value_mapping: Dict[str, str] = Field(description="Mapping of prompt variable names to their values.")

EmbeddingInputInstance

Used with BatchJobManager to provide the text(s) for an embedding request.

Bases: InputInstance

An input instance specifically for embedding requests.

Attributes:

Name Type Description
id str

Unique identifier of the input instance.

input Union[str, List[str]]

The text or list of texts to be embedded for this instance.

instance_request_options Optional[Dict[str, Any]]

Options specific to the input instance that can be set in the API request. Optional.

Source code in openbatch/model.py
121
122
123
124
125
126
127
128
129
130
131
class EmbeddingInputInstance(InputInstance):
    """
    An input instance specifically for embedding requests.

    Attributes:
        id (str): Unique identifier of the input instance.
        input (Union[str, List[str]]): The text or list of texts to be embedded for this instance.
        instance_request_options (Optional[Dict[str, Any]]): Options specific to the
            input instance that can be set in the API request. Optional.
    """
    input: Union[str, List[str]] = Field(description="Text(s) to be embedded.")

MessagesInputInstance

A generic input instance defined by a complete list of messages.

Bases: InputInstance

An input instance defined by a list of messages.

Attributes:

Name Type Description
id str

Unique identifier of the input instance.

messages List[Message]

List of messages to be sent to the model for this instance.

instance_request_options Optional[Dict[str, Any]]

Options specific to the input instance that can be set in the API request. Optional.

Source code in openbatch/model.py
 96
 97
 98
 99
100
101
102
103
104
105
106
class MessagesInputInstance(InputInstance):
    """
    An input instance defined by a list of messages.

    Attributes:
        id (str): Unique identifier of the input instance.
        messages (List[Message]): List of messages to be sent to the model for this instance.
        instance_request_options (Optional[Dict[str, Any]]): Options specific to the
            input instance that can be set in the API request. Optional.
    """
    messages: List[Message] = Field(description="List of messages to be sent to the model.")

Core Components

Message

Represents a single message in a conversation.

Bases: BaseModel

Represents a single message in a conversation or prompt.

Attributes:

Name Type Description
role str

The role of the message sender (e.g., "user", "assistant", "system").

content str

The text content of the message.

Source code in openbatch/model.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Message(BaseModel):
    """
    Represents a single message in a conversation or prompt.

    Attributes:
        role (str): The role of the message sender (e.g., "user", "assistant", "system").
        content (str): The text content of the message.
    """
    role: str
    content: str

    def serialize(self):
        """
        Converts the Message instance into a dictionary suitable for API requests.

        Returns:
            Dict[str, str]: A dictionary with 'role' and 'content' keys.
        """
        return {"role": self.role, "content": self.content}

serialize()

Converts the Message instance into a dictionary suitable for API requests.

Returns:

Type Description

Dict[str, str]: A dictionary with 'role' and 'content' keys.

Source code in openbatch/model.py
22
23
24
25
26
27
28
29
def serialize(self):
    """
    Converts the Message instance into a dictionary suitable for API requests.

    Returns:
        Dict[str, str]: A dictionary with 'role' and 'content' keys.
    """
    return {"role": self.role, "content": self.content}