This is the main class that manages the whole AI application.
It contains references to all the main modules and is responsible for the bootstrapping of the application.
In most cases you will not need to interact with this class directly, but rather with class StrayCat which will be available in your plugin's hooks, tools, forms end endpoints.
@singletonclassCheshireCat:"""The Cheshire Cat. This is the main class that manages the whole AI application. It contains references to all the main modules and is responsible for the bootstrapping of the application. In most cases you will not need to interact with this class directly, but rather with class `StrayCat` which will be available in your plugin's hooks, tools, forms end endpoints. Attributes ---------- todo : list Yet to be written. """def__init__(self,fastapi_app):"""Cat initialization. At init time the Cat executes the bootstrap. """# bootstrap the Cat! ^._.^# get reference to the FastAPI appself.fastapi_app=fastapi_app# load AuthHandlerself.load_auth()# Start scheduling systemself.white_rabbit=WhiteRabbit()# instantiate MadHatter (loads all plugins' hooks and tools)self.mad_hatter=MadHatter()# allows plugins to do something before cat components are loadedself.mad_hatter.execute_hook("before_cat_bootstrap",cat=self)# load LLM and embedderself.load_natural_language()# Load memories (vector collections and working_memory)self.load_memory()# After memory is loaded, we can get/create tools embeddings self.mad_hatter.on_finish_plugins_sync_callback=self.on_finish_plugins_sync_callback# First time launched manually self.on_finish_plugins_sync_callback()# Main agent instance (for reasoning)self.main_agent=MainAgent()# Rabbit Hole Instanceself.rabbit_hole=RabbitHole(self)# :(# Cache for sessions / working memories et al.self.cache=CacheManager().cache# allows plugins to do something after the cat bootstrap is completeself.mad_hatter.execute_hook("after_cat_bootstrap",cat=self)defload_natural_language(self):"""Load Natural Language related objects. The method exposes in the Cat all the NLP related stuff. Specifically, it sets the language models (LLM and Embedder). Warnings -------- When using small Language Models it is suggested to turn off the memories and make the main prompt smaller to prevent them to fail. See Also -------- agent_prompt_prefix """# LLM and embedderself._llm=self.load_language_model()self.embedder=self.load_language_embedder()defload_language_model(self)->BaseLanguageModel:"""Large Language Model (LLM) selection at bootstrap time. Returns ------- llm : BaseLanguageModel Langchain `BaseLanguageModel` instance of the selected model. Notes ----- Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories, the *Main Agent*, the *Rabbit Hole* and the *White Rabbit*. """selected_llm=crud.get_setting_by_name(name="llm_selected")ifselected_llmisNone:# Return default LLMreturnLLMDefaultConfig.get_llm_from_config({})# Get LLM factory classselected_llm_class=selected_llm["value"]["name"]FactoryClass=get_llm_from_name(selected_llm_class)# Obtain configuration and instantiate LLMselected_llm_config=crud.get_setting_by_name(name=selected_llm_class)try:llm=FactoryClass.get_llm_from_config(selected_llm_config["value"])returnllmexceptException:log.error("Error during LLM instantiation")returnLLMDefaultConfig.get_llm_from_config({})defload_language_embedder(self)->embedders.EmbedderSettings:"""Hook into the embedder selection. Allows to modify how the Cat selects the embedder at bootstrap time. Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories, the *Main Agent*, the *Rabbit Hole* and the *White Rabbit*. Parameters ---------- cat: CheshireCat Cheshire Cat instance. Returns ------- embedder : Embeddings Selected embedder model. """# Embedding LLMselected_embedder=crud.get_setting_by_name(name="embedder_selected")ifselected_embedderisnotNone:# get Embedder factory classselected_embedder_class=selected_embedder["value"]["name"]FactoryClass=get_embedder_from_name(selected_embedder_class)# obtain configuration and instantiate Embedderselected_embedder_config=crud.get_setting_by_name(name=selected_embedder_class)try:embedder=FactoryClass.get_embedder_from_config(selected_embedder_config["value"])exceptException:log.error("Error during Embedder instantiation")embedder=embedders.EmbedderDumbConfig.get_embedder_from_config({})returnembedder# OpenAI embedderiftype(self._llm)in[OpenAI,ChatOpenAI]:embedder=embedders.EmbedderOpenAIConfig.get_embedder_from_config({"openai_api_key":self._llm.openai_api_key,})# For Azure avoid automatic embedder selection# Cohereeliftype(self._llm)in[Cohere]:embedder=embedders.EmbedderCohereConfig.get_embedder_from_config({"cohere_api_key":self._llm.cohere_api_key,"model":"embed-multilingual-v2.0",# Now the best model for embeddings is embed-multilingual-v2.0})eliftype(self._llm)in[ChatGoogleGenerativeAI]:embedder=embedders.EmbedderGeminiChatConfig.get_embedder_from_config({"model":"models/embedding-001","google_api_key":self._llm.google_api_key,})else:# If no embedder matches vendor, and no external embedder is configured, we use the DumbEmbedder.# `This embedder is not a model properly trained# and this makes it not suitable to effectively embed text,# "but it does not know this and embeds anyway".` - cit. Nicola Corbelliniembedder=embedders.EmbedderDumbConfig.get_embedder_from_config({})returnembedderdefload_auth(self):# Custom auth_handler # TODOAUTH: change the name to custom_authselected_auth_handler=crud.get_setting_by_name(name="auth_handler_selected")# if no auth_handler is saved, use default one and save to dbifselected_auth_handlerisNone:# create the auth settingscrud.upsert_setting_by_name(models.Setting(name="CoreOnlyAuthConfig",category="auth_handler_factory",value={}))crud.upsert_setting_by_name(models.Setting(name="auth_handler_selected",category="auth_handler_factory",value={"name":"CoreOnlyAuthConfig"},))# reload from dbselected_auth_handler=crud.get_setting_by_name(name="auth_handler_selected")# get AuthHandler factory classselected_auth_handler_class=selected_auth_handler["value"]["name"]FactoryClass=get_auth_handler_from_name(selected_auth_handler_class)# obtain configuration and instantiate AuthHandlerselected_auth_handler_config=crud.get_setting_by_name(name=selected_auth_handler_class)try:auth_handler=FactoryClass.get_auth_handler_from_config(selected_auth_handler_config["value"])exceptException:log.error("Error during AuthHandler instantiation")auth_handler=(auth_handlers.CoreOnlyAuthConfig.get_auth_handler_from_config({}))self.custom_auth_handler=auth_handlerself.core_auth_handler=CoreAuthHandler()defload_memory(self):"""Load LongTerMemory and WorkingMemory."""# Memory# Get embedder size (langchain classes do not store it)embedder_size=len(self.embedder.embed_query("hello world"))# Get embedder name (useful for for vectorstore aliases)ifhasattr(self.embedder,"model"):embedder_name=self.embedder.modelelifhasattr(self.embedder,"repo_id"):embedder_name=self.embedder.repo_idelse:embedder_name="default_embedder"# instantiate long term memoryvector_memory_config={"embedder_name":embedder_name,"embedder_size":embedder_size,}self.memory=LongTermMemory(vector_memory_config=vector_memory_config)defbuild_embedded_procedures_hashes(self,embedded_procedures):hashes={}forepinembedded_procedures:metadata=ep.payload["metadata"]content=ep.payload["page_content"]source=metadata["source"]# there may be legacy points with no trigger_typetrigger_type=metadata.get("trigger_type","unsupported")p_hash=f"{source}.{trigger_type}.{content}"hashes[p_hash]=ep.idreturnhashesdefbuild_active_procedures_hashes(self,active_procedures):hashes={}forapinactive_procedures:fortrigger_type,trigger_listinap.triggers_map.items():fortrigger_contentintrigger_list:p_hash=f"{ap.name}.{trigger_type}.{trigger_content}"hashes[p_hash]={"obj":ap,"source":ap.name,"type":ap.procedure_type,"trigger_type":trigger_type,"content":trigger_content,}returnhashesdefon_finish_plugins_sync_callback(self):self.activate_endpoints()self.embed_procedures()defactivate_endpoints(self):forendpointinself.mad_hatter.endpoints:ifendpoint.plugin_idinself.mad_hatter.active_plugins:endpoint.activate(self.fastapi_app)defembed_procedures(self):# Retrieve from vectorDB all procedural embeddingsembedded_procedures,_=self.memory.vectors.procedural.get_all_points()embedded_procedures_hashes=self.build_embedded_procedures_hashes(embedded_procedures)# Easy access to active procedures in mad_hatter (source of truth!)active_procedures_hashes=self.build_active_procedures_hashes(self.mad_hatter.procedures)# points_to_be_kept = set(active_procedures_hashes.keys()) and set(embedded_procedures_hashes.keys()) not necessarypoints_to_be_deleted=set(embedded_procedures_hashes.keys())-set(active_procedures_hashes.keys())points_to_be_embedded=set(active_procedures_hashes.keys())-set(embedded_procedures_hashes.keys())points_to_be_deleted_ids=[embedded_procedures_hashes[p]forpinpoints_to_be_deleted]ifpoints_to_be_deleted_ids:log.info("Deleting procedural triggers:")log.info(points_to_be_deleted)self.memory.vectors.procedural.delete_points(points_to_be_deleted_ids)active_triggers_to_be_embedded=[active_procedures_hashes[p]forpinpoints_to_be_embedded]ifactive_triggers_to_be_embedded:log.info("Embedding new procedural triggers:")fortinactive_triggers_to_be_embedded:metadata={"source":t["source"],"type":t["type"],"trigger_type":t["trigger_type"],"when":time.time(),}trigger_embedding=self.embedder.embed_documents([t["content"]])self.memory.vectors.procedural.add_point(t["content"],trigger_embedding[0],metadata,)log.info(f" {t['source']}.{t['trigger_type']}.{t['content']}")defsend_ws_message(self,content:str,msg_type="notification"):log.error("CheshireCat has no websocket connection. Call `send_ws_message` from a StrayCat instance.")# REFACTOR: cat.llm should be available here, without streaming clearly# (one could be interested in calling the LLM anytime, not only when there is a session)defllm(self,prompt,*args,**kwargs)->str:"""Generate a response using the LLM model. This method is useful for generating a response with both a chat and a completion model using the same syntax Parameters ---------- prompt : str The prompt for generating the response. Returns ------- str The generated response. """# Add a token counter to the callbackscaller=utils.get_caller_info()# here we deal with motherfucking langchainprompt=ChatPromptTemplate(messages=[HumanMessage(content=prompt)])chain=(prompt|RunnableLambda(lambdax:utils.langchain_log_prompt(x,f"{caller} prompt"))|self._llm|RunnableLambda(lambdax:utils.langchain_log_output(x,f"{caller} prompt output"))|StrOutputParser())output=chain.invoke({},# in case we need to pass info to the template)returnoutput
def__init__(self,fastapi_app):"""Cat initialization. At init time the Cat executes the bootstrap. """# bootstrap the Cat! ^._.^# get reference to the FastAPI appself.fastapi_app=fastapi_app# load AuthHandlerself.load_auth()# Start scheduling systemself.white_rabbit=WhiteRabbit()# instantiate MadHatter (loads all plugins' hooks and tools)self.mad_hatter=MadHatter()# allows plugins to do something before cat components are loadedself.mad_hatter.execute_hook("before_cat_bootstrap",cat=self)# load LLM and embedderself.load_natural_language()# Load memories (vector collections and working_memory)self.load_memory()# After memory is loaded, we can get/create tools embeddings self.mad_hatter.on_finish_plugins_sync_callback=self.on_finish_plugins_sync_callback# First time launched manually self.on_finish_plugins_sync_callback()# Main agent instance (for reasoning)self.main_agent=MainAgent()# Rabbit Hole Instanceself.rabbit_hole=RabbitHole(self)# :(# Cache for sessions / working memories et al.self.cache=CacheManager().cache# allows plugins to do something after the cat bootstrap is completeself.mad_hatter.execute_hook("after_cat_bootstrap",cat=self)
llm(prompt,*args,**kwargs)
Generate a response using the LLM model.
This method is useful for generating a response with both a chat and a completion model using the same syntax
defllm(self,prompt,*args,**kwargs)->str:"""Generate a response using the LLM model. This method is useful for generating a response with both a chat and a completion model using the same syntax Parameters ---------- prompt : str The prompt for generating the response. Returns ------- str The generated response. """# Add a token counter to the callbackscaller=utils.get_caller_info()# here we deal with motherfucking langchainprompt=ChatPromptTemplate(messages=[HumanMessage(content=prompt)])chain=(prompt|RunnableLambda(lambdax:utils.langchain_log_prompt(x,f"{caller} prompt"))|self._llm|RunnableLambda(lambdax:utils.langchain_log_output(x,f"{caller} prompt output"))|StrOutputParser())output=chain.invoke({},# in case we need to pass info to the template)returnoutput
load_language_embedder()
Hook into the embedder selection.
Allows to modify how the Cat selects the embedder at bootstrap time.
Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories,
the Main Agent, the Rabbit Hole and the White Rabbit.
defload_language_embedder(self)->embedders.EmbedderSettings:"""Hook into the embedder selection. Allows to modify how the Cat selects the embedder at bootstrap time. Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories, the *Main Agent*, the *Rabbit Hole* and the *White Rabbit*. Parameters ---------- cat: CheshireCat Cheshire Cat instance. Returns ------- embedder : Embeddings Selected embedder model. """# Embedding LLMselected_embedder=crud.get_setting_by_name(name="embedder_selected")ifselected_embedderisnotNone:# get Embedder factory classselected_embedder_class=selected_embedder["value"]["name"]FactoryClass=get_embedder_from_name(selected_embedder_class)# obtain configuration and instantiate Embedderselected_embedder_config=crud.get_setting_by_name(name=selected_embedder_class)try:embedder=FactoryClass.get_embedder_from_config(selected_embedder_config["value"])exceptException:log.error("Error during Embedder instantiation")embedder=embedders.EmbedderDumbConfig.get_embedder_from_config({})returnembedder# OpenAI embedderiftype(self._llm)in[OpenAI,ChatOpenAI]:embedder=embedders.EmbedderOpenAIConfig.get_embedder_from_config({"openai_api_key":self._llm.openai_api_key,})# For Azure avoid automatic embedder selection# Cohereeliftype(self._llm)in[Cohere]:embedder=embedders.EmbedderCohereConfig.get_embedder_from_config({"cohere_api_key":self._llm.cohere_api_key,"model":"embed-multilingual-v2.0",# Now the best model for embeddings is embed-multilingual-v2.0})eliftype(self._llm)in[ChatGoogleGenerativeAI]:embedder=embedders.EmbedderGeminiChatConfig.get_embedder_from_config({"model":"models/embedding-001","google_api_key":self._llm.google_api_key,})else:# If no embedder matches vendor, and no external embedder is configured, we use the DumbEmbedder.# `This embedder is not a model properly trained# and this makes it not suitable to effectively embed text,# "but it does not know this and embeds anyway".` - cit. Nicola Corbelliniembedder=embedders.EmbedderDumbConfig.get_embedder_from_config({})returnembedder
load_language_model()
Large Language Model (LLM) selection at bootstrap time.
Returns:
Name
Type
Description
llm
BaseLanguageModel
Langchain BaseLanguageModel instance of the selected model.
Notes
Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories,
the Main Agent, the Rabbit Hole and the White Rabbit.
defload_language_model(self)->BaseLanguageModel:"""Large Language Model (LLM) selection at bootstrap time. Returns ------- llm : BaseLanguageModel Langchain `BaseLanguageModel` instance of the selected model. Notes ----- Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories, the *Main Agent*, the *Rabbit Hole* and the *White Rabbit*. """selected_llm=crud.get_setting_by_name(name="llm_selected")ifselected_llmisNone:# Return default LLMreturnLLMDefaultConfig.get_llm_from_config({})# Get LLM factory classselected_llm_class=selected_llm["value"]["name"]FactoryClass=get_llm_from_name(selected_llm_class)# Obtain configuration and instantiate LLMselected_llm_config=crud.get_setting_by_name(name=selected_llm_class)try:llm=FactoryClass.get_llm_from_config(selected_llm_config["value"])returnllmexceptException:log.error("Error during LLM instantiation")returnLLMDefaultConfig.get_llm_from_config({})
defload_memory(self):"""Load LongTerMemory and WorkingMemory."""# Memory# Get embedder size (langchain classes do not store it)embedder_size=len(self.embedder.embed_query("hello world"))# Get embedder name (useful for for vectorstore aliases)ifhasattr(self.embedder,"model"):embedder_name=self.embedder.modelelifhasattr(self.embedder,"repo_id"):embedder_name=self.embedder.repo_idelse:embedder_name="default_embedder"# instantiate long term memoryvector_memory_config={"embedder_name":embedder_name,"embedder_size":embedder_size,}self.memory=LongTermMemory(vector_memory_config=vector_memory_config)
load_natural_language()
Load Natural Language related objects.
The method exposes in the Cat all the NLP related stuff. Specifically, it sets the language models
(LLM and Embedder).
Warnings
When using small Language Models it is suggested to turn off the memories and make the main prompt smaller
to prevent them to fail.
defload_natural_language(self):"""Load Natural Language related objects. The method exposes in the Cat all the NLP related stuff. Specifically, it sets the language models (LLM and Embedder). Warnings -------- When using small Language Models it is suggested to turn off the memories and make the main prompt smaller to prevent them to fail. See Also -------- agent_prompt_prefix """# LLM and embedderself._llm=self.load_language_model()self.embedder=self.load_language_embedder()