Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
holchan committed Dec 28, 2024
1 parent 6c3070b commit 7610b05
Show file tree
Hide file tree
Showing 4 changed files with 645 additions and 389 deletions.
4 changes: 3 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ POSTHOG_API_KEY=
POSTHOG_HOST=
POTPIE_PLUS_BASE_URL=http://localhost:8080
POTPIE_PLUS_HMAC_KEY=123
GOOGLE_API_KEY=
GOOGLE_API_KEY=
MAX_REQUESTS_PER_MINUTE=
MAX_CONCURRENT_REQUESTS=
91 changes: 71 additions & 20 deletions app/modules/intelligence/agents/agent_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
from typing import Any, Dict

from sqlalchemy.orm import Session

from app.modules.intelligence.agents.chat_agents.code_changes_chat_agent import (
Expand All @@ -24,34 +25,77 @@
AgentType,
ProviderService,
)
from app.modules.utils.rate_limiter import RateLimiter

# Configure logging
logger = logging.getLogger(__name__)

class AgentFactory:
def __init__(self, db: Session, provider_service: ProviderService):
"""Initialize AgentFactory with database session and provider service."""
logger.info("Initializing AgentFactory")
self.db = db
self.provider_service = provider_service
self._agent_cache: Dict[str, Any] = {}

# Initialize rate limiter
self.rate_limiter = RateLimiter("agent")
logger.debug("Rate limiter initialized for agent factory")

def get_agent(self, agent_id: str, user_id: str) -> Any:
"""Get or create an agent instance"""
cache_key = f"{agent_id}_{user_id}"
async def get_agent(self, agent_id: str, user_id: str) -> Any:
"""
Get or create an agent instance.
Args:
agent_id (str): The identifier for the agent type
user_id (str): The user's identifier
Returns:
Any: The requested agent instance
"""
logger.info(f"Getting agent for agent_id: {agent_id}, user_id: {user_id}")

try:
await self.rate_limiter.acquire()
cache_key = f"{agent_id}_{user_id}"

if cache_key in self._agent_cache:
return self._agent_cache[cache_key]
if cache_key in self._agent_cache:
logger.debug(f"Retrieved agent from cache for key: {cache_key}")
return self._agent_cache[cache_key]

mini_llm = self.provider_service.get_small_llm(agent_type=AgentType.LANGCHAIN)
reasoning_llm = self.provider_service.get_large_llm(
agent_type=AgentType.LANGCHAIN
)
logger.debug("Initializing LLMs for new agent")
mini_llm = await self.provider_service.get_small_llm(agent_type=AgentType.LANGCHAIN)
reasoning_llm = await self.provider_service.get_large_llm(
agent_type=AgentType.LANGCHAIN
)

agent = self._create_agent(agent_id, mini_llm, reasoning_llm, user_id)
self._agent_cache[cache_key] = agent
return agent
logger.debug(f"Creating new agent instance for agent_id: {agent_id}")
agent = self._create_agent(agent_id, mini_llm, reasoning_llm, user_id)
self._agent_cache[cache_key] = agent
logger.info(f"Successfully created and cached agent for key: {cache_key}")
return agent

except Exception as e:
logger.error(f"Error getting agent: {str(e)}", exc_info=True)
raise

def _create_agent(
self, agent_id: str, mini_llm, reasoning_llm, user_id: str
) -> Any:
"""Create a new agent instance"""
"""
Create a new agent instance based on the agent_id.
Args:
agent_id (str): The identifier for the agent type
mini_llm: The small language model instance
reasoning_llm: The large language model instance
user_id (str): The user's identifier
Returns:
Any: The created agent instance
"""
logger.debug(f"Creating agent with id: {agent_id}")

agent_map = {
"debugging_agent": lambda: DebuggingChatAgent(
mini_llm, reasoning_llm, self.db
Expand All @@ -72,10 +116,17 @@ def _create_agent(
),
}

if agent_id in agent_map:
return agent_map[agent_id]()
try:
if agent_id in agent_map:
logger.info(f"Creating system agent: {agent_id}")
return agent_map[agent_id]()

# If not a system agent, create custom agent
return CustomAgent(
llm=reasoning_llm, db=self.db, agent_id=agent_id, user_id=user_id
)
# If not a system agent, create custom agent
logger.info(f"Creating custom agent for agent_id: {agent_id}")
return CustomAgent(
llm=reasoning_llm, db=self.db, agent_id=agent_id, user_id=user_id
)

except Exception as e:
logger.error(f"Error creating agent {agent_id}: {str(e)}", exc_info=True)
raise
Loading

0 comments on commit 7610b05

Please sign in to comment.