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

Route directly to custom agents #227

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
10 changes: 8 additions & 2 deletions app/modules/conversations/conversation/conversation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ def __init__(self, db, provider_service):
self.classifier = None
self.agents_service = AgentsService(db)
self.agent_factory = AgentFactory(db, provider_service)
self.available_agents = []

async def initialize(self, user_id: str):
available_agents = await self.agents_service.list_available_agents(
current_user={"user_id": user_id}, list_system_agents=True
)

self.available_agents = available_agents
self.agents = {
agent.id: self.agent_factory.get_agent(agent.id, user_id)
for agent in available_agents
}

self.llm = self.provider_service.get_small_llm(user_id)

self.classifier_prompt = """
Expand Down Expand Up @@ -151,13 +151,19 @@ async def classifier_node(self, state: State) -> Command:
"""Classifies the query and routes to appropriate agent"""
if not state.get("query"):
return Command(update={"response": "No query provided"}, goto=END)
agent_list = {agent.id:agent.status for agent in self.available_agents}

#Do not route for custom agents
if state["agent_id"] in agent_list and agent_list[state["agent_id"]] != "SYSTEM":
return Command(update={"agent_id": state["agent_id"]}, goto=state["agent_id"])

Comment on lines +154 to +159
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validate agent_id before using it.

The code should validate that agent_id exists in agent_list before accessing it to prevent potential KeyError. Also, consider improving the comment style.

-        #Do not route for custom agents
-        if state["agent_id"] in agent_list and agent_list[state["agent_id"]] != "SYSTEM":
+        # Do not route for custom agents
+        agent_id = state.get("agent_id")
+        if agent_id and agent_id in agent_list and agent_list[agent_id] != "SYSTEM":
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
agent_list = {agent.id:agent.status for agent in self.available_agents}
#Do not route for custom agents
if state["agent_id"] in agent_list and agent_list[state["agent_id"]] != "SYSTEM":
return Command(update={"agent_id": state["agent_id"]}, goto=state["agent_id"])
agent_list = {agent.id:agent.status for agent in self.available_agents}
# Do not route for custom agents
agent_id = state.get("agent_id")
if agent_id and agent_id in agent_list and agent_list[agent_id] != "SYSTEM":
return Command(update={"agent_id": state["agent_id"]}, goto=state["agent_id"])
🧰 Tools
🪛 GitHub Actions: Pre-commit

[warning] Code formatting issues fixed by Black formatter

# Classification using LLM with enhanced prompt
prompt = self.classifier_prompt.format(
query=state["query"],
agent_id=state["agent_id"],
agent_descriptions=self.agent_descriptions,
)

response = await self.llm.ainvoke(prompt)
response = response.content.strip("`")
try:
Expand Down
Loading