from __future__ import annotations from typing import TYPE_CHECKING from agent.agent import Agent from agent.agent_client import AgentClient from agent.champ_skills_client import ChampSkillsAgentClient from agent.champ_rag_client import ChampRAGAgentClient from agent.fake_client import FakeAgentClient from agent.frontier_client import ( GEMINI_CONSERVATIVE_TEMPERATURE, GEMINI_CREATIVE_TEMPERATURE, GeminiAgentClient, GptAgentClient, ) from agent.marvin_connect_skills_client import MarvinConnectSkillsAgentClient from constants import LOCATIONS, SITE_LOCALES from providers.protocol import ChatProvider if TYPE_CHECKING: from langchain_community.vectorstores import FAISS as LCFAISS class AgentClientFactory: """Creates the client that answers a conversation, from the providers built at startup.""" def __init__( self, wiki_skills_agent: Agent, champ_provider: ChatProvider | None, champ_vector_store: LCFAISS | None, openai_provider: ChatProvider, openai_model_id: str, gemini_provider: ChatProvider, gemini_model_id: str, fake_provider: ChatProvider, marvin_connect_agent: Agent | None = None, marvin_connect_locale: tuple[str, str | None, str | None] = ("canada", "quebec", "montreal"), ) -> None: """Keep the providers and agents each model type's client is built from. Args: wiki_skills_agent (Agent): CHAMP's skills agent. champ_provider (ChatProvider | None): The RAG model type's provider; None when it is off. champ_vector_store (LCFAISS | None): The RAG model type's store; None when it is off. openai_provider (ChatProvider): The OpenAI model type's provider. openai_model_id (str): The OpenAI model. gemini_provider (ChatProvider): The two Gemini model types' provider. gemini_model_id (str): The Gemini model. fake_provider (ChatProvider): The fake model type's provider. marvin_connect_agent (Agent | None): MARVIN-CONNECT's skills agent; None leaves it unserved. marvin_connect_locale (tuple[str, str | None, str | None]): Its country, province and site. Raises: ValueError: When MARVIN-CONNECT is served and its locale is not one in LOCATIONS. """ country, province, site = marvin_connect_locale if marvin_connect_agent is not None and ( country not in LOCATIONS or (province is not None and province not in LOCATIONS[country]) or (site is not None and (province is None or site not in LOCATIONS[country][province])) ): raise ValueError(f"MARVIN-CONNECT's locale is not a supported one: {marvin_connect_locale}") self.marvin_connect_agent = marvin_connect_agent self.marvin_connect_locale = marvin_connect_locale self.wiki_skills_agent = wiki_skills_agent self.champ_provider = champ_provider self.champ_vector_store = champ_vector_store self.openai_provider = openai_provider self.openai_model_id = openai_model_id self.gemini_provider = gemini_provider self.gemini_model_id = gemini_model_id self.fake_provider = fake_provider def create(self, model_type: str, site: str | None = None) -> AgentClient: """Create a new client for a model type, with an empty history. Args: model_type (str): The conversation's model type. site (str | None): MARVIN-CONNECT's site; None uses the deployment's locale. Raises: RuntimeError: When the model type has no client, or its agent or store is off. KeyError: When the site is not one in LOCATIONS. Returns: AgentClient: The new client. """ if model_type == "skills_wiki": return ChampSkillsAgentClient(agent=self.wiki_skills_agent) if model_type == "marvin_connect": if self.marvin_connect_agent is None: raise RuntimeError("the model type 'marvin_connect' is not served here") locale = SITE_LOCALES[site] if site is not None else self.marvin_connect_locale return MarvinConnectSkillsAgentClient(self.marvin_connect_agent, *locale) if model_type == "champ": # Requests are refused before this; a router built with the RAG model off has no store. if self.champ_vector_store is None: raise RuntimeError( "the RAG model type 'champ' is turned off (CHAMP_RAG_ENABLED is not true)" ) return ChampRAGAgentClient( provider=self.champ_provider, vector_store=self.champ_vector_store, ) if model_type == "openai": return GptAgentClient( provider=self.openai_provider, model_id=self.openai_model_id, ) if model_type == "google-conservative": return GeminiAgentClient( provider=self.gemini_provider, model_id=self.gemini_model_id, temperature=GEMINI_CONSERVATIVE_TEMPERATURE, ) if model_type == "google-creative": return GeminiAgentClient( provider=self.gemini_provider, model_id=self.gemini_model_id, temperature=GEMINI_CREATIVE_TEMPERATURE, ) if model_type == "fake": return FakeAgentClient(provider=self.fake_provider) raise RuntimeError(f"Unknown model_type: {model_type}")