[GH-ISSUE #5640] Pass array of messages as an argument #3516

Closed
opened 2026-04-12 14:13:08 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @M3cubo on GitHub (Jul 11, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/5640

In the Docs, it shows that using the API you can pass an array of messages:

"messages": [
{ "role": "user", "content": "why is the sky blue?" }
]

My question is, how can I do it with the CLI? It is possible?

I'm looking into something like:

ollama run "model" "prompt" "messages"

where the argument "messages" could be used to give context.

Do you have any ideas on how to do it?

Originally created by @M3cubo on GitHub (Jul 11, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/5640 In the Docs, it shows that using the API you can pass an array of messages: "messages": [ { "role": "user", "content": "why is the sky blue?" } ] My question is, how can I do it with the CLI? It is possible? I'm looking into something like: > ollama run "model" "prompt" "messages" where the argument "messages" could be used to give context. Do you have any ideas on how to do it?
GiteaMirror added the feature request label 2026-04-12 14:13:08 -05:00
Author
Owner

@rick-github commented on GitHub (Jul 12, 2024):

I don't think you can do this with stock ollama, but it's straightforward for a shell script:

#!/bin/bash

[ "${#@}" -lt 3 ] && { echo "Usage: $0 model prompt context" ; exit 1 ; }

model=$(echo "$1" | jq -R) ; shift
prompt=$(echo "$1" | jq -R) ; shift
context=$(echo "$1" | jq -R) ; shift
query=$(printf '{"model": %s, "messages":[{"role":"assistant","content":%s},{"role":"user","content":%s}],"stream":false}' "$model" "$context" "$prompt")

curl -s ${OLLAMA_HOST-localhost:11434}/api/chat -d "$query" | jq -r .message.content
$ ./ollama-context gemma2 'what does an apple cost' 'i see a sign that says "apples: $1.50"'
An apple costs $1.50, according to the sign I see. 🍎
<!-- gh-comment-id:2226410619 --> @rick-github commented on GitHub (Jul 12, 2024): I don't think you can do this with stock ollama, but it's straightforward for a shell script: ```sh #!/bin/bash [ "${#@}" -lt 3 ] && { echo "Usage: $0 model prompt context" ; exit 1 ; } model=$(echo "$1" | jq -R) ; shift prompt=$(echo "$1" | jq -R) ; shift context=$(echo "$1" | jq -R) ; shift query=$(printf '{"model": %s, "messages":[{"role":"assistant","content":%s},{"role":"user","content":%s}],"stream":false}' "$model" "$context" "$prompt") curl -s ${OLLAMA_HOST-localhost:11434}/api/chat -d "$query" | jq -r .message.content ``` ``` $ ./ollama-context gemma2 'what does an apple cost' 'i see a sign that says "apples: $1.50"' An apple costs $1.50, according to the sign I see. 🍎 ```
Author
Owner

@M3cubo commented on GitHub (Jul 15, 2024):

Many thanks for the workaround!

<!-- gh-comment-id:2228233498 --> @M3cubo commented on GitHub (Jul 15, 2024): Many thanks for the workaround!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#3516