Skip to content

Commit

Permalink
update log commands to new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
pieroit committed Sep 15, 2023
1 parent 9369a31 commit 24732d2
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 53 deletions.
4 changes: 2 additions & 2 deletions core/cat/looking_glass/agent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def execute_agent(self, agent_input):
# Try to get information from tools if there is some allowed
if len(allowed_tools) > 0:

log(f"{len(allowed_tools)} allowed tools retrived.", "DEBUG")
log.debug(f"{len(allowed_tools)} allowed tools retrived.")

try:
tools_result = self.execute_tool_agent(agent_input, allowed_tools)
Expand Down Expand Up @@ -150,7 +150,7 @@ def execute_agent(self, agent_input):

except Exception as e:
error_description = str(e)
log(error_description, "ERROR")
log.error(error_description)

#If an exeption occur in the execute_tool_agent or there is no allowed tools execute only the memory chain

Expand Down
12 changes: 6 additions & 6 deletions core/cat/looking_glass/cheshire_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def recall_relevant_memories_to_working_memory(self):

# We may want to search in memory
memory_query_text = self.mad_hatter.execute_hook("cat_recall_query", user_message)
log(f'Recall query: "{memory_query_text}"')
log.info(f'Recall query: "{memory_query_text}"')

# Embed recall query
memory_query_embedding = self.embedder.embed_query(memory_query_text)
Expand Down Expand Up @@ -315,7 +315,7 @@ def __call__(self, user_message_json):
answer. This is formatted in a dictionary to be sent as a JSON via Websocket to the client.
"""
log(user_message_json, "INFO")
log.info(user_message_json)

# Change working memory based on received user_id
user_id = user_message_json.get('user_id', 'user')
Expand All @@ -333,7 +333,7 @@ def __call__(self, user_message_json):
try:
self.recall_relevant_memories_to_working_memory()
except Exception as e:
log(e, "ERROR")
log.error(e)
traceback.print_exc(e)

err_message = (
Expand Down Expand Up @@ -361,7 +361,7 @@ def __call__(self, user_message_json):
# non instruction-fine-tuned models can still be used.
error_description = str(e)

log(error_description, "ERROR")
log.error(error_description)
if not "Could not parse LLM output: `" in error_description:
raise e

Expand All @@ -372,8 +372,8 @@ def __call__(self, user_message_json):
"output": unparsable_llm_output
}

log("cat_message:", "DEBUG")
log(cat_message, "DEBUG")
log.info("cat_message:")
log.info(cat_message)

# update conversation history
user_message = self.working_memory["user_message_json"]["text"]
Expand Down
1 change: 0 additions & 1 deletion core/cat/mad_hatter/core_plugin/hooks/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def before_agent_starts(agent_input, cat) -> Union[None, Dict]:
Example 2: don't remember (no uploaded documents about topic)
```python
num_declarative_memories = len( cat.working_memory["declarative_memories"] )
log(num_declarative_memories, "ERROR")
if num_declarative_memories == 0:
return {
"output": "Sorry, I have no memories about that."
Expand Down
14 changes: 7 additions & 7 deletions core/cat/mad_hatter/mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def find_plugins(self):

all_plugin_folders = [core_plugin_folder] + glob.glob(f"{plugins_folder}*/")

log("ACTIVE PLUGINS:", "INFO")
log(self.active_plugins, "INFO")
log.info("ACTIVE PLUGINS:")
log.info(self.active_plugins)

# discover plugins, folder by folder
for folder in all_plugin_folders:
Expand All @@ -110,7 +110,7 @@ def load_plugin(self, plugin_path):
except Exception as e:
# Something happened while loading the plugin.
# Print the error and go on with the others.
log(str(e), "ERROR")
log.error(str(e))

# Load hooks and tools of the active plugins into MadHatter
def sync_hooks_and_tools(self):
Expand Down Expand Up @@ -193,7 +193,7 @@ def embed_tools(self):
}],
)

log(f"Newly embedded tool: {tool.description}", "WARNING")
log.warning(f"Newly embedded tool: {tool.description}")

# easy access to mad hatter tools (found in plugins)
mad_hatter_tools_descriptions = [t.description for t in self.tools]
Expand All @@ -203,7 +203,7 @@ def embed_tools(self):
for id, descr in zip(embedded_tools_ids, embedded_tools_descriptions):
# if the tool is not active, it inserts it in the list of points to be deleted
if descr not in mad_hatter_tools_descriptions:
log(f"Deleting embedded tool: {descr}", "WARNING")
log.warning(f"Deleting embedded tool: {descr}")
points_to_be_deleted.append(id)

# delete not active tools
Expand All @@ -221,13 +221,13 @@ def toggle_plugin(self, plugin_id):

# update list of active plugins
if plugin_is_active:
log(f"Toggle plugin {plugin_id}: Deactivate", "WARNING")
log.warning(f"Toggle plugin {plugin_id}: Deactivate")
# Deactivate the plugin
self.plugins[plugin_id].deactivate()
# Remove the plugin from the list of active plugins
self.active_plugins.remove(plugin_id)
else:
log(f"Toggle plugin {plugin_id}: Activate", "WARNING")
log.warning(f"Toggle plugin {plugin_id}: Activate")
# Activate the plugin
self.plugins[plugin_id].activate()
# Ass the plugin in the list of active plugins
Expand Down
14 changes: 7 additions & 7 deletions core/cat/mad_hatter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def deactivate(self):

# If the module is imported it is removed
if py_filename in sys.modules:
log(f"Remove module {py_filename}", "DEBUG")
log.debug(f"Remove module {py_filename}")
sys.modules.pop(py_filename)

self._hooks = []
Expand Down Expand Up @@ -98,8 +98,8 @@ def load_settings(self):
with open(settings_file_path, "r") as json_file:
settings = json.load(json_file)
except Exception as e:
log(f"Unable to load plugin {self._id} settings", "ERROR")
log(e, "ERROR")
log.error(f"Unable to load plugin {self._id} settings")
log.error(e)

return settings

Expand All @@ -126,7 +126,7 @@ def save_settings(self, settings: Dict):
with open(settings_file_path, "w") as json_file:
json.dump(updated_settings, json_file, indent=4)
except Exception:
log(f"Unable to save plugin {self._id} settings", "ERROR")
log.error(f"Unable to save plugin {self._id} settings")
return {}

return updated_settings
Expand All @@ -144,7 +144,7 @@ def _load_manifest(self):
json_file_data = json.load(json_file)
json_file.close()
except Exception:
log(f"Loading plugin {self._path} metadata, defaulting to generated values", "INFO")
log.info(f"Loading plugin {self._path} metadata, defaulting to generated values")

meta["name"] = json_file_data.get("name", to_camel_case(self._id))
meta["description"] = json_file_data.get("description", (
Expand All @@ -169,15 +169,15 @@ def _load_hooks_and_tools(self):
for py_file in self.py_files:
py_filename = py_file.replace("/", ".").replace(".py", "") # this is UGLY I know. I'm sorry

log(f"Import module {py_filename}", "INFO")
log.info(f"Import module {py_filename}")

# save a reference to decorated functions
try:
plugin_module = importlib.import_module(py_filename)
hooks += getmembers(plugin_module, self._is_cat_hook)
tools += getmembers(plugin_module, self._is_cat_tool)
except Exception as e:
log(f"Error in {py_filename}: {str(e)}","ERROR")
log.error(f"Error in {py_filename}: {str(e)}")
traceback.print_exc()
raise Exception(f"Unable to load the plugin {self._id}")

Expand Down
6 changes: 3 additions & 3 deletions core/cat/mad_hatter/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ async def registry_search_plugins(
return response.json()["plugins"]

except Exception as e:
log(e, "ERROR")
log.error(e)
return []


def registry_download_plugin(url: str) -> str:

log(f"Downloading {url}", "INFO")
log.info(f"Downloading {url}")

registry_url = get_registry_url()
payload = {
Expand All @@ -52,6 +52,6 @@ def registry_download_plugin(url: str) -> str:
with open(plugin_zip_path, "wb") as f:
f.write(response.content)

log(f"Saved plugin as {plugin_zip_path}", "INFO")
log.info(f"Saved plugin as {plugin_zip_path}")

return plugin_zip_path
27 changes: 13 additions & 14 deletions core/cat/memory/vector_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def connect_to_vector_memory(self) -> None:
qdrant_host = os.getenv("QDRANT_HOST", db_path)

if len(qdrant_host) == 0 or qdrant_host == db_path:
log(f"Qdrant path: {db_path}","INFO")
log.info(f"Qdrant path: {db_path}")
# Qdrant local vector DB client

# reconnect only if it's the first boot and not a reload
Expand All @@ -77,8 +77,7 @@ def connect_to_vector_memory(self) -> None:
s = socket.socket()
s.connect((qdrant_host, qdrant_port))
except Exception:
log("QDrant does not respond to %s:%s" %
(qdrant_host, qdrant_port), "ERROR")
log.error(f"QDrant does not respond to {qdrant_host}:{qdrant_port}")
sys.exit()
finally:
s.close()
Expand Down Expand Up @@ -117,8 +116,8 @@ def __init__(self, cat, client: Any, collection_name: str, embeddings: Embedding
self.check_embedding_size()

# log collection info
log(f"Collection {self.collection_name}:", "INFO")
log(dict(self.client.get_collection(self.collection_name)), "INFO")
log.info(f"Collection {self.collection_name}:")
log.info(dict(self.client.get_collection(self.collection_name)))

def check_embedding_size(self):

Expand All @@ -127,15 +126,15 @@ def check_embedding_size(self):
same_size = (self.client.get_collection(self.collection_name).config.params.vectors.size==self.embedder_size)
alias = self.embedder_name + "_" + self.collection_name
if alias==self.client.get_collection_aliases(self.collection_name).aliases[0].alias_name and same_size:
log(f'Collection "{self.collection_name}" has the same embedder', "INFO")
log.info(f'Collection "{self.collection_name}" has the same embedder')
else:
log(f'Collection "{self.collection_name}" has different embedder', "WARNING")
log.warning(f'Collection "{self.collection_name}" has different embedder')
# dump collection on disk before deleting
self.save_dump()
log(f'Dump "{self.collection_name}" completed', "INFO")
log.info(f'Dump "{self.collection_name}" completed')

self.client.delete_collection(self.collection_name)
log(f'Collection "{self.collection_name}" deleted', "WARNING")
log.warning(f'Collection "{self.collection_name}" deleted')
self.create_collection()

def create_db_collection_if_not_exists(self):
Expand All @@ -145,15 +144,15 @@ def create_db_collection_if_not_exists(self):
for c in collections_response.collections:
if c.name == self.collection_name:
# collection exists. Do nothing
log(f'Collection "{self.collection_name}" already present in vector store', "INFO")
log.info(f'Collection "{self.collection_name}" already present in vector store')
return

self.create_collection()

# create collection
def create_collection(self):

log(f"Creating collection {self.collection_name} ...", "WARNING")
log.warning(f"Creating collection {self.collection_name} ...")
self.client.recreate_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(
Expand Down Expand Up @@ -265,9 +264,9 @@ def save_dump(self, folder="dormouse/"):
port = self.client._client._port

if os.path.isdir(folder):
log(f'Directory dormouse exists', "INFO")
log.info(f'Directory dormouse exists')
else:
log(f'Directory dormouse NOT exists, creating it.', "WARNING")
log.warning(f'Directory dormouse does NOT exists, creating it.')
os.mkdir(folder)

self.snapshot_info = self.client.create_snapshot(collection_name=self.collection_name)
Expand All @@ -281,5 +280,5 @@ def save_dump(self, folder="dormouse/"):
os.rename(snapshot_url_out, new_name)
for s in self.client.list_snapshots(self.collection_name):
self.client.delete_snapshot(collection_name=self.collection_name, snapshot_name=s.name)
log(f'Dump "{new_name}" completed', "WARNING")
log.warning(f'Dump "{new_name}" completed')
# dump complete
11 changes: 5 additions & 6 deletions core/cat/rabbit_hole.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def ingest_memory(self, file: UploadFile):
} for p in declarative_memories]
vectors = [v["vector"] for v in declarative_memories]

log(f"Preparing to load {len(vectors)} vector memories", "INFO")
log.info(f"Preparing to load {len(vectors)} vector memories")

# Check embedding size is correct
embedder_size = self.cat.memory.vectors.embedder_size
Expand Down Expand Up @@ -200,7 +200,7 @@ def file_to_docs(
with urlopen(request) as response:
file_bytes = response.read()
except HTTPError as e:
log(e, "ERROR")
log.error(e)
else:

# Get mime type from file extension and source
Expand Down Expand Up @@ -253,7 +253,7 @@ def store_documents(self, docs: List[Document], source: str) -> None:
before_rabbithole_insert_memory
"""

log(f"Preparing to memorize {len(docs)} vectors")
log.info(f"Preparing to memorize {len(docs)} vectors")

# hook the docs before they are stored in the vector memory
docs = self.cat.mad_hatter.execute_hook(
Expand Down Expand Up @@ -281,10 +281,9 @@ def store_documents(self, docs: List[Document], source: str) -> None:
[doc.metadata],
)

# log(f"Inserted into memory({inserting_info})", "INFO")
print(f"Inserted into memory({inserting_info})")
log.info(f"Inserted into memory({inserting_info})")
else:
log(f"Skipped memory insertion of empty doc ({inserting_info})", "INFO")
log.info(f"Skipped memory insertion of empty doc ({inserting_info})")

# wait a little to avoid APIs rate limit errors
time.sleep(0.1)
Expand Down
6 changes: 3 additions & 3 deletions core/cat/routes/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def install_plugin(
},
)

log(f"Uploading {content_type} plugin {file.filename}", "INFO")
log.info(f"Uploading {content_type} plugin {file.filename}")
plugin_archive_path = f"/tmp/{file.filename}"
with open(plugin_archive_path, "wb+") as f:
f.write(file.file.read())
Expand Down Expand Up @@ -113,8 +113,8 @@ async def install_plugin_from_registry(
try:
tmp_plugin_path = registry_download_plugin( payload["url"] )
except Exception as e:
log("Could not download plugin form registry", "ERROR")
log(e, "ERROR")
log.error("Could not download plugin form registry")
log.error(e)
raise HTTPException(
status_code = 500,
detail = { "error": str(e)}
Expand Down
4 changes: 2 additions & 2 deletions core/cat/routes/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def upload_file(

# Get file mime type
content_type = mimetypes.guess_type(file.filename)[0]
log(f"Uploaded {content_type} down the rabbit hole", "INFO")
log.info(f"Uploaded {content_type} down the rabbit hole")

# check if MIME type of uploaded file is supported
if content_type not in admitted_types:
Expand Down Expand Up @@ -122,7 +122,7 @@ async def upload_memory(

# Get file mime type
content_type = mimetypes.guess_type(file.filename)[0]
log(f"Uploaded {content_type} down the rabbit hole", "INFO")
log.info(f"Uploaded {content_type} down the rabbit hole")
if content_type != "application/json":
raise HTTPException(
status_code=400,
Expand Down
4 changes: 2 additions & 2 deletions core/cat/routes/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ async def websocket_endpoint(websocket: WebSocket):
)
except WebSocketDisconnect:
# Handle the event where the user disconnects their WebSocket.
log("WebSocket connection closed", "INFO")
log.info("WebSocket connection closed")
except Exception as e:
# Log any unexpected errors and send an error message back to the user.
log(e, "ERROR")
log.error(e)
traceback.print_exc()
await manager.send_personal_message({
"type": "error",
Expand Down

0 comments on commit 24732d2

Please sign in to comment.