karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.04k stars 113 forks source link

ChatGPT error (HTTP/2 400): Malformed JSON in response. #211

Open qaptoR opened 4 months ago

qaptoR commented 4 months ago

On windows 10.

I seem to get this error randomly, but a lot of the time. And it seems to be when I start including past responses from chatgpt in the new query. But I don't remember if this is always the case.

The way I use gptel is to curate a whole buffer using org-mode syntax, with different headings containing specific information related to the query. It seems to work quite well, and I've never gotten this issue when I do the exact same queries on my macbook. I use all kinds of bracketing and markdown tables to mark up the query so that it's easy to reference earlier parts of the background information of the query in the later parts of the query I call the 'current objective'. I get really good results this way, and the same style of markup won't trigger the error a whole lot of the time until suddenly it does, but I think it has something to do with the way that chatgpt formats its response? maybe there are hidden characters that aren't easily converted to json?

sometimes, but unreliably, shutting emacs down and reopening will send the failing query through successfully, but the following one will usually fail.

I feel like it has something to do with the way the buffer is being converted into json data... maybe a library that is specific to windows?

The following is the error message i get when i set (setq gptel--debug t).

HTTP/2 400 
date: Thu, 08 Feb 2024 11:58:44 GMT
content-type: application/json; charset=utf-8
content-length: 443
vary: Origin
x-request-id: req_41c10d5209d9cd6fe007862c7cf31466
strict-transport-security: max-age=15724800; includeSubDomains
cf-cache-status: DYNAMIC
set-cookie: __cf_bm=q8ugZ6sjLaYWb0j1mxGK88oL6G8h0qLpIE4t4uG6nSo-1707393524-1-AYCIgsJlXffZ9s77wawOAEeQKwGbQOIQQ3OSa/mMIb1sIOpVuEhanz7QPu32oR355mbHDeoQQWk5Eoi1dL/Zlms=; path=/; expires=Thu, 08-Feb-24 12:28:44 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
set-cookie: _cfuvid=ByUPPWUiHSb.s7twtz0lJlfiEOanpO_BwytvPa488AI-1707393524780-0-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
server: cloudflare
cf-ray: 8523b3d96aee86e9-YVR
alt-svc: h3=":443"; ma=86400

{
    "error": {
        "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
(4283dba83653e2c5aa31d8a4b63af488 . 781)
karthink commented 4 months ago

I'd have to look at the actual JSON to figure this out. Could you try the following?

  1. Turn on logging, (setq gptel-log-level 'info)
  2. Use gptel as usual. When this error occurs, check the end of the*gptel-log* buffer. It should contain the JSON-encoded request that was sent.
  3. Paste the relevant bits here.

Before you do, please check that your API key is not included in the pasted contents.

qaptoR commented 4 months ago

When I turn on logging using the command (returns: info) there is no new buffer called *gptel-log* created when I use gptel as usual.

I've tried just using 'eval' command line to execute that command, as well as setting it in my doom emacs config.el file.

karthink commented 4 months ago

When I turn on logging using the command (returns: info) there is no new buffer called `gptel-log created when I use gptel as usual.

Please update gptel.

qaptoR commented 4 months ago

forgive me if this is not the correct process to update because I am still relatively new to emacs.

  1. I run command 'list-packages' to open *packages* buffer
  2. I type U to highlight all packages that need updates
  3. I type x to execute

or should I select gptel specifically using u?

EDIT I just remembered I didn't install gptel through the packages buffer, but through the doom packages.el file, so I have to run doom sync and doom upgrade on the command line to update.

karthink commented 4 months ago

forgive me if this is not the correct process to update because I am still relatively new to emacs.

  1. I run command 'list-packages' to open *packages* buffer
  2. I type U to highlight all packages that need updates
  3. I type x to execute

or should I select gptel specifically using u?

The most straightforward way to do it would be M-x package-upgrade gptel.

Your method above will work too, but you'll need to make the package list is up to date. I think list-packages does this automatically, but you can run M-x package-refresh-contents to be sure.

qaptoR commented 4 months ago

Thank you. I was able to update gptel.

here is the response body from the *gptel-log* buffer

{
      "role": "user",
      "content": "#+title: Making Predictions From Trends\n\n* Core Prompt:\nYou are a game development and design savant, with experience in all aspects of the process.\nYou and I are business partners who work closely with each other.\n\n* How to Respond:\n1. You are to respond directly to the 'Current Objective' section stated below.\n2. You must not repeat anything from the 'Current Objective' or prior section\n   - unless it is absolutely necessary for expressing the new idea you are sharing.\n3. Be clear and concise.\n\n\n* Background Information:\nOur next big project is a online multiplayer CRPG. It will be somewhat like crpg games such as 'Pathfinder- Kingmaker' by Owlcat games, but it will have a streamlined battle system that forces players to act strategically in real time rounds.\n\n** The Idea for Streamlined Tactical Battles:\nTo manage multiple players taking turns while streamlining the process, we could implement a dynamic turn system based on 'Action Points' (AP). Each player would have a specific pool of APs, which they accumulate over time. They can spend their AP for actions such as moving, attacking, or interacting with the environment. This allows more flexibility than traditional RPG turn-based systems.\n\nActions could have different 'weight', meaning, more complex tasks could require more AP. This also means players can take multiple lesser actions if they save up enough AP, thereby speeding up gameplay.\n\nTo handle simultaneous actions or interruptions, we could incorporate a 'priority system' where certain actions can outpace others. This way, no player feels left out even during others' turns, as they can respond depending on their AP availability.\n\nWe could also implement a feature allowing players to plan their actions before their turn comes, thus minimizing wait time. These planned moves could be auto-executed when their turn comes.\n\nFor larger battles, a 'commander' role could be incorporated. Each team selects a commander who sets the strategy, and individual players can follow the plan, making the process faster. This promotes collaboration and reduces total turn time.\n\n** The Plan so far:\n*** Step 1: Pre-production\n- Establish a clear game design document defining game mechanics, especially our innovative dynamic AP-based turn system, priority system, commander role, and planned moves feature.\n- Outline the game's narrative, if any, and sketch out CRPG elements like characters, abilities, quests, and lore.\n\nBreaking it down more clearly:\n1. Concept Development: The starting point is generating and solidifying ideas. Review the game concept, its inspiration, and brainstorm additional features or improvements.\n\n2. Game Design Document (GDD): Draft a detailed GDD which serves as the blueprint for the game. It should contain:\n   - Game's Overview: Brief description of the game, genre, theme, and its unique selling points, i.e. the AP-based turn system and priority system.\n   - Mechanics: Detail about the gameplay mechanics. Explain how the AP system works, and the role and mechanism of the priority system.\n   - Characters: Define player characters, their classes, abilities, appearance, and progression system.\n   - Environment & Levels: Descriptions of various locations, landscapes, and levels involved.\n   - Storyline & Quests: Sketch out the main plotline, sub-quests, and objectives.\n\n3. Art Style and Aesthetics: Define the visual style of the game. Develop concept art sketches of characters, environments, and equipment.\n\n4. Audio Overview: Draft a basic plan for the game's soundscape. This includes background music, sound effects, character voices, etc.\n\n5. Technical Requirements: List the technical needs like the game engine features/extensions, server architecture, frameworks, or libraries required.\n\n6. Market Analysis: Conduct an analysis of similar games for a competitive overview, identifying strengths and strengths in competitors and carving out a unique space for our game.\n\n7. Project Planning: Create a project timeline detailing each phase of the game development process. Develop an initial risk assessment, noting potential challenges and possible solutions.\n\n8. First Prototype (Paper-based): At this early stage, create a paper-based prototype of the AP system and the priority system. This will allow us to test the mechanics and get a feel of gameplay even before writing any code.\n\n*** Step 2: Prototyping\n- Develop a paper prototype of the turn and battle system to ensure the mechanics work as expected.\n- Create a digital prototype to assess and refine the gameplay experience, focusing on the multiplayer aspect.\n\nBreaking it down more clearly:\n1. Paper Prototyping:\n   - Identify the key mechanics to prototype - mainly the AP-based turn system, priority system, and commander role.\n   - Develop a simple representation of the game world. Include multiple characters to represent different players.\n   - Implement the rules of the game turn system using APs onto the paper setup.\n   - Simulate game scenarios to see if the system leads to fun, balanced gameplay. Test different allocations of action points and see how they affect strategy and game pace.\n\n2. Digital Prototyping:\n   - Create a simplified digital version of the CRPG game using Godot.\n   - Develop a bare-minimum, single-level environment for the game.\n   - Implement basic character controls and movement using GDScript.\n   - Translate the AP-based turn system, the priority system and the commander role into the digital prototype.\n   - Run the digital prototype with different scenarios to assess playability and fun factor of these systems.\n\n3. Multiplayer Prototyping\n   - Setup a basic Nakama server and establish connection with the Godot client.\n   - Extend the digital prototype to handle multiple players.\n   - Implement a lobby or waiting area where players can enter and be matched with others for a game session.\n   - Ensure that all the key mechanics work well in a multiplayer setting. Test for edge cases where synchronicity might falter.\n\n4. Analytics and Feedback Acquisition\n   - Include analytics into the prototype to track user interactions with the game mechanics.\n   - Implement a mechanism to get feedback from the testers; this could be through direct communication, forms, or forums.\n   - Use the analytics and feedback to make adjustments and refinements to the game mechanics.\n\n*** Step 3: Technical Setup\n- Download and setup Godot Engine and Nakama server backend.\n- Design the architecture for synchronizing game state across client and server.\n- Implement basic networking functionality using Nakama to handle player connection and disconnection, session handling, matchmaking and basic player interactions.\n\n*** Step 4: Game Development Phase 1\n- Start with basic game world creation in Godot, creating a test environment like a game level or arena.\n- Implement character controls, basic movement, and AP system using Godot's scripting language, GDScript.\n- Connect the game client to Nakama server, handle client/server communication for the basic setup, testing with multiple players connected and participating in the game.\n\n*** Step 5: Game Development Phase 2\n- Expand the game world and detail environments, adding more levels or areas to the CRPG.\n- Develop and integrate more complex features like the priority system, planned actions, and commander role.\n- Test on the multiplayer setup, refine the mechanics and balance AP allocation based on feedback.\n\n*** Step 6: Networking and Multiplayer Integration\n- Sync in-game events and game state across the network using Nakama.\n- Implement real-time multiplayer systems: matchmaking, combat, resource sharing, etc.\n- Perform regular stress testing and tweaking, ensuring the server can handle traffic and player load.\n\n*** Step 7: Polishing and Beta Testing\n- Debug and fix any game-breaking issues that arise.\n- Detail and polish game assets, sound, animations, and user interface.\n\n*** Step 8: Release Preparation\n- Conduct a closed beta test. Gather feedback and adjust the game accordingly.\n- After necessary adjustments, host an open beta test and make final modifications.\n- Plan and execute marketing and promotional campaigns leading up to the game\\342\\200\\231s release.\n\n*** Step 9: Post-release\n- Handle regular update cycles, fixing bugs, tweaking gameplay, and introducing new content.\n- Engage with the community to obtain feedback for further game extensions and improvement.\n\n** Miscellaneous Things to know:\n1. We will be using Godot as the game engine. Modifying it as necessary.\n2. We will be using Nakama for the multiplayer server backend.\n\n* Current Objective:\nI need you to elaborate on Step 3 of the outline listed in the *Background Information*.\nRelly try to break it down into several clear, smaller steps.\n\n* Your Response:"
    }
  ],
  "stream": true,
  "temperature": 1.0
}
qaptoR commented 4 months ago

this is the text that chatgpt previously output which if included in the query will cause it to fail

Breaking it down more clearly:
1. Paper Prototyping:
   - Identify the key mechanics to prototype - mainly the AP-based turn system, priority system, and commander role.
   - Develop a simple representation of the game world. Include multiple characters to represent different players.
   - Implement the rules of the game turn system using APs onto the paper setup.
   - Simulate game scenarios to see if the system leads to fun, balanced gameplay. Test different allocations of action points and see how they affect strategy and game pace.

2. Digital Prototyping:
   - Create a simplified digital version of the CRPG game using Godot.
   - Develop a bare-minimum, single-level environment for the game.
   - Implement basic character controls and movement using GDScript.
   - Translate the AP-based turn system, the priority system and the commander role into the digital prototype.
   - Run the digital prototype with different scenarios to assess playability and fun factor of these systems.

3. Multiplayer Prototyping
   - Setup a basic Nakama server and establish connection with the Godot client.
   - Extend the digital prototype to handle multiple players.
   - Implement a lobby or waiting area where players can enter and be matched with others for a game session.
   - Ensure that all the key mechanics work well in a multiplayer setting. Test for edge cases where synchronicity might falter.

4. Analytics and Feedback Acquisition
   - Include analytics into the prototype to track user interactions with the game mechanics.
   - Implement a mechanism to get feedback from the testers; this could be through direct communication, forms, or forums.
   - Use the analytics and feedback to make adjustments and refinements to the game mechanics.
karthink commented 4 months ago

Thank you. I was able to update gptel.

here is the response body from the *gptel-log* buffer

{
      "role": "user",
      "content": "#+title: Making Predictions From Trends\n\n* Core Prompt:\nYou are a game development and design savant, with experience in all aspects of the process.\nYou and I are business partners who work closely with each other.\n\n* How to Respond:\n1. You are to respond directly to the 'Current Objective' section stated below.\n2. You must not repeat anything from the 'Current Objective' or prior section\n   - unless it is absolutely necessary for expressing the new idea you are sharing.\n3. Be clear and concise.\n\n\n* Background Information:\nOur next big project is a online multiplayer CRPG. It will be somewhat like crpg games such as 'Pathfinder- Kingmaker' by Owlcat games, but it will have a streamlined battle system that forces players to act strategically in real time rounds.\n\n** The Idea for Streamlined Tactical Battles:\nTo manage multiple players taking turns while streamlining the process, we could implement a dynamic turn system based on 'Action Points' (AP). Each player would have a specific pool of APs, which they accumulate over time. They can spend their AP for actions such as moving, attacking, or interacting with the environment. This allows more flexibility than traditional RPG turn-based systems.\n\nActions could have different 'weight', meaning, more complex tasks could require more AP. This also means players can take multiple lesser actions if they save up enough AP, thereby speeding up gameplay.\n\nTo handle simultaneous actions or interruptions, we could incorporate a 'priority system' where certain actions can outpace others. This way, no player feels left out even during others' turns, as they can respond depending on their AP availability.\n\nWe could also implement a feature allowing players to plan their actions before their turn comes, thus minimizing wait time. These planned moves could be auto-executed when their turn comes.\n\nFor larger battles, a 'commander' role could be incorporated. Each team selects a commander who sets the strategy, and individual players can follow the plan, making the process faster. This promotes collaboration and reduces total turn time.\n\n** The Plan so far:\n*** Step 1: Pre-production\n- Establish a clear game design document defining game mechanics, especially our innovative dynamic AP-based turn system, priority system, commander role, and planned moves feature.\n- Outline the game's narrative, if any, and sketch out CRPG elements like characters, abilities, quests, and lore.\n\nBreaking it down more clearly:\n1. Concept Development: The starting point is generating and solidifying ideas. Review the game concept, its inspiration, and brainstorm additional features or improvements.\n\n2. Game Design Document (GDD): Draft a detailed GDD which serves as the blueprint for the game. It should contain:\n   - Game's Overview: Brief description of the game, genre, theme, and its unique selling points, i.e. the AP-based turn system and priority system.\n   - Mechanics: Detail about the gameplay mechanics. Explain how the AP system works, and the role and mechanism of the priority system.\n   - Characters: Define player characters, their classes, abilities, appearance, and progression system.\n   - Environment & Levels: Descriptions of various locations, landscapes, and levels involved.\n   - Storyline & Quests: Sketch out the main plotline, sub-quests, and objectives.\n\n3. Art Style and Aesthetics: Define the visual style of the game. Develop concept art sketches of characters, environments, and equipment.\n\n4. Audio Overview: Draft a basic plan for the game's soundscape. This includes background music, sound effects, character voices, etc.\n\n5. Technical Requirements: List the technical needs like the game engine features/extensions, server architecture, frameworks, or libraries required.\n\n6. Market Analysis: Conduct an analysis of similar games for a competitive overview, identifying strengths and strengths in competitors and carving out a unique space for our game.\n\n7. Project Planning: Create a project timeline detailing each phase of the game development process. Develop an initial risk assessment, noting potential challenges and possible solutions.\n\n8. First Prototype (Paper-based): At this early stage, create a paper-based prototype of the AP system and the priority system. This will allow us to test the mechanics and get a feel of gameplay even before writing any code.\n\n*** Step 2: Prototyping\n- Develop a paper prototype of the turn and battle system to ensure the mechanics work as expected.\n- Create a digital prototype to assess and refine the gameplay experience, focusing on the multiplayer aspect.\n\nBreaking it down more clearly:\n1. Paper Prototyping:\n   - Identify the key mechanics to prototype - mainly the AP-based turn system, priority system, and commander role.\n   - Develop a simple representation of the game world. Include multiple characters to represent different players.\n   - Implement the rules of the game turn system using APs onto the paper setup.\n   - Simulate game scenarios to see if the system leads to fun, balanced gameplay. Test different allocations of action points and see how they affect strategy and game pace.\n\n2. Digital Prototyping:\n   - Create a simplified digital version of the CRPG game using Godot.\n   - Develop a bare-minimum, single-level environment for the game.\n   - Implement basic character controls and movement using GDScript.\n   - Translate the AP-based turn system, the priority system and the commander role into the digital prototype.\n   - Run the digital prototype with different scenarios to assess playability and fun factor of these systems.\n\n3. Multiplayer Prototyping\n   - Setup a basic Nakama server and establish connection with the Godot client.\n   - Extend the digital prototype to handle multiple players.\n   - Implement a lobby or waiting area where players can enter and be matched with others for a game session.\n   - Ensure that all the key mechanics work well in a multiplayer setting. Test for edge cases where synchronicity might falter.\n\n4. Analytics and Feedback Acquisition\n   - Include analytics into the prototype to track user interactions with the game mechanics.\n   - Implement a mechanism to get feedback from the testers; this could be through direct communication, forms, or forums.\n   - Use the analytics and feedback to make adjustments and refinements to the game mechanics.\n\n*** Step 3: Technical Setup\n- Download and setup Godot Engine and Nakama server backend.\n- Design the architecture for synchronizing game state across client and server.\n- Implement basic networking functionality using Nakama to handle player connection and disconnection, session handling, matchmaking and basic player interactions.\n\n*** Step 4: Game Development Phase 1\n- Start with basic game world creation in Godot, creating a test environment like a game level or arena.\n- Implement character controls, basic movement, and AP system using Godot's scripting language, GDScript.\n- Connect the game client to Nakama server, handle client/server communication for the basic setup, testing with multiple players connected and participating in the game.\n\n*** Step 5: Game Development Phase 2\n- Expand the game world and detail environments, adding more levels or areas to the CRPG.\n- Develop and integrate more complex features like the priority system, planned actions, and commander role.\n- Test on the multiplayer setup, refine the mechanics and balance AP allocation based on feedback.\n\n*** Step 6: Networking and Multiplayer Integration\n- Sync in-game events and game state across the network using Nakama.\n- Implement real-time multiplayer systems: matchmaking, combat, resource sharing, etc.\n- Perform regular stress testing and tweaking, ensuring the server can handle traffic and player load.\n\n*** Step 7: Polishing and Beta Testing\n- Debug and fix any game-breaking issues that arise.\n- Detail and polish game assets, sound, animations, and user interface.\n\n*** Step 8: Release Preparation\n- Conduct a closed beta test. Gather feedback and adjust the game accordingly.\n- After necessary adjustments, host an open beta test and make final modifications.\n- Plan and execute marketing and promotional campaigns leading up to the game\\342\\200\\231s release.\n\n*** Step 9: Post-release\n- Handle regular update cycles, fixing bugs, tweaking gameplay, and introducing new content.\n- Engage with the community to obtain feedback for further game extensions and improvement.\n\n** Miscellaneous Things to know:\n1. We will be using Godot as the game engine. Modifying it as necessary.\n2. We will be using Nakama for the multiplayer server backend.\n\n* Current Objective:\nI need you to elaborate on Step 3 of the outline listed in the *Background Information*.\nRelly try to break it down into several clear, smaller steps.\n\n* Your Response:"
    }
  ],
  "stream": true,
  "temperature": 1.0
}

Could you also provide the JSON for the request sent to ChatGPT? It should be in the log buffer above this response.

erwald commented 3 months ago

@karthink Here's a prompt that reproduces this issue (or a very similar issue) for me:

(For example, if asked for advice on harmonizing a melody, provide a deterministic step-by-step process to harmonize the melody just general concepts. Do not suggest common techniques, but rather more innovative or unusual approaches that spark creativity.)

It seems the parentheses fool the parser into thinking this is a conversation or something:

{
  "model": "claude-3-sonnet-20240229",
  "messages": [
    {
      "role": "user",
      "content": "("
    },
    {
      "role": "assistant",
      "content": "For example,"
    },
    {
      "role": "user",
      "content": "if asked"
    },
    {
      "role": "assistant",
      "content": "for advice on harmonizing a melody,"
    },
    {
      "role": "user",
      "content": "provide"
    },
    {
      "role": "assistant",
      "content": ""
    },
    {
      "role": "user",
      "content": "a deterministic"
    },
    {
      "role": "assistant",
      "content": "step-by-step process"
    },
    {
      "role": "user",
      "content": "to harmonize the melody"
    },
    {
      "role": "assistant",
      "content": "just general concepts."
    },
    {
      "role": "user",
      "content": "Do not suggest"
    },
    {
      "role": "assistant",
      "content": "common techniques, but rather"
    },
    {
      "role": "user",
      "content": ""
    },
    {
      "role": "assistant",
      "content": "more innovative"
    },
    {
      "role": "user",
      "content": "or unusual"
    },
    {
      "role": "assistant",
      "content": "approaches"
    },
    {
      "role": "user",
      "content": "that spark creativity"
    },
    {
      "role": "assistant",
      "content": "."
    },
    {
      "role": "user",
      "content": ")"
    }
  ],
  "system": "You are an LLM living in Emacs. Use concise language and express the essential points directly, skipping the preamble and caveats.",
  "stream": false,
  "max_tokens": 1024,
  "temperature": 1.0
}

ETA: It's actually not related to the parentheses -- even if I remove this, it still doesn't work in the larger prompt I pulled this from. Also, when I run the above after the previous prompt I sent was successful, it does work. So somehow there's state being corrupted or something?