🚀 feat(api): add support for frontend_node in validate_prompt endpoint
✨ feat(base.py): add frontend_node parameter to ValidatePromptRequest and PromptValidationResponse models
The validate_prompt endpoint now accepts a frontend_node parameter in the ValidatePromptRequest model. This parameter is used to add input variables to the frontend_node's template fields and custom fields. The PromptValidationResponse model now includes the frontend_node parameter to return the updated frontend_node object.
This commit is contained in:
parent
ab77792d65
commit
880fa5034f
2 changed files with 24 additions and 5 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
from langflow.template.frontend_node.base import FrontendNode
|
||||||
from pydantic import BaseModel, validator
|
from pydantic import BaseModel, validator
|
||||||
|
|
||||||
from langflow.interface.utils import extract_input_variables_from_prompt
|
from langflow.interface.utils import extract_input_variables_from_prompt
|
||||||
|
|
@ -12,8 +13,9 @@ class Code(BaseModel):
|
||||||
code: str
|
code: str
|
||||||
|
|
||||||
|
|
||||||
class Prompt(BaseModel):
|
class ValidatePromptRequest(BaseModel):
|
||||||
template: str
|
template: str
|
||||||
|
frontend_node: FrontendNode
|
||||||
|
|
||||||
|
|
||||||
# Build ValidationResponse class for {"imports": {"errors": []}, "function": {"errors": []}}
|
# Build ValidationResponse class for {"imports": {"errors": []}, "function": {"errors": []}}
|
||||||
|
|
@ -32,6 +34,7 @@ class CodeValidationResponse(BaseModel):
|
||||||
|
|
||||||
class PromptValidationResponse(BaseModel):
|
class PromptValidationResponse(BaseModel):
|
||||||
input_variables: list
|
input_variables: list
|
||||||
|
frontend_node: FrontendNode
|
||||||
|
|
||||||
|
|
||||||
INVALID_CHARACTERS = {
|
INVALID_CHARACTERS = {
|
||||||
|
|
@ -66,7 +69,7 @@ def validate_prompt(template: str):
|
||||||
# if len(input_variables) > 1:
|
# if len(input_variables) > 1:
|
||||||
# # If there's more than one input variable
|
# # If there's more than one input variable
|
||||||
|
|
||||||
return PromptValidationResponse(input_variables=input_variables)
|
return input_variables
|
||||||
|
|
||||||
|
|
||||||
def check_input_variables(input_variables: list):
|
def check_input_variables(input_variables: list):
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,11 @@ from fastapi import APIRouter, HTTPException
|
||||||
from langflow.api.v1.base import (
|
from langflow.api.v1.base import (
|
||||||
Code,
|
Code,
|
||||||
CodeValidationResponse,
|
CodeValidationResponse,
|
||||||
Prompt,
|
ValidatePromptRequest,
|
||||||
PromptValidationResponse,
|
PromptValidationResponse,
|
||||||
validate_prompt,
|
validate_prompt,
|
||||||
)
|
)
|
||||||
|
from langflow.template.field.base import TemplateField
|
||||||
from langflow.utils.logger import logger
|
from langflow.utils.logger import logger
|
||||||
from langflow.utils.validate import validate_code
|
from langflow.utils.validate import validate_code
|
||||||
|
|
||||||
|
|
@ -27,9 +28,24 @@ def post_validate_code(code: Code):
|
||||||
|
|
||||||
|
|
||||||
@router.post("/prompt", status_code=200, response_model=PromptValidationResponse)
|
@router.post("/prompt", status_code=200, response_model=PromptValidationResponse)
|
||||||
def post_validate_prompt(prompt: Prompt):
|
def post_validate_prompt(prompt: ValidatePromptRequest):
|
||||||
try:
|
try:
|
||||||
return validate_prompt(prompt.template)
|
input_variables = validate_prompt(prompt.template)
|
||||||
|
for variable in input_variables:
|
||||||
|
try:
|
||||||
|
template_field = TemplateField(
|
||||||
|
name=variable, field_type="str", show=True, advanced=False
|
||||||
|
)
|
||||||
|
prompt.frontend_node.template.fields.append(template_field)
|
||||||
|
prompt.frontend_node.custom_fields.append(variable)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.exception(exc)
|
||||||
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
||||||
|
|
||||||
|
return PromptValidationResponse(
|
||||||
|
input_variables=input_variables,
|
||||||
|
frontend_node=prompt.frontend_node,
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue