[GH-ISSUE #1720] Have response return minimized JSON (not formatted) to save token space #63012

Closed
opened 2026-05-03 11:15:47 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @erf on GitHub (Dec 26, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/1720

When i get JSON as a response it seem to be formatted with newlines and spaces. If i want to include the response message in follow-up request it will take up extra token space. Maybe better to have the client format the JSON as needed?

EDIT: I'm not really sure if i need to include the previous messages or if i should just set the context ?

Originally created by @erf on GitHub (Dec 26, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/1720 When i get JSON as a response it seem to be formatted with newlines and spaces. If i want to include the response message in follow-up request it will take up extra token space. Maybe better to have the client format the JSON as needed? EDIT: I'm not really sure if i need to include the previous messages or if i should just set the `context` ?
GiteaMirror added the feature request label 2026-05-03 11:15:47 -05:00
Author
Owner

@BruceMacD commented on GitHub (Jan 2, 2024):

Hi @erf, I'd suggest using multi-shot prompting along with the /chat api to achieve this goal. Start the chat with some minified JSON, and then the LLM will be more likely to respond with minified JSON. It also means it will be easier to reformat the responses to save tokens.

Here is an example:

curl http://localhost:11434/api/chat -d '{
    "model": "llama2",
    "format": "json",
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "1234 example ave"
        },
        {
            "role": "assistant",
            "content": "{\"number\":1234,\"street\":\"example ave\"}"
        },
        {
            "role": "user",
            "content": "4567 oak st"
        },
        {
            "role": "assistant",
            "content": "{\"number\":4567,\"street\":\"oak st\"}"
        },
        {
            "role": "user",
            "content": "89 another ave"
        }
    ]
}'

and the response I got:

{
  "model": "llama2",
  "created_at": "2024-01-02T10:30:50.036408Z",
  "message": {
    "role": "assistant",
    "content": "{\"number\":89,\"street\":\"another ave\" }"
  },
  "done": true,
  "total_duration": 605612250,
  "load_duration": 2409375,
  "prompt_eval_count": 102,
  "prompt_eval_duration": 382979000,
  "eval_count": 12,
  "eval_duration": 213132000
}

Hope that helps.

<!-- gh-comment-id:1873851420 --> @BruceMacD commented on GitHub (Jan 2, 2024): Hi @erf, I'd suggest using multi-shot prompting along with the `/chat` api to achieve this goal. Start the chat with some minified JSON, and then the LLM will be more likely to respond with minified JSON. It also means it will be easier to reformat the responses to save tokens. Here is an example: ``` curl http://localhost:11434/api/chat -d '{ "model": "llama2", "format": "json", "stream": false, "messages": [ { "role": "user", "content": "1234 example ave" }, { "role": "assistant", "content": "{\"number\":1234,\"street\":\"example ave\"}" }, { "role": "user", "content": "4567 oak st" }, { "role": "assistant", "content": "{\"number\":4567,\"street\":\"oak st\"}" }, { "role": "user", "content": "89 another ave" } ] }' ``` and the response I got: ``` { "model": "llama2", "created_at": "2024-01-02T10:30:50.036408Z", "message": { "role": "assistant", "content": "{\"number\":89,\"street\":\"another ave\" }" }, "done": true, "total_duration": 605612250, "load_duration": 2409375, "prompt_eval_count": 102, "prompt_eval_duration": 382979000, "eval_count": 12, "eval_duration": 213132000 } ``` Hope that helps.
Author
Owner

@pdevine commented on GitHub (Mar 13, 2024):

Going to go ahead and close the issue, but feel free to keep commenting.

<!-- gh-comment-id:1996100819 --> @pdevine commented on GitHub (Mar 13, 2024): Going to go ahead and close the issue, but feel free to keep commenting.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#63012