Skip to content

Commit

Permalink
feat: create voice agent from template route
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Dec 12, 2023
1 parent f4298c3 commit 3b7a2c6
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 64 deletions.
57 changes: 30 additions & 27 deletions apps/server/controllers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from exceptions import AgentNotFoundException
# Local application imports
from models.agent import AgentModel
from typings.agent import AgentConfigInput, AgentWithConfigsOutput
from typings.agent import (AgentConfigInput, AgentWithConfigsOutput,
CreateVoiceAgentInput)
from typings.auth import UserAccount
from utils.agent import convert_agents_to_agent_list, convert_model_to_response
from utils.auth import authenticate, authenticate_by_token_or_api_key
Expand Down Expand Up @@ -46,32 +47,34 @@ 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(
# agent_with_configs: AgentConfigInput,
# 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.
# """

# db_agent = AgentModel.create_agent(
# db,
# agent=agent_with_configs.agent,
# configs=agent_with_configs.configs,
# user=auth.user,
# account=auth.account,
# )
# 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(
Expand Down
52 changes: 52 additions & 0 deletions apps/server/models/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,58 @@ def create_agent_from_template(

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,
check_is_template=check_is_template,
)

return new_agent

@classmethod
def get_agents(cls, db, account):
agents = (
Expand Down
68 changes: 66 additions & 2 deletions apps/server/models/agent_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sqlalchemy import Column, Text, String, UUID, ForeignKey, Index
import uuid

from sqlalchemy import UUID, Column, ForeignKey, Index, String, Text
from sqlalchemy.orm import relationship

from models.base_model import BaseModel
import uuid
from typings.agent import ConfigInput


Expand Down Expand Up @@ -126,3 +127,66 @@ def create_configs_from_template(cls, db, configs, user, agent_id):
db.session.commit()

return changes

@classmethod
def create_voice_configs_from_template(
cls, db, configs, user, account, agent_id, check_is_template
):
"""
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.
"""
import ast

from models.agent import AgentModel

changes = []
for config in configs:
new_config = AgentConfigModel(
key=config.key,
value=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)

if runner_id:
runner_model = AgentModel.create_agent_from_template(
db, runner_id, user, account, check_is_template
)

runner["runner"] = str(runner_model.id)

new_config.value = str(runner)
elif new_config.key == "runners":
runners = ast.literal_eval(new_config.value)

for runner in runners:
runner_id = runner.get("runner", None)

if runner_id:
runner_model = AgentModel.create_agent_from_template(
db, runner_id, user, account, check_is_template
)
runner["runner"] = str(runner_model.id)

new_config.value = str(runners)

changes.append(new_config)

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

return changes
37 changes: 2 additions & 35 deletions apps/server/typings/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,8 @@ class AgentConfigInput(BaseModel):
configs: ConfigInput


class VoiceAgentInput(BaseModel):
name: str
description: Optional[str]
agent_type: Optional[str]
workspace_id: Optional[UUID4]
role: Optional[str]
is_memory: Optional[bool]
avatar: Optional[str]
is_template: bool


class VoiceConfigInput(BaseModel):
goals: List[str]
constraints: List[str]
tools: List[str]
datasources: List[str]
model: Optional[str]
temperature: float
instructions: List[str]
suggestions: Optional[List[str]]
greeting: Optional[str]
text: Optional[str]
integrations: Optional[List[dict]]
source_flow: Optional[str]
synthesizer: Optional[str]
default_voice: Optional[str]
voice_id: Optional[str]
transcriber: Optional[str]
response_mode: Optional[List[str]]
input_mode: Optional[List[str]]


class VoiceAgentConfigInput(BaseModel):
agent: AgentInput
configs: ConfigInput
class CreateVoiceAgentInput(BaseModel):
template_id: UUID4


class ConfigsOutput(BaseModel):
Expand Down

0 comments on commit 3b7a2c6

Please sign in to comment.