-
Notifications
You must be signed in to change notification settings - Fork 513
MT-v2: Prompt studio core variable replacement minor changes from PR 611 #718
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
Merged
hari-kuriakose
merged 4 commits into
main
from
feat/mt-v2-prompt-studio-changes-from-pr-611
Sep 25, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
40fd836
multi-tenant v2 PS changes from PR 611
chandrasekharan-zipstack 9f9991e
Updated to v2 import
chandrasekharan-zipstack 2240d93
resolved import issue
muhammad-ali-e 3ef578f
Merge branch 'main' into feat/mt-v2-prompt-studio-changes-from-pr-611
muhammad-ali-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
95 changes: 95 additions & 0 deletions
95
backend/prompt_studio/prompt_studio_core_v2/prompt_variable_service.py
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import re | ||
from enum import Enum | ||
from typing import Any | ||
|
||
from prompt_studio.prompt_studio_core_v2.exceptions import PromptNotRun | ||
from prompt_studio.prompt_studio_output_manager_v2.models import ( | ||
PromptStudioOutputManager, | ||
) | ||
from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt | ||
|
||
|
||
class VariableType(str, Enum): | ||
STATIC = "STATIC" | ||
DYNAMIC = "DYNAMIC" | ||
|
||
|
||
class VariableConstants: | ||
|
||
VARIABLE_REGEX = "{{(.+?)}}" | ||
DYNAMIC_VARIABLE_DATA_REGEX = r"\[(.*?)\]" | ||
DYNAMIC_VARIABLE_URL_REGEX = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))" # noqa: E501 | ||
|
||
|
||
class PromptStudioVariableService: | ||
|
||
@staticmethod | ||
def fetch_variable_outputs(variable: str, doc_id: str, tool_id: str) -> Any: | ||
variable_prompt: ToolStudioPrompt = ToolStudioPrompt.objects.get( | ||
prompt_key=variable, tool_id=tool_id | ||
) | ||
try: | ||
output = PromptStudioOutputManager.objects.get( | ||
prompt_id=variable_prompt.prompt_id, | ||
document_manager=doc_id, | ||
tool_id=variable_prompt.tool_id, | ||
profile_manager=variable_prompt.profile_manager, | ||
is_single_pass_extract=False, | ||
) | ||
except PromptStudioOutputManager.DoesNotExist: | ||
raise PromptNotRun( | ||
f"The prompt : {variable} must be executed before " | ||
"it can be used as a variable in another prompt. " | ||
"Please execute the prompt first and try again." | ||
) | ||
return output.output | ||
|
||
@staticmethod | ||
def identify_variable_type(variable: str) -> VariableType: | ||
variable_type: VariableType | ||
pattern = re.compile(VariableConstants.DYNAMIC_VARIABLE_URL_REGEX) | ||
if re.findall(pattern, variable): | ||
variable_type = VariableType.DYNAMIC | ||
else: | ||
variable_type = VariableType.STATIC | ||
return variable_type | ||
|
||
@staticmethod | ||
def extract_variables_from_prompt(prompt: str) -> list[str]: | ||
variable: list[str] = [] | ||
variable = re.findall(VariableConstants.VARIABLE_REGEX, prompt) | ||
return variable | ||
|
||
@staticmethod | ||
def frame_variable_replacement_map( | ||
doc_id: str, prompt_object: ToolStudioPrompt | ||
) -> dict[str, Any]: | ||
variable_output_map: dict[str, Any] = {} | ||
prompt = prompt_object.prompt | ||
variables = PromptStudioVariableService.extract_variables_from_prompt( | ||
prompt=prompt | ||
) | ||
for variable in variables: | ||
variable_type: VariableType = ( | ||
PromptStudioVariableService.identify_variable_type(variable=variable) | ||
) | ||
if variable_type == VariableType.STATIC: | ||
variable_output_map[variable] = ( | ||
PromptStudioVariableService.fetch_variable_outputs( | ||
variable=variable, | ||
doc_id=doc_id, | ||
tool_id=prompt_object.tool_id.tool_id, | ||
) | ||
) | ||
if variable_type == VariableType.DYNAMIC: | ||
variable = re.findall( | ||
VariableConstants.DYNAMIC_VARIABLE_DATA_REGEX, variable | ||
)[0] | ||
variable_output_map[variable] = ( | ||
PromptStudioVariableService.fetch_variable_outputs( | ||
variable=variable, | ||
doc_id=doc_id, | ||
tool_id=prompt_object.tool_id.tool_id, | ||
) | ||
) | ||
return variable_output_map |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.