Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to JSON handling for local LLMs #269

Merged
merged 4 commits into from
Nov 3, 2023
Merged

Conversation

cpacker
Copy link
Collaborator

@cpacker cpacker commented Nov 3, 2023

"Closes" the double-JSON error mentioned in #177

  • Add a double-json string parse fallback for common JSON failure mode (double object)
  • Make "no wrapper specified" only pop up once
  • Allow retries on failed JSON decoding errors

Move to separate PR:

- [ ] Add/remove heartbeats manually
- [ ] Move web UI to openai extension setup? See @TheOnlyWiseJEDI's comments on discord
- [ ] Print the model that's running in the backend (with GET for lm studio)

Example fixing the double-JSON problem

Example bad LLM output (from #177):

{
  "function": "core_memory_append",
  "params": {
    "name": "human",
    "content": "Horst, 42 years old, from Germany."
  }
}
{
  "function": "send_message",
  "params": {
    "message": "Got it! Your age and nationality are now saved in my memory."
  }
}

MemGPT output (without patch):

memgpt.errors.LocalLLMError: Failed to parse JSON from local LLM response - error: Failed to decode JSON from LLM output:
{
      "function": "core_memory_append",
      "params": {
        "name": "human",
        "content": "Horst, 42 years old, from Germany."
      }
    }
    {
      "function": "send_message",
      "params": {
        "message": "Got it! Your age and nationality are now saved in my memory."
      }
    }

MemGPT output (with patch):

💭 Bootup sequence complete. Persona activated. Testing messaging functionality.
🧑 {'type': 'login', 'last_login': 'Never (first login)', 'time': '2023-11-02 11:59:28 PM PDT-0700'}
Hit enter to begin (will request first MemGPT message)

Warning: no wrapper specified for local LLM, using the default wrapper (you can remove this warning by specifying the wrapper with --model)
💭 None
⚡🧠 [function] updating memory with core_memory_append


`/dumpraw`

{'role': 'function', 'name': 'send_message', 'content': '{"status": "OK", "message": null, "time": "2023-11-02 11:59:28 PM PDT-0700"}'}
{'role': 'user', 'content': '{"type": "login", "last_login": "Never (first login)", "time": "2023-11-02 11:59:28 PM PDT-0700"}'}
{'role': 'assistant', 'content': None, 'function_call': {'name': 'core_memory_append', 'arguments': '{"name": "human", "content": "Horst, 42 years old, from Germany."}'}}
{'role': 'function', 'name': 'core_memory_append', 'content': '{"status": "OK", "message": null, "time": "2023-11-02 11:59:29 PM PDT-0700"}'}

Example fixing the sep token (eg <im_sep>, FUNC RET, ...) after JSON problem

Example bad LLM output:

{
  "function": "send_message",
  "params": {
    "message": "Welcome to our platform, Chad!"
  }
}<|im_end|>
<|im_start|>assistant

MemGPT output (without patch):

memgpt.errors.LocalLLMError: Failed to parse JSON from local LLM response - error: Failed to decode JSON from LLM output:
{
  "function": "send_message",
  "params": {
    "message": "Welcome to our platform, Chad!"
  }
}<|im_end|>
<|im_start|>assistant

MemGPT output (with patch):

💭 Bootup sequence complete. Persona activated. Testing messaging functionality.
🧑 {'type': 'login', 'last_login': 'Never (first login)', 'time': '2023-11-03 12:02:56 AM PDT-0700'}
Hit enter to begin (will request first MemGPT message)

Warning: no wrapper specified for local LLM, using the default wrapper (you can remove this warning by specifying the wrapper with --model)
💭 None
🤖 Welcome to our platform, Chad!

\dumpraw

{'role': 'function', 'name': 'send_message', 'content': '{"status": "OK", "message": null, "time": "2023-11-03 12:02:56 AM PDT-0700"}'}
{'role': 'user', 'content': '{"type": "login", "last_login": "Never (first login)", "time": "2023-11-03 12:02:56 AM PDT-0700"}'}
{'role': 'assistant', 'content': None, 'function_call': {'name': 'send_message', 'arguments': '{"message": "Welcome to our platform, Chad!"}'}}
{'role': 'function', 'name': 'send_message', 'content': '{"status": "OK", "message": null, "time": "2023-11-03 12:02:56 AM PDT-0700"}'}

@cpacker cpacker marked this pull request as draft November 3, 2023 05:35
}
}
"""
raise NotImplementedError
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this in a separate PR to address #245

@cpacker cpacker changed the title [Draft] Improvements to JSON handling for local LLMs Improvements to JSON handling for local LLMs Nov 3, 2023
@cpacker cpacker marked this pull request as ready for review November 3, 2023 07:06
@cpacker cpacker requested a review from vivi November 3, 2023 07:06


def extract_first_json(string):
"""Handles the case of two JSON objects back-to-back"""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just counts opening vs closing brackets to try and pull the first JSON object out of a potential "run-on JSON object"

@@ -184,9 +185,9 @@ def output_to_chat_completion_response(self, raw_llm_output):
raw_llm_output = "{" + raw_llm_output

try:
function_json_output = json.loads(raw_llm_output)
function_json_output = clean_json(raw_llm_output)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should be added to the other wrappers too

data = json.loads(raw_llm_output)
except json.JSONDecodeError:
try:
data = json.loads(raw_llm_output + "}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤌

Copy link
Contributor

@vivi vivi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 📜

@cpacker cpacker merged commit a2e50d9 into main Nov 3, 2023
2 checks passed
@cpacker cpacker deleted the local-llm-goodies branch November 3, 2023 08:07
mattzh72 pushed a commit that referenced this pull request Oct 9, 2024
* some extra json hacks

* add 'smart' json loader to other wrapers

* added chatml related stop tokens by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants