PromptTemplate¶
- class openicl.PromptTemplate(template: Dict | str, column_token_map: Dict, selected_column_name: str | None = None, selected_column_map: Dict | None = None, ice_token: str | None = None)[source]¶
- In-context Learning Prompt Template Class
This class represents a template that guides the generation of prompts in the retrieval or inference process.
- template¶
A custom template dictionary or string. If a dictionary, the keys of the dictionary represent the values of the output_column, and the values represent the corresponding generated statement. If a string, it represents a string template.
- Type:
Dictorstr
- column_token_map¶
A dictionary mapping column names to specific tokens. The tokens will be replaced by data in the corresponding column (one piece each time) during the retrieval or inference process.
- Type:
Dict
- selected_column_name¶
Used only with string-type templates. A specific column that needs its value to be mapped.
- Type:
str, optional
- selected_column_map¶
Used only with string-type templates. Maps the value of the column
selected_column_name.- Type:
Dict, optional
- ice_token¶
A string that represents the specific token mapping from in-context examples. None if you want to use this template only to generate in-context examples, otherwise it can be used to generate the final prompt that is fed into the PLM. The ice_token will be invisible when generating in-context examples.
- Type:
str, optional
- generate_ice_item(entry: Dict, label: Hashable) str[source]¶
Generate in-context example based on the provided
entrydata.- Parameters:
entry (
Dict) – A piece of data to be used for generating the in-context example.label (
Hashable) – The value of the output field.
- Returns:
The generated in-context example.
- Return type:
str
- generate_item(entry: Dict, output_field: Hashable | None = None, output_field_replace_token: str | None = '', ice_field_replace_token: str | None = '') str[source]¶
Generate an item based on the provided
entrydata, as well as optional output field and ice field tokens.- Parameters:
entry (
Dict) – A piece of data.output_field (
Hashable, optional) – Column name of output field. Defaults toNone.output_field_replace_token (
str, optional) – Tokens used to replace output field. Defaults to''.ice_field_replace_token (str, optional) – Tokens used to replace the
ice_token. Defaults to''.
- Returns:
The generated item.
- Return type:
str
- generate_label_prompt_item(entry: Dict, ice: str, label: Hashable) str[source]¶
Generate prompt based on
entrydata,icein-context example, and the correspondinglabel.- Parameters:
entry (
Dict) – A piece of data containing the input field content.ice (
str) – The generated in-context example.label (
Hashable) – The value of the output field.
- Raises:
ValueError – If the
ice_tokenattribute of thePromptTemplateinstance isNone.- Returns:
The generated prompt.
- Return type:
str