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/new app type workflow #38

Merged
merged 16 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions backend/app/alembic/versions/b5d6291d6db9_add_graph_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""add graph table

Revision ID: b5d6291d6db9
Revises: f3e7a75611f1
Create Date: 2024-09-21 11:55:41.298045

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'b5d6291d6db9'
down_revision = 'f3e7a75611f1'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('graph',
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
sa.Column('config', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('metadata', postgresql.JSONB(astext_type=sa.Text()), server_default='{}', nullable=False),
sa.Column('owner_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('team_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ),
sa.ForeignKeyConstraint(['team_id'], ['team.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.alter_column('user', 'language',
existing_type=sa.VARCHAR(),
nullable=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('user', 'language',
existing_type=sa.VARCHAR(),
nullable=True)
op.drop_table('graph')
# ### end Alembic commands ###
3 changes: 3 additions & 0 deletions backend/app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
utils,
provider,
providermodel,
graphs
)



api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
Expand All @@ -32,3 +34,4 @@
api_router.include_router(provider.router, prefix="/provider", tags=["provider"])

api_router.include_router(providermodel.router, prefix="/model", tags=["model"])
api_router.include_router(graphs.router, prefix="/teams/{team_id}/graphs", tags=["graphs"])
180 changes: 180 additions & 0 deletions backend/app/api/routes/graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
from typing import Any

from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import col, func, select

from app.api.deps import CurrentUser, SessionDep
from app.models import (
Graph,
GraphCreate,
GraphOut,
GraphsOut,
GraphUpdate,
Team,
)

router = APIRouter()


async def validate_name_on_create(session: SessionDep, graph_in: GraphCreate) -> None:
"""Validate that graph name is unique"""
statement = select(Graph).where(Graph.name == graph_in.name)
graph = session.exec(statement).first()
if graph:
raise HTTPException(status_code=400, detail="Graph name already exists")


async def validate_name_on_update(
session: SessionDep, graph_in: GraphUpdate, id: int
) -> None:
"""Validate that graph name is unique"""
statement = select(Graph).where(Graph.name == graph_in.name, Graph.id != id)
graph = session.exec(statement).first()
if graph:
raise HTTPException(status_code=400, detail="Graph name already exists")


@router.get("/", response_model=GraphsOut)
def read_graphs(
session: SessionDep,
current_user: CurrentUser,
team_id: int,
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve graphs from team.
"""
if current_user.is_superuser:
count_statement = select(func.count()).select_from(Graph)
count = session.exec(count_statement).one()
statement = (
select(Graph).where(Graph.team_id == team_id).offset(skip).limit(limit)
)
graphs = session.exec(statement).all()
else:
count_statement = (
select(func.count())
.select_from(Graph)
.join(Team)
.where(Team.owner_id == current_user.id, Graph.team_id == team_id)
)
count = session.exec(count_statement).one()
statement = (
select(Graph)
.join(Team)
.where(Team.owner_id == current_user.id, Graph.team_id == team_id)
.offset(skip)
.limit(limit)
)
graphs = session.exec(statement).all()

return GraphsOut(data=graphs, count=count)


@router.get("/{id}", response_model=GraphOut)
def read_graph(
session: SessionDep,
current_user: CurrentUser,
team_id: int,
id: int,
) -> Any:
"""
Get graph by ID.
"""
if current_user.is_superuser:
statement = select(Graph).where(Graph.id == id, Graph.team_id == team_id)
graph = session.exec(statement).first()
else:
statement = (
select(Graph)
.join(Team)
.where(
Graph.id == id,
Graph.team_id == team_id,
Team.owner_id == current_user.id,
)
)
graph = session.exec(statement).first()

if not graph:
raise HTTPException(status_code=404, detail="Graph not found")
return graph


@router.post("/", response_model=GraphOut)
def create_graph(
*,
session: SessionDep,
current_user: CurrentUser,
team_id: int,
graph_in: GraphCreate,
_: bool = Depends(validate_name_on_create),
) -> Any:
"""
Create new graph.
"""
if not current_user.is_superuser:
team = session.get(Team, team_id)
if not team:
raise HTTPException(status_code=404, detail="Team not found.")
if team.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
graph = Graph.model_validate(
graph_in, update={"team_id": team_id, "owner_id": current_user.id}
)
session.add(graph)
session.commit()
session.refresh(graph)
return graph


@router.put("/{id}", response_model=GraphOut)
def update_graph(
*,
session: SessionDep,
current_user: CurrentUser,
team_id: int,
id: int,
graph_in: GraphUpdate,
) -> Any:
"""
Update graph by ID.
"""

if not current_user.is_superuser:
team = session.get(Team, team_id)
if not team:
raise HTTPException(status_code=404, detail="Team not found.")
if team.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
graph = session.get(Graph, id)
if not graph:
raise HTTPException(status_code=404, detail="Graph not found")
graph.sqlmodel_update(graph_in)
session.commit()
session.refresh(graph)
return graph


@router.delete("/{id}")
def delete_graph(
session: SessionDep,
current_user: CurrentUser,
team_id: int,
id: int,
) -> None:
"""
Delete graph by ID.
"""
if not current_user.is_superuser:
team = session.get(Team, team_id)
if not team:
raise HTTPException(status_code=404, detail="Team not found.")
if team.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
graph = session.get(Graph, id)
if not graph:
raise HTTPException(status_code=404, detail="Graph not found")
session.delete(graph)
session.commit()
23 changes: 21 additions & 2 deletions backend/app/api/routes/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def create_team(
Create new team and it's team leader
"""
team = Team.model_validate(team_in, update={"owner_id": current_user.id})
if team.workflow not in ["hierarchical", "sequential", "chatbot", "ragbot"]:
if team.workflow not in [
"hierarchical",
"sequential",
"chatbot",
"ragbot",
"workflow",
]:
raise HTTPException(status_code=400, detail="Invalid workflow")
session.add(team)
session.commit()
Expand Down Expand Up @@ -145,6 +151,17 @@ def create_team(
position_y=0,
belongs_to=team.id,
)
elif team.workflow == "workflow":
# Create a freelancer head
member = Member(
name="Workflow",
type="workflow",
role="Answer the user's question.",
owner_of=None,
position_x=0,
position_y=0,
belongs_to=team.id,
)
else:
raise ValueError("Unsupported graph type")
session.add(member)
Expand Down Expand Up @@ -225,7 +242,9 @@ async def stream(
for member in members:
member.skills = member.skills
member.uploads = member.uploads

graphs = team.graphs
for graph in graphs:
graph.config = graph.config
return StreamingResponse(
generator(team, members, team_chat.messages, thread_id, team_chat.interrupt),
media_type="text/event-stream",
Expand Down
32 changes: 18 additions & 14 deletions backend/app/core/graph/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
config_sequential_with_tools,
config_hierarchical,
config_n_new,
new_config,
)


Expand Down Expand Up @@ -234,9 +235,9 @@ def convert_chatbot_chatrag_team_to_dict(

member = members[0]
assert member.id is not None, "member.id is unexpectedly None"

if workflow_type == "ragbot":
tools: list[GraphUpload] = [
tools: list[GraphSkill | GraphUpload]
if workflow_type == "ragbot" or workflow_type == "chatbot":
tools = [
GraphUpload(
name=upload.name,
description=upload.description,
Expand All @@ -247,7 +248,7 @@ def convert_chatbot_chatrag_team_to_dict(
if upload.owner_id is not None
]
elif workflow_type == "chatbot":
tools: list[GraphSkill] = [
tools += [
GraphSkill(
name=skill.name,
managed=skill.managed,
Expand Down Expand Up @@ -720,16 +721,7 @@ async def generator(
member_dict = convert_chatbot_chatrag_team_to_dict(
members, workflow_type=team.workflow
)

# root = create_chatbot_ragbot_graph(member_dict, checkpointer)

# config = config_with_2_tool_router
# config = config_hierarchical
# config =config_n_new
# config = config_with_no_tools
# config = config_with_3_llm
config = config_sequential_with_tools
root = initialize_graph(config, checkpointer,save_graph_img=False)
root = create_chatbot_ragbot_graph(member_dict, checkpointer)
first_member = list(member_dict.values())[0]
state = {
"history": formatted_messages,
Expand Down Expand Up @@ -773,6 +765,18 @@ async def generator(
"next": first_member.name,
"all_messages": formatted_messages,
}
elif team.workflow in ["workflow"]:

# config = config_with_2_tool_router
config = team.graphs[0].config

root = initialize_graph(config, checkpointer, save_graph_img=False)

state = {
"history": formatted_messages,
"messages": [],
"all_messages": formatted_messages,
}
else:
raise ValueError("Unsupported graph type ")

Expand Down
Loading
Loading