🔥 chore(database.py): remove unused code and endpoints related to flows

 feat(router.py): add new routers for flows and flow styles
🔧 refactor(__init__.py): add new routers to __all__ list
🔧 refactor(conftest.py): update import statement for get_session function
The unused code and endpoints related to flows have been removed from the database.py file. New routers for flows and flow styles have been added to the router.py file. The __all__ list in the __init__.py file has been updated to include the new routers. The import statement for the get_session function in the conftest.py file has been updated to reflect the new location of the function.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-06 16:23:37 -03:00
commit 183f80b1fc
4 changed files with 22 additions and 110 deletions

View file

@ -1,106 +0,0 @@
from typing import List
from uuid import UUID
from langflow.api.schemas import FlowListCreate
from langflow.database.models.flow import Flow, FlowCreate, FlowRead
from langflow.database.base import get_session
from sqlmodel import Session, select
from fastapi import APIRouter, Depends, HTTPException
from fastapi import File, UploadFile
import json
# build router
router = APIRouter(prefix="/flows", tags=["Flows"])
@router.post("/", response_model=FlowRead)
def create_flow(*, session: Session = Depends(get_session), flow: FlowCreate):
"""Create a new flow."""
db_flow = Flow.from_orm(flow)
session.add(db_flow)
session.commit()
session.refresh(db_flow)
return db_flow
@router.get("/", response_model=list[FlowRead])
def read_flows(*, session: Session = Depends(get_session)):
"""Read all flows."""
flows = session.exec(select(Flow)).all()
return flows
@router.get("/{flow_id}", response_model=FlowRead)
def read_flow(*, session: Session = Depends(get_session), flow_id: UUID):
"""Read a flow."""
flow = session.get(Flow, flow_id)
if not flow:
raise HTTPException(status_code=404, detail="Flow not found")
return flow
@router.put("/{flow_id}", response_model=FlowRead)
def update_flow(
*, session: Session = Depends(get_session), flow_id: UUID, flow: FlowCreate
):
"""Update a flow."""
db_flow = session.get(Flow, flow_id)
if not db_flow:
raise HTTPException(status_code=404, detail="Flow not found")
update_data = flow.dict(exclude_unset=True)
for key, value in update_data.items():
setattr(db_flow, key, value)
session.add(db_flow)
session.commit()
session.refresh(db_flow)
return db_flow
@router.delete("/{flow_id}")
def delete_flow(*, session: Session = Depends(get_session), flow_id: UUID):
"""Delete a flow."""
flow = session.get(Flow, flow_id)
if not flow:
raise HTTPException(status_code=404, detail="Flow not found")
session.delete(flow)
session.commit()
return {"message": "Flow deleted successfully"}
# Define a new model to handle multiple flows
@router.post("/batch/", response_model=List[FlowRead])
def create_flows(*, session: Session = Depends(get_session), flow_list: FlowListCreate):
"""Create multiple new flows."""
db_flows = []
for flow in flow_list.flows:
db_flow = Flow.from_orm(flow)
session.add(db_flow)
db_flows.append(db_flow)
session.commit()
for db_flow in db_flows:
session.refresh(db_flow)
return db_flows
@router.post("/upload/", response_model=List[FlowRead])
async def upload_file(
*, session: Session = Depends(get_session), file: UploadFile = File(...)
):
"""Upload flows from a file."""
contents = await file.read()
data = json.loads(contents)
if "flows" in data:
flow_list = FlowListCreate(**data)
else:
flow_list = FlowListCreate(flows=[FlowCreate(**flow) for flow in data])
return create_flows(session=session, flow_list=flow_list)
@router.get("/download/")
async def download_file(*, session: Session = Depends(get_session)):
"""Download all flows as a file."""
flows = read_flows(session=session)
return {"file": json.dumps([flow.dict() for flow in flows], default=str)}

View file

@ -1,8 +1,18 @@
# Router for base api
from fastapi import APIRouter
from langflow.api.v1 import chat_router, endpoints_router, validate_router
from langflow.api.v1 import (
chat_router,
endpoints_router,
validate_router,
flows_router,
flow_styles_router,
)
router = APIRouter(prefix="/api/v1", tags=["api"])
router = APIRouter(
prefix="/api/v1",
)
router.include_router(chat_router)
router.include_router(endpoints_router)
router.include_router(validate_router)
router.include_router(flows_router)
router.include_router(flow_styles_router)

View file

@ -1,5 +1,13 @@
from langflow.api.v1.endpoints import router as endpoints_router
from langflow.api.v1.validate import router as validate_router
from langflow.api.v1.chat import router as chat_router
from langflow.api.v1.flows import router as flows_router
from langflow.api.v1.flow_styles import router as flow_styles_router
__all__ = ["chat_router", "endpoints_router", "validate_router"]
__all__ = [
"chat_router",
"endpoints_router",
"validate_router",
"flows_router",
"flow_styles_router",
]

View file

@ -1,7 +1,7 @@
import json
from pathlib import Path
from typing import AsyncGenerator
from langflow.api.database import get_session
from langflow.api.v1.flows import get_session
from langflow.graph.graph.base import Graph
import pytest