feat: adding validate/prompt endpoint
This commit is contained in:
parent
9d626c7cbc
commit
c41957c67e
6 changed files with 130 additions and 25 deletions
|
|
@ -1,16 +1,17 @@
|
|||
from langflow.graph.utils import extract_input_variables_from_prompt
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
|
||||
class Code(BaseModel):
|
||||
code: str
|
||||
|
||||
@validator("code")
|
||||
def validate_code(cls, v):
|
||||
return v
|
||||
|
||||
class Prompt(BaseModel):
|
||||
template: str
|
||||
|
||||
|
||||
# Build ValidationResponse class for {"imports": {"errors": []}, "function": {"errors": []}}
|
||||
class ValidationResponse(BaseModel):
|
||||
class CodeValidationResponse(BaseModel):
|
||||
imports: dict
|
||||
function: dict
|
||||
|
||||
|
|
@ -21,3 +22,14 @@ class ValidationResponse(BaseModel):
|
|||
@validator("function")
|
||||
def validate_function(cls, v):
|
||||
return v or {"errors": []}
|
||||
|
||||
|
||||
class PromptValidationResponse(BaseModel):
|
||||
input_variables: list
|
||||
valid: bool
|
||||
|
||||
|
||||
def validate_prompt(template):
|
||||
# Extract the input variables from template
|
||||
input_variables = extract_input_variables_from_prompt(template)
|
||||
return input_variables, len(input_variables) > 0
|
||||
|
|
|
|||
|
|
@ -3,10 +3,8 @@ from typing import Any, Dict
|
|||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from langflow.api.base import Code, ValidationResponse
|
||||
from langflow.interface.run import process_graph
|
||||
from langflow.interface.types import build_langchain_types_dict
|
||||
from langflow.utils.validate import validate_code
|
||||
|
||||
# build router
|
||||
router = APIRouter()
|
||||
|
|
@ -26,15 +24,3 @@ def get_load(data: Dict[str, Any]):
|
|||
# Log stack trace
|
||||
logger.exception(e)
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
|
||||
|
||||
@router.post("/validate", status_code=200, response_model=ValidationResponse)
|
||||
def post_validate_code(code: Code):
|
||||
try:
|
||||
errors = validate_code(code.code)
|
||||
return ValidationResponse(
|
||||
imports=errors.get("imports", {}),
|
||||
function=errors.get("function", {}),
|
||||
)
|
||||
except Exception as e:
|
||||
return HTTPException(status_code=500, detail=str(e))
|
||||
|
|
|
|||
38
src/backend/langflow/api/validate.py
Normal file
38
src/backend/langflow/api/validate.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from fastapi import HTTPException
|
||||
from langflow.api.base import (
|
||||
Code,
|
||||
CodeValidationResponse,
|
||||
Prompt,
|
||||
PromptValidationResponse,
|
||||
validate_prompt,
|
||||
)
|
||||
|
||||
from langflow.utils.validate import validate_code
|
||||
from langflow.utils.logger import logger
|
||||
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
# build router
|
||||
router = APIRouter(prefix="/validate", tags=["validate"])
|
||||
|
||||
|
||||
@router.post("/code", status_code=200, response_model=CodeValidationResponse)
|
||||
def post_validate_code(code: Code):
|
||||
try:
|
||||
errors = validate_code(code.code)
|
||||
return CodeValidationResponse(
|
||||
imports=errors.get("imports", {}),
|
||||
function=errors.get("function", {}),
|
||||
)
|
||||
except Exception as e:
|
||||
return HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/prompt", status_code=200, response_model=PromptValidationResponse)
|
||||
def post_validate_prompt(prompt: Prompt):
|
||||
try:
|
||||
input_variables, valid = validate_prompt(prompt.template)
|
||||
return PromptValidationResponse(input_variables=input_variables, valid=valid)
|
||||
except Exception as e:
|
||||
return HTTPException(status_code=500, detail=str(e))
|
||||
|
|
@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from langflow.api.endpoints import router as endpoints_router
|
||||
from langflow.api.validate import router as validate_router
|
||||
|
||||
|
||||
def create_app():
|
||||
|
|
@ -21,6 +22,7 @@ def create_app():
|
|||
)
|
||||
|
||||
app.include_router(endpoints_router)
|
||||
app.include_router(validate_router)
|
||||
return app
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,5 +12,5 @@ export async function sendAll(data:sendAllProps) {
|
|||
|
||||
export async function checkCode(code:string):Promise<AxiosResponse<errorsTypeAPI>>{
|
||||
|
||||
return await axios.post('/validate',{code})
|
||||
return await axios.post('/validate/code',{code})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue