Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: Append encoding='utf-8' to open (#1423)

Bear Huange5fd3fbab68f
diff --git a/memgpt/cli/cli_config.py b/memgpt/cli/cli_config.py
index 94bec9ab0..b29381e91 100644
--- a/memgpt/cli/cli_config.py
+++ b/memgpt/cli/cli_config.py
@@ -1150,7 +1150,7 @@ def add(
ms = MetadataStore(config)
if filename: # read from file
assert text is None, "Cannot specify both text and filename"
- with open(filename, "r") as f:
+ with open(filename, "r", encoding="utf-8") as f:
text = f.read()
if option == "persona":
persona = ms.get_persona(name=name, user_id=user_id)
diff --git a/memgpt/client/admin.py b/memgpt/client/admin.py
index 2199da8a2..944699523 100644
--- a/memgpt/client/admin.py
+++ b/memgpt/client/admin.py
@@ -87,7 +87,7 @@ class Admin:
# tools (currently only available for admin)
def create_tool(self, name: str, file_path: str, source_type: Optional[str] = "python", tags: Optional[List[str]] = None) -> ToolModel:
"""Add a tool implemented in a file path"""
- source_code = open(file_path, "r").read()
+ source_code = open(file_path, "r", encoding="utf-8").read()
data = {"name": name, "source_code": source_code, "source_type": source_type, "tags": tags}
response = requests.post(f"{self.base_url}/api/tools", json=data, headers=self.headers)
if response.status_code != 200:
diff --git a/memgpt/functions/functions.py b/memgpt/functions/functions.py
index 8cade7704..c75c74a99 100644
--- a/memgpt/functions/functions.py
+++ b/memgpt/functions/functions.py
@@ -69,7 +69,7 @@ def write_function(module_name: str, function_name: str, function_code: str):
# Write the function to a file
file_path = os.path.join(USER_FUNCTIONS_DIR, f"{module_name}.py")
- with open(file_path, "w") as f:
+ with open(file_path, "w", encoding="utf-8") as f:
f.write(function_code)
succ, error = validate_function(module_name, file_path)
diff --git a/memgpt/local_llm/llm_chat_completion_wrappers/configurable_wrapper.py b/memgpt/local_llm/llm_chat_completion_wrappers/configurable_wrapper.py
index cf5478dee..9d272449d 100644
--- a/memgpt/local_llm/llm_chat_completion_wrappers/configurable_wrapper.py
+++ b/memgpt/local_llm/llm_chat_completion_wrappers/configurable_wrapper.py
@@ -346,7 +346,7 @@ class ConfigurableJSONWrapper(LLMChatCompletionWrapper):
"default_stop_sequences": self.default_stop_sequences,
}
- with open(file_path, "w") as yaml_file:
+ with open(file_path, "w", encoding="utf-8") as yaml_file:
yaml.dump(data, yaml_file, default_flow_style=False)
@staticmethod
@@ -357,7 +357,7 @@ class ConfigurableJSONWrapper(LLMChatCompletionWrapper):
Args:
file_path (str): The path to the YAML file.
"""
- with open(file_path, "r") as yaml_file:
+ with open(file_path, "r", encoding="utf-8") as yaml_file:
data = yaml.safe_load(yaml_file)
wrapper = ConfigurableJSONWrapper()
diff --git a/memgpt/presets/presets.py b/memgpt/presets/presets.py
index 14df189f6..fd5fe7855 100644
--- a/memgpt/presets/presets.py
+++ b/memgpt/presets/presets.py
@@ -53,7 +53,7 @@ def add_default_tools(user_id: uuid.UUID, ms: MetadataStore):
def add_default_humans_and_personas(user_id: uuid.UUID, ms: MetadataStore):
for persona_file in list_persona_files():
- text = open(persona_file, "r").read()
+ text = open(persona_file, "r", encoding="utf-8").read()
name = os.path.basename(persona_file).replace(".txt", "")
if ms.get_persona(user_id=user_id, name=name) is not None:
printd(f"Persona '{name}' already exists for user '{user_id}'")
@@ -61,7 +61,7 @@ def add_default_humans_and_personas(user_id: uuid.UUID, ms: MetadataStore):
persona = PersonaModel(name=name, text=text, user_id=user_id)
ms.add_persona(persona)
for human_file in list_human_files():
- text = open(human_file, "r").read()
+ text = open(human_file, "r", encoding="utf-8").read()
name = os.path.basename(human_file).replace(".txt", "")
if ms.get_human(user_id=user_id, name=name) is not None:
printd(f"Human '{name}' already exists for user '{user_id}'")
diff --git a/memgpt/server/rest_api/server.py b/memgpt/server/rest_api/server.py
index b9fa13f4e..6e080c37a 100644
--- a/memgpt/server/rest_api/server.py
+++ b/memgpt/server/rest_api/server.py
@@ -121,7 +121,7 @@ def on_startup():
memgpt_api = app.openapi_schema.copy()
memgpt_api["paths"] = {key: value for key, value in memgpt_api["paths"].items() if not key.startswith(OPENAI_API_PREFIX)}
memgpt_api["info"]["title"] = "MemGPT API"
- with open("openapi_memgpt.json", "w") as file:
+ with open("openapi_memgpt.json", "w", encoding="utf-8") as file:
print(f"Writing out openapi_memgpt.json file")
json.dump(memgpt_api, file, indent=2)
@@ -132,7 +132,7 @@ def on_startup():
if not (key.startswith(API_PREFIX) or key.startswith(ADMIN_PREFIX))
}
openai_assistants_api["info"]["title"] = "OpenAI Assistants API"
- with open("openapi_assistants.json", "w") as file:
+ with open("openapi_assistants.json", "w", encoding="utf-8") as file:
print(f"Writing out openapi_assistants.json file")
json.dump(openai_assistants_api, file, indent=2)
diff --git a/memgpt/utils.py b/memgpt/utils.py
index 2b7632954..1ff2c5086 100644
--- a/memgpt/utils.py
+++ b/memgpt/utils.py
@@ -978,7 +978,7 @@ def get_human_text(name: str, enforce_limit=True):
for file_path in list_human_files():
file = os.path.basename(file_path)
if f"{name}.txt" == file or name == file:
- human_text = open(file_path, "r").read().strip()
+ human_text = open(file_path, "r", encoding="utf-8").read().strip()
if enforce_limit and len(human_text) > CORE_MEMORY_HUMAN_CHAR_LIMIT:
raise ValueError(f"Contents of {name}.txt is over the character limit ({len(human_text)} > {CORE_MEMORY_HUMAN_CHAR_LIMIT})")
return human_text
@@ -990,7 +990,7 @@ def get_persona_text(name: str, enforce_limit=True):
for file_path in list_persona_files():
file = os.path.basename(file_path)
if f"{name}.txt" == file or name == file:
- persona_text = open(file_path, "r").read().strip()
+ persona_text = open(file_path, "r", encoding="utf-8").read().strip()
if enforce_limit and len(persona_text) > CORE_MEMORY_PERSONA_CHAR_LIMIT:
raise ValueError(
f"Contents of {name}.txt is over the character limit ({len(persona_text)} > {CORE_MEMORY_PERSONA_CHAR_LIMIT})"
@@ -1004,7 +1004,7 @@ def get_human_text(name: str):
for file_path in list_human_files():
file = os.path.basename(file_path)
if f"{name}.txt" == file or name == file:
- return open(file_path, "r").read().strip()
+ return open(file_path, "r", encoding="utf-8").read().strip()
def get_schema_diff(schema_a, schema_b):