Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-code documentation update #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Evol_Instruct/breadth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@


def createBreadthPrompt(instruction):
"""
Creates a breadth prompt by combining a base instruction with a given instruction.

Parameters
----------
instruction : str
The given instruction to be included in the prompt.

Returns
-------
prompt : str
The created breadth prompt.
"""
prompt = base_instruction
prompt += "#Given Prompt#: \r\n {} \r\n".format(instruction)
prompt += "#Created Prompt#:\r\n"
Expand Down
57 changes: 56 additions & 1 deletion Evol_Instruct/depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,80 @@


def createConstraintsPrompt(instruction):
"""
Creates a prompt for adding constraints/requirements to a given instruction.

Parameters
----------
instruction : str
The given instruction to which constraints/requirements will be added.

Returns
-------
prompt : str
The prompt for adding constraints/requirements to the given instruction.
"""
prompt = base_instruction.format("Please add one more constraints/requirements into #The Given Prompt#'")
prompt += "#The Given Prompt#: \r\n {} \r\n".format(instruction)
prompt += "#Rewritten Prompt#:\r\n"
return prompt

def createDeepenPrompt(instruction):
"""
Creates a deepened prompt by adding a rewritten prompt to the given instruction.

Parameters
----------
instruction : str
The given prompt that contains inquiries about certain issues.

Returns
-------
prompt : str
The deepened prompt that includes the given prompt and a rewritten prompt.

Notes
-----
The depth and breadth of the inquiry can be increased by adding a rewritten prompt to the given prompt.
"""
prompt = base_instruction.format("If #The Given Prompt# contains inquiries about certain issues, the depth and breadth of the inquiry can be increased.")
prompt += "#The Given Prompt#: \r\n {} \r\n".format(instruction)
prompt += "#Rewritten Prompt#:\r\n"
return prompt

def createConcretizingPrompt(instruction):
"""
Creates a concretizing prompt by replacing general concepts with more specific concepts.

Parameters
----------
instruction : str
The original instruction to be concretized.

Returns
-------
prompt : str
The concretizing prompt with the given instruction and rewritten prompt.
"""
prompt = base_instruction.format("Please replace general concepts with more specific concepts.")
prompt += "#The Given Prompt#: \r\n {} \r\n".format(instruction)
prompt += "#Rewritten Prompt#:\r\n"
return prompt


def createReasoningPrompt(instruction):
"""
Creates a reasoning prompt by rewriting the given prompt to explicitly request multiple-step reasoning.

Parameters
----------
instruction : str
The given prompt that needs to be rewritten.

Returns
-------
prompt : str
The rewritten prompt that explicitly requests multiple-step reasoning.
"""
prompt = base_instruction.format("If #The Given Prompt# can be solved with just a few simple thinking processes, you can rewrite it to explicitly request multiple-step reasoning.")
prompt += "#The Given Prompt#: \r\n {} \r\n".format(instruction)
prompt += "#Rewritten Prompt#:\r\n"
Expand Down
52 changes: 52 additions & 0 deletions Evol_Instruct/openai_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,33 @@


def get_oai_completion(prompt):
"""
Retrieves OpenAI GPT-3.5-Turbo completion for the given prompt.

Parameters
----------
prompt : str
The input prompt for which completion is requested.

Returns
-------
gpt_output : str or None
The completion generated by the OpenAI GPT-3.5-Turbo model, or None if an error occurs.

Notes
-----
This function sends a request to the OpenAI API to generate completion for the given prompt
using the GPT-3.5-Turbo model. It handles various types of errors and retries the request
in case of a timeout error.

Raises
------
None

See Also
--------
openai.ChatCompletion.create
"""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
Expand Down Expand Up @@ -47,6 +73,32 @@ def get_oai_completion(prompt):
return get_oai_completion(prompt)

def call_chatgpt(ins):
"""
Calls the OpenAI GPT-3.5-Turbo model to generate completion for the given input.

Parameters
----------
ins : str
The input prompt for which completion is requested.

Returns
-------
ans : str
The completion generated by the OpenAI GPT-3.5-Turbo model.

Notes
-----
This function repeatedly calls the `get_oai_completion` function to retrieve completion
for the given input prompt. It retries the request if an error occurs, with a maximum of 15 retries.

Raises
------
None

See Also
--------
get_oai_completion
"""
success = False
re_try_count = 15
ans = ''
Expand Down
134 changes: 132 additions & 2 deletions WizardMath/inference/grader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@


def is_digit(s):
"""
Checks if the input can be converted to a float.

Parameters
----------
s : any
The input to check.

Returns
-------
bool
True if the input can be converted to a float, False otherwise.

Raises
------
None
"""
try:
float(str(s).replace(",", ""))
return True
Expand All @@ -25,9 +42,37 @@ def math_equal(prediction: Union[bool, float, str],
timeout: bool = False,
) -> bool:
"""
Determines if two math expressions are equal.

Exact match of math if and only if:
1. numerical equal: both can convert to float and are equal
2. symbolic equal: both can convert to sympy expression and are equal

Parameters
----------
prediction : Union[bool, float, str]
The predicted value.

reference : Union[float, str]
The reference value.

include_percentage : bool, optional
Whether to include percentage variations, by default True.

is_close : bool, optional
Whether to consider close match using relative tolerance, by default True.

timeout : bool, optional
Whether to apply timeout to the symbolic equal check, by default False.

Returns
-------
bool
True if the values are equal, False otherwise.

Raises
------
None
"""
try: # 1. numerical equal
if is_digit(prediction) and is_digit(reference):
Expand Down Expand Up @@ -92,10 +137,47 @@ def math_equal(prediction: Union[bool, float, str],


def math_equal_process(param):
"""
Calls `math_equal` function with the provided parameters.

Parameters
----------
param : tuple
Tuple containing parameters for `math_equal` function.

Returns
-------
bool
True if the values are equal, False otherwise.

Raises
------
None
"""
return math_equal(param[-2], param[-1])


def symbolic_equal(a, b):
"""
Checks if two expressions are symbolically equal using sympy.

Parameters
----------
a : str
First expression.

b : str
Second expression.

Returns
-------
bool
True if the expressions are equal, False otherwise.

Raises
------
None
"""
def _parse(s):
for f in [parse_latex, parse_expr]:
try:
Expand All @@ -120,12 +202,60 @@ def _parse(s):
return False


def symbolic_equal_process(a, b, output_queue):
def symbolic_equal_process(a, b, output_queue):
"""
Checks if two expressions are symbolically equal using sympy.

Parameters
----------
a : str
First expression.

b : str
Second expression.

output_queue : multiprocessing.Queue
Queue to store the result.

Returns
-------
None

Raises
------
None
"""
result = symbolic_equal(a, b)
output_queue.put(result)


def call_with_timeout(func, *args, timeout=1, **kwargs):
def call_with_timeout(func, *args, timeout=1, **kwargs):
"""
Calls a function with a specified timeout.

Parameters
----------
func : function
The function to call.

timeout : int, optional
Timeout value in seconds, by default 1.

*args : list
Positional arguments for the function.

**kwargs : dict
Keyword arguments for the function.

Returns
-------
bool
True if the function call succeeds within the timeout, False otherwise.

Raises
------
None
"""
output_queue = multiprocessing.Queue()
process_args = args + (output_queue,)
process = multiprocessing.Process(target=func, args=process_args, kwargs=kwargs)
Expand Down