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

Feat/prompt key required field #885

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7b95550
added required field in prompt key
vishnuszipstack Dec 11, 2024
43dbed3
required fields constant and added required field in promptstudio helper
vishnuszipstack Dec 11, 2024
997d310
added frontend required field prompt key functionality
vishnuszipstack Dec 11, 2024
53fb466
added prompt service required key adding in metadata
vishnuszipstack Dec 11, 2024
0c97509
added required key in prompt studio registry and removed some unwante…
vishnuszipstack Dec 11, 2024
4781f5f
changed required field bool to option with any/all
vishnuszipstack Dec 12, 2024
fd29593
made changes in frontend to support option required
vishnuszipstack Dec 12, 2024
0dcf64a
prompt service changes to support required options field
vishnuszipstack Dec 12, 2024
d70c55d
required field based on enforce type
vishnuszipstack Dec 16, 2024
871c67d
changed required choices to class
vishnuszipstack Dec 17, 2024
91f8b6b
Merge branch 'main' into feat/prompt-key-required-field
vishnuszipstack Dec 17, 2024
7e7f666
frontend condition fail issue fix in highlight data
vishnuszipstack Dec 17, 2024
ebd1375
Merge branch 'main' into feat/prompt-key-required-field
vishnuszipstack Dec 17, 2024
cebcb11
required field text changes as per the feedback
vishnuszipstack Dec 17, 2024
b4a7746
Merge remote-tracking branch 'origin/feat/prompt-key-required-field' …
vishnuszipstack Dec 17, 2024
9000cd0
Merge branch 'main' into feat/prompt-key-required-field
vishnuszipstack Dec 20, 2024
4def998
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 20, 2024
b589eee
addressed pr comments
vishnuszipstack Dec 20, 2024
f3b46f0
Merge remote-tracking branch 'origin/feat/prompt-key-required-field' …
vishnuszipstack Dec 20, 2024
e7e2237
added info for required fields
vishnuszipstack Dec 20, 2024
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
1 change: 1 addition & 0 deletions backend/prompt_studio/prompt_studio_core_v2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class ToolStudioPromptKeys:
RECORD = "record"
FILE_PATH = "file_path"
ENABLE_HIGHLIGHT = "enable_highlight"
REQUIRED = "required"


