bramses / chatgpt-md

A (nearly) seamless integration of ChatGPT into Obsidian.
MIT License
824 stars 61 forks source link

Error message `${role}\\\\n\\\\n`;' is not one of ['system', 'assistant', 'user']` when asked chatgpt to to explain code snippet #7

Closed MaksimZinovev closed 1 year ago

MaksimZinovev commented 1 year ago

Hi , Thank you for the plugin. I came across this issue. Probably I am doing something incorrectly. Would appreciate it if you could help me . Thanks

I can attach source file if needed.

Preconditions:

  1. Install and enable plugin as per instructions https://github.com/bramses/chatgpt-md

To reproduce

  1. Create new note.
  2. Paste snippet 1
  3. Type question "Can you explain the above code?^"
  4. Run command "Chat GPT MD: Chat"
  5. Observe response in your file , as expected.
  6. Run command "Chat GPT MD: Add divider"
  7. Paste snippet 2
  8. Run command "Chat GPT MD: Chat"
  9. Observe error message
    Error: issue calling OpenAI API, see error for more details: Error: Error: {"message":"'${role}\\\\n\\\\n`;' is not one of ['system', 'assistant', 'user'] - 'messages.5.role'","type":"invalid_request_error","param":null,"code":null}
    at ChatGPT_MD.callOpenAIAPI (plugin:chatgpt-md:148:13)

Snippet 1


console.log("Hello")

Snippet 2

    addHR(editor: Editor, role: string) {
        const newLine = `\n\n<hr class="__chatgpt_plugin">\n\nrole::${role}\n\n`;
        editor.replaceRange(newLine, editor.getCursor());

        // move cursor to end of file
        const cursor = editor.getCursor();
        const newCursor = {
            line: cursor.line,
            ch: cursor.ch + newLine.length,
        };
        editor.setCursor(newCursor);
    }

image

Expected result: Chat GPT should explain snippet 2 correctly.

Environment

  1. MacOS Ventura 13.2.1
  2. Obsidian 1.1.16
  3. ChatGPT MD plugin: 1.0.2
bramses commented 1 year ago

Hi @MaksimZinovev, haha yes this is an edge case. The program "splits" messages for the ChatGPT API on that HR line <hr... since it all reads in as a string. In other words, it's a meta case.

So it errored because it saw that as the split. Remove that line and it will work. Does that make sense?

MaksimZinovev commented 1 year ago

Haha, sorry I am a bit slow :) Just realised what kind of issue I "reported". I wanted to test the code explain case and grabbed the first snippet I found in your repo :) The one that broke the plugin. I attribute this to the fact that I am a software tester :) Sometimes I break software completely unintentionally and then realise I just found a bug (or a feature:) )