-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
63 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 45 additions & 13 deletions
58
src/bpmn_assistant/services/process_editing/define_change_request.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
from importlib import resources | ||
import traceback | ||
|
||
from pydantic import BaseModel | ||
|
||
from bpmn_assistant.config import logger | ||
from bpmn_assistant.core import LLMFacade, MessageItem | ||
from bpmn_assistant.utils import prepare_prompt, message_history_to_string | ||
from bpmn_assistant.utils import message_history_to_string, prepare_prompt | ||
|
||
|
||
class DefineChangeRequestResponse(BaseModel): | ||
change_request: str | ||
|
||
|
||
def _validate_define_change_request(response: dict) -> None: | ||
""" | ||
Validate the response from the define_change_request function. | ||
Args: | ||
response: The response to validate | ||
Raises: | ||
ValueError: If the response is invalid | ||
""" | ||
if "change_request" not in response: | ||
raise ValueError("Invalid response: 'change_request' key not found") | ||
|
||
|
||
def define_change_request( | ||
llm_facade: LLMFacade, process: list[dict], message_history: list[MessageItem] | ||
llm_facade: LLMFacade, | ||
process: list[dict], | ||
message_history: list[MessageItem], | ||
max_retries: int = 3, | ||
) -> str: | ||
""" | ||
Defines the change to be made in the BPMN process based on the message history. | ||
Args: | ||
llm_facade: The LLM facade object | ||
process: The BPMN process | ||
message_history: The message history | ||
max_retries: The maximum number of retries in case of failure | ||
Returns: | ||
str: The change request | ||
""" | ||
|
||
prompt_template = resources.read_text( | ||
"bpmn_assistant.prompts", "define_change_request.txt" | ||
) | ||
|
||
prompt = prepare_prompt( | ||
prompt_template, | ||
"define_change_request.txt", | ||
process=str(process), | ||
message_history=message_history_to_string(message_history), | ||
) | ||
|
||
json_object = llm_facade.call(prompt, max_tokens=100, temperature=0.4) | ||
attempts = 0 | ||
|
||
# TODO: validate the response, retry until it's valid | ||
while attempts < max_retries: | ||
|
||
logger.info(f"Change request: {json_object['change_request']}") | ||
attempts += 1 | ||
|
||
return json_object["change_request"] | ||
try: | ||
json_object = llm_facade.call(prompt, max_tokens=100, temperature=0.4) | ||
_validate_define_change_request(json_object) | ||
logger.info(f"Change request: {json_object['change_request']}") | ||
return json_object["change_request"] | ||
except Exception as e: | ||
logger.warning( | ||
f"Validation error (attempt {attempts}): {str(e)}\n" | ||
f"Traceback: {traceback.format_exc()}" | ||
) | ||
|
||
prompt = f"Error: {str(e)}. Try again." | ||
|
||
raise Exception( | ||
"Maximum number of retries reached. Could not define change request." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters