from dart_math.utils import *
Utilities
Prompt
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. |
PromptTemplate.load_from_id_or_path
PromptTemplate.load_from_id_or_path (prompt_template:str='alpaca')
Load prompt template from ID or file path.
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. |
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}
.
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
= PromptTemplate.load_from_id_or_path("deepseekmath") deepseekmath_prompt_template
"What is 2+2?") deepseekmath_prompt_template.make_full_prompt(
'User: What is 2+2?\nPlease reason step by step, and put your final answer within \\boxed{}.\n\nAssistant: '
"What is 2+2?", [("What is 1+1?", "2")]) deepseekmath_prompt_template.make_full_prompt(
'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
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
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) andorjson
(more efficient).
load_jsonl
load_jsonl (fpath:str, use_tqdm:bool=False)
Load JSONL file.
save_jsonl
save_jsonl (data:list, fpath:str)
Save JSONL file.
load_json
load_json (fpath:str)
Load JSON file.
save_json
save_json (data:dict, fpath:str, indent:int=2)
Save JSON file.