Utilities

Utitlities shared by multiple modules or not belonging to any module.
from dart_math.utils import *

Prompt


source

PromptTemplate

 PromptTemplate (id:str='alpaca', sys_prompt:str='Below is an instruction
                 that describes a task. Write a response that
                 appropriately completes the request.\n\n',
                 query_prompt:str='### Instruction:\n',
                 prompt_after_query:str='\n\n', resp_prompt:str='###
                 Response:\n', prompt_before_resp:str='',
                 delim:str='\n\n')

Prompt template. The complete prompt is in the form {sys_prompt}{eg_qa1}{delim}{eg_qa2}{delim}...{delim}{eg_qaN}{delim}{query_prompt}{query}{prompt_after_query}{resp_prompt}{prompt_before_resp}. default: PROMPT_TEMPLATE_ID2DICT[“alpaca”]

Type Default Details
id str alpaca Short name as ID of the prompt template, like “alpaca”.
sys_prompt str Below is an instruction that describes a task. Write a response that appropriately completes the request.

System prompt as the beginning of the full prompt.
query_prompt str ### Instruction:
Simple prompt as delimiter between response and new query.
prompt_after_query str

Prompt to append after the raw query, like “Let’s think step by step.”.
resp_prompt str ### Response:
Simple prompt as delimiter between query and response.
prompt_before_resp str
delim str

Delimiter between query-response pairs.

source

PromptTemplate.load_from_id_or_path

 PromptTemplate.load_from_id_or_path (prompt_template:str='alpaca')

Load prompt template from ID or file path.


source

PromptTemplate.get_prompt_template_from_prompt_type_and_model

 PromptTemplate.get_prompt_template_from_prompt_type_and_model
                                                                (prompt_ty
                                                                pe:str, mo
                                                                del_dirnam
                                                                e:str)

Get the prompt template suitable for the model.

Type Details
prompt_type str Prompt type, like “cot” or “tool”.
model_dirname str HF ID or path to the model.
Returns PromptTemplate The prompt template suitable for the model.

source

PromptTemplate.make_qa_pair

 PromptTemplate.make_qa_pair (query:str, response:str)

Make a QA pair of {query_prompt}{query}{prompt_after_query}{resp_prompt}{prompt_before_resp}{response}.


source

PromptTemplate.make_full_prompt

 PromptTemplate.make_full_prompt (query:str,
                                  eg_qas:list[tuple[str,str]]=[])

Make full prompt as input to the model. Format: f”{sys_prompt}{eg_qa1}{eg_qa2}…{eg_qaN}{query_prompt}{query}{prompt_after_query}{resp_prompt}{prompt_before_resp}“.

Examples

deepseekmath_prompt_template = PromptTemplate.load_from_id_or_path("deepseekmath")
deepseekmath_prompt_template.make_full_prompt("What is 2+2?")
'User: What is 2+2?\nPlease reason step by step, and put your final answer within \\boxed{}.\n\nAssistant: '
deepseekmath_prompt_template.make_full_prompt("What is 2+2?", [("What is 1+1?", "2")])
'User: What is 1+1?\nPlease reason step by step, and put your final answer within \\boxed{}.\n\nAssistant: 2<|end▁of▁sentence|>User: What is 2+2?\nPlease reason step by step, and put your final answer within \\boxed{}.\n\nAssistant: '

Logging


source

init_logging

 init_logging (log_path:str=None, format:str='[%(levelname)s]
               [%(asctime)s.%(msecs)d] [pid %(process)d]
               [%(pathname)s:%(lineno)d:%(funcName)s]\n%(message)s',
               datefmt:str='%Y-%m-%d %H:%M:%S', level:int=20,
               force:bool=True)

Initialize logging configuration.

Type Default Details
log_path str None File path to save log to.
format : str, default: “[%(levelname)s] [%(asctime)s.%(msecs)d] [pid %(process)d] [%(pathname)s:%(lineno)d:%(funcName)s]
format str [%(levelname)s] [%(asctime)s.%(msecs)d] [pid %(process)d] [%(pathname)s:%(lineno)d:%(funcName)s]
%(message)s
Logging format
datefmt str %Y-%m-%d %H:%M:%S
level int 20
force bool True
Returns None

IO

Path


source

get_pathname_from_name_or_path

 get_pathname_from_name_or_path (name_or_path:str)

Get the name suitable for file system from the HF-style name_or_path.

JSON(L)

Based on json (more versatile) and orjson (more efficient).


source

load_jsonl

 load_jsonl (fpath:str, use_tqdm:bool=False)

Load JSONL file.


source

save_jsonl

 save_jsonl (data:list, fpath:str)

Save JSONL file.


source

load_json

 load_json (fpath:str)

Load JSON file.


source

save_json

 save_json (data:dict, fpath:str, indent:int=2)

Save JSON file.

Back to top