letta-ai / letta

Letta (formerly MemGPT) is a framework for creating LLM services with memory.
https://letta.com
Apache License 2.0
12.11k stars 1.34k forks source link

/MemGPT/memgpt/data_types.py:92: UserWarning: Failed to put inner thoughts in kwargs: Invalid control character at: line 2 column 609 (char 610) #1577

Open quantumalchemy opened 3 months ago

quantumalchemy commented 3 months ago

Describe the bug

After update 0.3.21 Getting --> 2024-07-27 13:34:07,646 - MemGPT.memgpt.server.server - DEBUG - Starting agent step /MemGPT/memgpt/data_types.py:92: UserWarning: Failed to put inner thoughts in kwargs: Invalid control character at: line 2 column 609 (char 610) warnings.warn(f"Failed to put inner thoughts in kwargs: {e}") -- se using https://inference.memgpt.ai

config: [defaults] preset = memgpt_chat persona = sam_pov human = basic

[model] model = memgpt-openai model_endpoint = https://inference.memgpt.ai model_endpoint_type = openai context_window = 8192

[embedding] embedding_endpoint_type = hugging-face embedding_endpoint = https://embeddings.memgpt.ai embedding_model = BAAI/bge-large-en-v1.5 embedding_dim = 1024 embedding_chunk_size = 300

[archival_storage] type = chroma path = /root/.memgpt/chroma

[recall_storage] type = sqlite path = /root/.memgpt

[metadata_storage] type = sqlite path = /root/.memgpt

[version] memgpt_version = 0.3.21

[client] anon_clientid = 000000000000000000000242ac130002

quantumalchemy commented 3 months ago

So I created a little patch for this -- just to get over the hump & not just have the serve crap out & go on error / also seems to corrupt the db so you need to do a whole reboot+delete db / config ect. just plugging leaks for demo only .. not doing a pr of course.. But it does work --> memgpt/data_types.py # 74

from typing import Dict, List, Optional, TypeVar

def add_inner_thoughts_to_tool_call(
    tool_call: ToolCall,
    inner_thoughts: str,
    inner_thoughts_key: str,
) -> ToolCall:
    """Add inner thoughts (arg + value) to a tool call"""
    def parse_and_fix_json(json_str: str) -> Dict[str, Any]:
        try:
            return json.loads(json_str)
        except json.JSONDecodeError:
            # Attempt to fix common JSON issues
            fixed_str = json_str.replace("'", '"')  # Replace single quotes with double quotes
            fixed_str = fixed_str.replace("True", "true").replace("False", "false")  # Fix boolean values
            try:
                return json.loads(fixed_str)
            except json.JSONDecodeError:
                # If still can't parse, return an empty dict
                warnings.warn(f"Failed to parse JSON even after attempted fixes: {json_str}")
                return {}

    # Parse and potentially fix the arguments
    func_args = parse_and_fix_json(tool_call.function["arguments"])

    # Add the inner thoughts to the args dict
    func_args[inner_thoughts_key] = inner_thoughts

    # Create the updated tool call
    updated_tool_call = copy.deepcopy(tool_call)
    updated_tool_call.function["arguments"] = json.dumps(func_args, ensure_ascii=JSON_ENSURE_ASCII)

    return updated_tool_call