class FileViewTypes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ def _fetch_response(

output[TSPKeys.PROMPT] = prompt.prompt
output[TSPKeys.ACTIVE] = prompt.active
output[TSPKeys.REQUIRED] = prompt.required
output[TSPKeys.CHUNK_SIZE] = profile_manager.chunk_size
output[TSPKeys.VECTOR_DB] = vector_db
output[TSPKeys.EMBEDDING] = embedding_model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class JsonSchemaKey:
SUMMARIZE_AS_SOURCE = "summarize_as_source"
ENABLE_HIGHLIGHT = "enable_highlight"
PLATFORM_POSTAMBLE = "platform_postamble"
REQUIRED = "required"


class SpecKey:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def frame_export_json(

output[JsonSchemaKey.PROMPT] = prompt.prompt
output[JsonSchemaKey.ACTIVE] = prompt.active
output[JsonSchemaKey.REQUIRED] = prompt.required
harini-venkataraman marked this conversation as resolved.
Show resolved Hide resolved
output[JsonSchemaKey.CHUNK_SIZE] = prompt.profile_manager.chunk_size
output[JsonSchemaKey.VECTOR_DB] = vector_db
output[JsonSchemaKey.EMBEDDING] = embedding_model
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.1 on 2024-12-10 10:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("prompt_studio_v2", "0002_alter_toolstudioprompt_enforce_type"),
]

operations = [
migrations.AddField(
model_name="toolstudioprompt",
name="required",
field=models.BooleanField(default=False),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.1 on 2024-12-12 08:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("prompt_studio_v2", "0003_toolstudioprompt_required"),
]

operations = [
migrations.AlterField(
model_name="toolstudioprompt",
name="required",
field=models.CharField(
blank=True,
choices=[("all", "All values required"), ("any", "Any value required")],
default=None,
max_length=3,
null=True,
),
),
]
11 changes: 11 additions & 0 deletions backend/prompt_studio/prompt_studio_v2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ class Mode(models.TextChoices):
db_comment="Field to store the prompt key",
unique=False,
)
REQUIRED_CHOICES = [
vishnuszipstack marked this conversation as resolved.
Show resolved Hide resolved
("all", "All values required"),
("any", "Any value required"),
]
required = models.CharField(
vishnuszipstack marked this conversation as resolved.
Show resolved Hide resolved
max_length=3,
vishnuszipstack marked this conversation as resolved.
Show resolved Hide resolved
choices=REQUIRED_CHOICES,
null=True, # Allows the field to store NULL in the database
blank=True, # Allows the field to be optional in forms
default=None, # Sets the default value to None
)
is_assert = models.BooleanField(default=False)
active = models.BooleanField(default=True, null=False, blank=False)
output_metadata = models.JSONField(
Expand Down
50 changes: 49 additions & 1 deletion frontend/src/components/custom-tools/prompt-card/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function Header({
setExpandCard,
spsLoading,
handleSpsLoading,
enforceType,
}) {
const {
selectedDoc,
Expand All @@ -58,6 +59,7 @@ function Header({
const [items, setItems] = useState([]);

const [isDisablePrompt, setIsDisablePrompt] = useState(null);
const [required, setRequired] = useState(false);

const handleRunBtnClick = (promptRunType, docId = null) => {
setExpandCard(true);
Expand All @@ -73,8 +75,22 @@ function Header({
}
);
};
const handleRequiredChange = (value) => {
const newValue = value === required ? null : value; // Allow deselection
setRequired(newValue);
handleChange(
newValue,
promptDetails?.prompt_id,
"required",
true,
true
).catch(() => {
setRequired(promptDetails?.required || null); // Rollback state in case of error
});
};
useEffect(() => {
setIsDisablePrompt(promptDetails?.active);
setRequired(promptDetails?.required);
}, [promptDetails, details]);

useEffect(() => {
Expand All @@ -87,6 +103,37 @@ function Header({
),
key: "enable",
},
{
label: (
<div>
{["json", "table", "record"].indexOf(enforceType) === -1 && (
<Checkbox
checked={required === "all"}
onChange={() => handleRequiredChange("all")}
>
Required
</Checkbox>
)}
{enforceType === "json" && (
<>
<Checkbox
checked={required === "all"}
onChange={() => handleRequiredChange("all")}
>
All Required
</Checkbox>
<Checkbox
checked={required === "any"}
onChange={() => handleRequiredChange("any")}
>
Any Required
</Checkbox>
</>
)}
</div>
),
key: "required",
},
{
label: (
<ConfirmModal
Expand All @@ -109,7 +156,7 @@ function Header({
}

setItems(dropdownItems);
}, [isDisablePrompt]);
}, [isDisablePrompt, required, enforceType]);

return (
<Row>
Expand Down Expand Up @@ -270,6 +317,7 @@ Header.propTypes = {
setExpandCard: PropTypes.func.isRequired,
spsLoading: PropTypes.object,
handleSpsLoading: PropTypes.func.isRequired,
enforceType: PropTypes.text,
};

export { Header };
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function PromptCardItems({
enabledProfiles={enabledProfiles}
spsLoading={spsLoading}
handleSpsLoading={handleSpsLoading}
enforceType={enforceType}
/>
</Space>
</div>
Expand Down
2 changes: 2 additions & 0 deletions prompt-service/src/unstract/prompt_service/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class PromptServiceContants:
FILE_PATH = "file_path"
HIGHLIGHT_DATA = "highlight_data"
CONFIDENCE_DATA = "confidence_data"
REQUIRED_FIELDS = "required_fields"
REQUIRED = "required"


class RunLevel(Enum):
Expand Down
23 changes: 21 additions & 2 deletions prompt-service/src/unstract/prompt_service/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ def run_completion(
answer: str = completion[PSKeys.RESPONSE].text
highlight_data = completion.get(PSKeys.HIGHLIGHT_DATA)
confidence_data = completion.get(PSKeys.CONFIDENCE_DATA)

if metadata is not None and prompt_key:
if highlight_data:
metadata.setdefault(PSKeys.HIGHLIGHT_DATA, {})[
Expand All @@ -308,7 +307,6 @@ def run_completion(
metadata.setdefault(PSKeys.CONFIDENCE_DATA, {})[
prompt_key
] = confidence_data

return answer
# TODO: Catch and handle specific exception here
except SdkRateLimitError as e:
Expand Down Expand Up @@ -344,3 +342,24 @@ def extract_table(
except table_extractor["exception_cls"] as e:
msg = f"Couldn't extract table. {e}"
raise APIError(message=msg)


def add_required_field(
required_fields: dict[str, str], key: str, required: str
) -> dict[str, str]:
"""
Add or update a required field in the required_fields dictionary.

Args:
required_fields (Dict[str, str]): The dictionary
containing existing required fields,
where keys are field names and values are boolean flags.
key (str): The field key to add or update in the dictionary.
required (str): A indicating whether the
all/any values in field is required.

Returns:
Dict[str, str]: The updated dictionary of required fields.
"""
required_fields[key] = required
return required_fields
8 changes: 7 additions & 1 deletion prompt-service/src/unstract/prompt_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from unstract.prompt_service.constants import RunLevel
from unstract.prompt_service.exceptions import APIError, ErrorResponse, NoPayloadError
from unstract.prompt_service.helper import (
add_required_field,
construct_and_run_prompt,
extract_table,
extract_variable,
Expand Down Expand Up @@ -108,6 +109,7 @@ def prompt_processor() -> Any:
PSKeys.RUN_ID: run_id,
PSKeys.FILE_NAME: doc_name,
PSKeys.CONTEXT: {},
PSKeys.REQUIRED_FIELDS: {},
}
variable_names: list[str] = []
publish_log(
Expand All @@ -117,10 +119,14 @@ def prompt_processor() -> Any:
RunLevel.RUN,
f"Preparing to execute {len(prompts)} prompt(s)",
)

# TODO: Rename "output" to "prompt"
for output in prompts: # type:ignore
variable_names.append(output[PSKeys.NAME])
metadata[PSKeys.REQUIRED_FIELDS] = add_required_field(
metadata[PSKeys.REQUIRED_FIELDS],
output[PSKeys.NAME],
output.get(PSKeys.REQUIRED, None),
)
vishnuszipstack marked this conversation as resolved.
Show resolved Hide resolved
for output in prompts: # type:ignore
prompt_name = output[PSKeys.NAME]
prompt_text = output[PSKeys.PROMPT]
Expand Down
Loading