Skip to content

Commit

Permalink
Merge pull request #383 from l3vels/fix/apiDocs
Browse files Browse the repository at this point in the history
Fix/api docs
  • Loading branch information
Chkhikvadze authored Dec 18, 2023
2 parents d708da7 + fcfcc59 commit c38f52f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 142 deletions.
60 changes: 22 additions & 38 deletions apps/server/controllers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

router = APIRouter()

agent_customer_router = APIRouter()


@router.post("", status_code=201, response_model=AgentWithConfigsOutput)
def create_agent(
Expand All @@ -47,36 +45,6 @@ def create_agent(
return convert_model_to_response(AgentModel.get_agent_by_id(db, db_agent.id))


@agent_customer_router.post(
"/voice", status_code=201, response_model=AgentWithConfigsOutput
)
def create_voice_agent(
voice_agent_input: CreateVoiceAgentInput,
auth: UserAccount = Depends(authenticate_by_token_or_api_key),
) -> AgentWithConfigsOutput:
"""
Create a new agent with configurations.
Args:
agent_with_configs (AgentConfigInput): Data for creating a new agent with configurations.
auth (UserAccount): Authenticated user account.
Returns:
AgentWithConfigsOutput: Created agent object.
"""

agent_template_id = voice_agent_input.template_id

try:
agent = AgentModel.create_voice_agent_from_template(
db, agent_template_id, auth.user, auth.account, True
)

return convert_model_to_response(agent)
except AgentNotFoundException:
raise HTTPException(status_code=404, detail="Template agent not found")


@router.put(
"/{id}", status_code=200, response_model=AgentWithConfigsOutput
) # Changed status code to 200
Expand Down Expand Up @@ -118,7 +86,7 @@ def update_agent(
response_model=AgentWithConfigsOutput,
) # Changed status code to 200
def create_agent_from_template(
template_id: str, auth: UserAccount = Depends(authenticate)
template_id: str, auth: UserAccount = Depends(authenticate_by_token_or_api_key)
) -> AgentWithConfigsOutput:
"""
Update an existing agent with configurations.
Expand Down Expand Up @@ -165,7 +133,11 @@ def get_agents(


# todo need remove, is depricated
@router.get("/discover", response_model=Dict[str, List[AgentWithConfigsOutput]])
@router.get(
"/discover",
response_model=Dict[str, List[AgentWithConfigsOutput]],
include_in_schema=False,
)
def get_template_and_system_agents() -> Dict[str, List[AgentWithConfigsOutput]]:
template_agents = AgentModel.get_template_agents(
session=db.session, account_id=None
Expand All @@ -183,14 +155,22 @@ def get_template_and_system_agents() -> Dict[str, List[AgentWithConfigsOutput]]:
return result


@router.get("/discover/public", response_model=List[AgentWithConfigsOutput])
@router.get(
"/discover/public",
response_model=List[AgentWithConfigsOutput],
include_in_schema=False,
)
def get_public_agents() -> Dict[str, List[AgentWithConfigsOutput]]:
public_agents = AgentModel.get_public_agents(db=db)

return convert_agents_to_agent_list(public_agents)


@router.get("/discover/templates", response_model=List[AgentWithConfigsOutput])
@router.get(
"/discover/templates",
response_model=List[AgentWithConfigsOutput],
include_in_schema=False,
)
def get_template_agents(
auth: UserAccount = Depends(authenticate),
) -> Dict[str, List[AgentWithConfigsOutput]]:
Expand All @@ -201,7 +181,9 @@ def get_template_agents(


@router.get(
"/from-template/is-created/{parent_id}", response_model=AgentWithConfigsOutput
"/from-template/is-created/{parent_id}",
response_model=AgentWithConfigsOutput,
include_in_schema=False,
)
def get_agent_by_parent_id(
parent_id: str, auth: UserAccount = Depends(authenticate)
Expand Down Expand Up @@ -257,7 +239,9 @@ def get_agent_by_id(
return agent_with_configs


@router.get("/discover/{id}", response_model=AgentWithConfigsOutput)
@router.get(
"/discover/{id}", response_model=AgentWithConfigsOutput, include_in_schema=False
)
def get_discover_agent_by_id(id: str) -> AgentWithConfigsOutput:
"""
Get an agent by its ID.
Expand Down
18 changes: 10 additions & 8 deletions apps/server/controllers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
router = APIRouter()


@router.post("", status_code=201)
@router.post("", status_code=201, include_in_schema=False)
def create_chat(
chat: ChatInput, auth: UserAccount = Depends(authenticate_by_token_or_api_key)
) -> ChatOutput:
Expand All @@ -41,7 +41,7 @@ def create_chat(
return convert_model_to_response(db_chat)


@router.patch("/{id}", status_code=200)
@router.patch("/{id}", status_code=200, include_in_schema=False)
def update_chat(
id: UUID,
chat: UpdateChatInput,
Expand All @@ -66,7 +66,7 @@ def get_chats(auth: UserAccount = Depends(authenticate)) -> List[ChatOutput]:
return convert_chats_to_chat_list(db_chats)


@router.post("/messages", status_code=201)
@router.post("/messages", status_code=201, include_in_schema=False)
def create_user_chat_message(
body: ChatUserMessageInput, auth: UserAccount = Depends(authenticate)
):
Expand All @@ -78,7 +78,9 @@ def create_user_chat_message(
return ""


@router.post("/stop", status_code=201, response_model=ConfigOutput)
@router.post(
"/stop", status_code=201, response_model=ConfigOutput, include_in_schema=False
)
def stop_run(body: ChatStopInput, auth: UserAccount = Depends(authenticate)):
session_id = get_chat_session_id(
auth.user.id, auth.account.id, body.agent_id, body.team_id
Expand Down Expand Up @@ -200,7 +202,7 @@ def get_history(agent_id: Optional[UUID] = None, team_id: Optional[UUID] = None)
return chat_messages


@router.get("/negotiate", response_model=NegotiateOutput)
@router.get("/negotiate", response_model=NegotiateOutput, include_in_schema=False)
def negotiate(id: str):
"""
Get Azure PubSub url with access token
Expand All @@ -217,7 +219,7 @@ def negotiate(id: str):
return NegotiateOutput(url=token["url"])


@router.post("/session/messages/insert", status_code=201)
@router.post("/session/messages/insert", status_code=201, include_in_schema=False)
def insert_chat_messages(
body: InsertChatMessagesInput,
auth: UserAccount = Depends(authenticate_by_token_or_api_key),
Expand Down Expand Up @@ -271,7 +273,7 @@ def insert_chat_messages(
return ""


@router.post("/session/messages", status_code=201)
@router.post("/session/messages", status_code=201, include_in_schema=False)
def create_chat_message(request: Request, response: Response, body: ChatMessageInput):
"""
Create new chat message
Expand All @@ -281,7 +283,7 @@ def create_chat_message(request: Request, response: Response, body: ChatMessageI
return create_client_message(body, auth)


@router.post("/session/messages/draft", status_code=201)
@router.post("/session/messages/draft", status_code=201, include_in_schema=False)
def create_chat_message_draft(
request: Request, response: Response, body: ChatMessageInput
):
Expand Down
8 changes: 4 additions & 4 deletions apps/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from strawberry.fastapi import GraphQLRouter

from config import Config
from controllers.agent import agent_customer_router
from controllers.agent import router as agent_router
from controllers.api_key import router as api_key_router
from controllers.auth import router as user_router
Expand Down Expand Up @@ -102,14 +101,15 @@ def jwt_exception_handler(request: Request, exc: AuthJWTException):
app.include_router(workspace_router, prefix="/workspace", include_in_schema=False)
app.include_router(team_router, prefix="/team", include_in_schema=False)
app.include_router(team_agent_router, prefix="/team-of-agents", include_in_schema=False)
app.include_router(agent_router, prefix="/agent", include_in_schema=False)
app.include_router(agent_customer_router, prefix="/v1/agent")
app.include_router(agent_router, prefix="/agent", include_in_schema=True)
# app.include_router(agent_customer_router, prefix="/v1/agent")
# app.include_router(chat_customer_router, prefix="/v1/chat")
app.include_router(config_router, prefix="/config", include_in_schema=False)
app.include_router(datasource_router, prefix="/datasource", include_in_schema=False)
app.include_router(run_router, prefix="/run", include_in_schema=False)
app.include_router(tool_router, prefix="/tool", include_in_schema=False)
app.include_router(llm_router, prefix="/llm", include_in_schema=False)
app.include_router(chat_router, prefix="/chat", include_in_schema=False)
app.include_router(chat_router, prefix="/chat", include_in_schema=True)
app.include_router(file_router, prefix="/file", include_in_schema=False)
app.include_router(model_router, prefix="/model", include_in_schema=False)
app.include_router(schedule_router, prefix="/schedule", include_in_schema=False)
Expand Down
49 changes: 1 addition & 48 deletions apps/server/models/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,58 +189,11 @@ def create_agent_from_template(
db.session.commit()

AgentConfigModel.create_configs_from_template(
db=db, configs=template_agent.configs, user=user, agent_id=new_agent.id
)

return new_agent

@classmethod
def create_voice_agent_from_template(
cls, db, template_id, user, account, check_is_template: True
):
"""
Creates a new agent with the provided configuration.
Args:
db: The database object.
agent_with_config: The object containing the agent and configuration details.
Returns:
Agent: The crated agent.
"""
template_agent = cls.get_agent_by_id(db=db, agent_id=template_id)
if check_is_template:
if template_agent is None or not (
template_agent.is_public or template_agent.is_template
):
raise AgentNotFoundException("Agent not found")

new_agent = AgentModel(
name=template_agent.name,
role=template_agent.role,
agent_type=template_agent.agent_type,
description=template_agent.description,
is_memory=template_agent.is_memory,
is_public=False,
is_template=False,
created_by=user.id,
account_id=account.id,
modified_by=None,
parent_id=template_agent.id,
avatar=template_agent.avatar,
)

db.session.add(new_agent)
db.session.commit()
db.session.flush()

AgentConfigModel.create_voice_configs_from_template(
db=db,
configs=template_agent.configs,
user=user,
agent_id=new_agent.id,
account=account,
agent_id=new_agent.id,
check_is_template=check_is_template,
)

Expand Down
41 changes: 6 additions & 35 deletions apps/server/models/agent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,7 @@ def create_or_update(cls, db, agent, update_configs, user, account):
return changes

@classmethod
def create_configs_from_template(cls, db, configs, user, agent_id):
"""
Create or update agent configurations in the database.
Args:
db (Session): The database session.
configs (list): The list of configurations.
user (UserModel): The user object.
agent_id (UUID): The agent id.
Returns:
List[AgentConfigModel]: The list of created or updated configurations.
"""
changes = []
for template_config in configs:
new_config = AgentConfigModel(
key=template_config.key,
value=template_config.value,
agent_id=agent_id,
created_by=user.id,
)
changes.append(new_config)

db.session.add_all(changes)
db.session.commit()

return changes

@classmethod
def create_voice_configs_from_template(
def create_configs_from_template(
cls, db, configs, user, account, agent_id, check_is_template
):
"""
Expand All @@ -144,20 +115,20 @@ def create_voice_configs_from_template(
Returns:
List[AgentConfigModel]: The list of created or updated configurations.
"""

import ast

from models.agent import AgentModel

changes = []
for config in configs:
for template_config in configs:
new_config = AgentConfigModel(
key=config.key,
value=config.value,
key=template_config.key,
value=template_config.value,
agent_id=agent_id,
created_by=user.id,
)

# Copy sentiment analyzer and runner agents from template
if new_config.key == "sentiment_analyzer":
runner = ast.literal_eval(new_config.value)
runner_id = runner.get("runner", None)
Expand All @@ -184,7 +155,7 @@ def create_voice_configs_from_template(

new_config.value = str(runners)

changes.append(new_config)
changes.append(new_config)

db.session.add_all(changes)
db.session.commit()
Expand Down
Loading

0 comments on commit c38f52f

Please sign in to comment.