Skip to content

Commit fdf3d71

Browse files
committed
Added comments to icl
1 parent bb2bd3b commit fdf3d71

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

‎src/hackingBuddyGPT/utils/prompt_generation/prompts/state_learning/in_context_learning_prompt.py‎

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def generate_prompt(
5656
move_type (str): The type of move to generate.
5757
hint (Optional[str]): An optional hint to guide the prompt generation.
5858
previous_prompt (List[Dict[str, str]]): A list of previous prompt entries, each containing a "content" key.
59+
turn (Optional[int]): Current turn.
5960
6061
Returns:
6162
str: The generated prompt.
@@ -77,6 +78,22 @@ def generate_prompt(
7778
return self.prompt_helper._check_prompt(previous_prompt=previous_prompt, steps=steps)
7879

7980
def _get_documentation_steps(self, move_type: str, previous_prompt, doc_steps: Any) -> List[str]:
81+
"""
82+
Generates documentation steps based on the current API specification, previous prompts,
83+
and the intended move type.
84+
85+
Args:
86+
move_type (str): Determines the strategy to apply. Accepted values:
87+
- "explore": Generates initial documentation steps for exploration.
88+
- Any other value: Triggers identification of endpoints needing more help.
89+
previous_prompt (Any): A history of previously generated prompts used to determine
90+
which endpoints have already been addressed.
91+
doc_steps (Any): Existing documentation steps that are modified or expanded based on
92+
the selected move_type.
93+
94+
Returns:
95+
List[str]: A list of documentation prompts tailored to the move_type and current context.
96+
"""
8097
# Extract properties and example response
8198
if "endpoints" in self.open_api_spec:
8299
properties = self.extract_properties()
@@ -118,11 +135,23 @@ def _get_documentation_steps(self, move_type: str, previous_prompt, doc_steps: A
118135
return self.prompt_helper.get_endpoints_needing_help(
119136
info=f"Based on this information :\n{icl_prompt}\n Do the following: ")
120137

121-
# Function to extract properties from the schema
122-
123138

124-
# Function to extract example response from paths
125139
def extract_example_response(self, api_paths, endpoint, method="get"):
140+
"""
141+
Extracts a representative example response for a specified API endpoint and method
142+
from an OpenAPI specification.
143+
Args:
144+
api_paths (dict): A dictionary representing the paths section of the OpenAPI spec,
145+
typically `self.open_api_spec["endpoints"]`.
146+
endpoint (str): The specific API endpoint to extract the example from (e.g., "/users").
147+
method (str, optional): The HTTP method to consider (e.g., "get", "post").
148+
Defaults to "get".
149+
150+
Returns:
151+
dict: A dictionary with the HTTP method as the key and the extracted example
152+
response as the value. If no suitable example is found, returns an empty dict.
153+
Format: { "get": { "exampleName": exampleData } }
154+
"""
126155
example_method = {}
127156
example_response = {}
128157
# Ensure that the provided endpoint and method exist in the schema
@@ -159,8 +188,23 @@ def extract_example_response(self, api_paths, endpoint, method="get"):
159188

160189
# Function to generate the prompt for In-Context Learning
161190
def generate_icl_prompt(self, properties, example_response, endpoint):
191+
"""
192+
Generates an in-context learning (ICL) prompt to guide a language model in understanding
193+
and documenting a REST API endpoint.
194+
195+
Args:
196+
properties (dict): A dictionary of property names to their types and example values.
197+
Format: { "property_name": {"type": "string", "example": "value"} }
198+
example_response (dict): A dictionary containing example API responses, typically extracted
199+
using `extract_example_response`. Format: { "get": { ...example... } }
200+
endpoint (str): The API endpoint path (e.g., "/users").
201+
202+
Returns:
203+
str: A formatted prompt string containing API metadata, property descriptions,
204+
and a JSON-formatted example response.
205+
"""
162206
# Core information about API
163-
if example_response.keys() != {}:
207+
if len(example_response.keys()) > 0:
164208
prompt = f"# REST API: {list(example_response.keys())[0].upper()} {endpoint}\n\n"
165209
else:
166210
prompt = f"# REST API: {endpoint}\n\n"
@@ -186,6 +230,22 @@ def generate_icl_prompt(self, properties, example_response, endpoint):
186230
return prompt
187231

188232
def extract_properties_with_examples(self, data):
233+
"""
234+
Extracts and flattens properties from a nested dictionary or list of dictionaries,
235+
producing a dictionary of property names along with their inferred types and example values.
236+
237+
Args:
238+
data (dict or list): The input data, usually an example API response. This can be:
239+
- A single dictionary (representing a single API object).
240+
- A list of dictionaries (representing a collection of API objects).
241+
- A special-case dict with a single `None` key, which is unwrapped.
242+
243+
Returns:
244+
dict: A dictionary mapping property names to a dictionary with keys:
245+
- "type": The inferred data type (e.g., "string", "integer").
246+
- "example": A sample value for the property.
247+
Format: { "property_name": {"type": "string", "example": "value"} }
248+
"""
189249

190250
# Handle nested dictionaries, return flattened properties
191251

@@ -327,7 +387,16 @@ def transform_test_case_to_string(self, test_case, character):
327387

328388
return ''.join(result)
329389

330-
def get_props(self, data, result ):
390+
def get_props(self, data:dict, result:dict ):
391+
"""
392+
Recursively extracts properties from a dictionary, including nested dictionaries and lists,
393+
and appends them to the result dictionary with their inferred data types and example values.
394+
395+
Returns:
396+
dict: The updated result dictionary containing all extracted properties, including those
397+
found in nested dictionaries or lists.
398+
"""
399+
331400
for key, value in data.items():
332401

333402
if isinstance(value, dict):

‎src/hackingBuddyGPT/utils/prompt_generation/prompts/task_planning/chain_of_thought_prompt.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,12 @@ def generate_documentation_steps(self, steps) -> list:
230230
Creates a chain of thought prompt to guide the model through the API documentation process.
231231
232232
Args:
233-
use_token (str): A string indicating whether authentication is required.
234-
endpoints (list): A list of endpoints to exclude from testing.
233+
steps (list): A list of steps, where each step is a list. The first element
234+
of each inner list is the step title, followed by its sub-steps or details.
235235
236-
Returns:
237-
str: A structured chain of thought prompt for documentation.
236+
Returns:
237+
list: A transformed list where each step (except the first) is prefixed with
238+
"Step X:" headers and includes its associated sub-steps.
238239
"""
239240

240241
transformed_steps = [steps[0]]

‎src/hackingBuddyGPT/utils/prompt_generation/prompts/task_planning/task_planning_prompt.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ def __init__(self, context: PromptContext, prompt_helper, strategy: PromptStrate
5151

5252
def _get_documentation_steps(self, common_steps: List[str], move_type: str, steps: Any) -> List[str]:
5353
"""
54-
Provides the steps for the chain-of-thought strategy when the context is documentation.
54+
Provides the steps for the task learning prompt when the context is documentation.
5555
5656
Args:
5757
common_steps (List[str]): A list of common steps for generating prompts.
5858
move_type (str): The type of move to generate.
59+
steps (Any): steps that are transformed into task planning prompt
5960
6061
Returns:
6162
List[str]: A list of steps for the chain-of-thought strategy in the documentation context.

0 commit comments

Comments
 (0)