Salta ai contenuti

CLI e API

Comando Uso
ollama serve avvia il daemon
ollama pull <m> scarica un modello
ollama run <m> ["prompt"] chat interattiva o prompt singolo
ollama list modelli su disco
ollama ps modelli caricati in memoria
ollama stop <m> scarica il modello dalla memoria
ollama show <m> [--modelfile] dettagli o Modelfile
ollama create <nome> -f Modelfile crea un modello derivato
ollama cp <src> <dst> copia/alias
ollama rm <m> elimina dal disco

Nella chat interattiva: /set parameter num_ctx 16384, /show info, /clear, /bye.

Prompt con input da pipe, utile per far spiegare output di kubectl:

Finestra del terminale
kubectl -n shop-demo describe pod -l app=api | ollama run qwen2.5:7b-instruct "Explain the root cause and the fix. Be concise."
Finestra del terminale
kubectl -n shop-demo get events --sort-by=.lastTimestamp | tail -30 | ollama run qwen2.5:7b-instruct "Group these events by root cause."
Finestra del terminale
curl -s http://localhost:11434/api/tags | jq -r '.models[].name'
Finestra del terminale
curl -s http://localhost:11434/api/ps | jq '.models[] | {name, size_vram, expires_at}'
Finestra del terminale
curl -s http://localhost:11434/api/chat -d '{"model":"qwen2.5:7b-instruct","stream":false,"messages":[{"role":"user","content":"What is a PodDisruptionBudget?"}]}' | jq -r .message.content
Finestra del terminale
curl -s http://localhost:11434/api/generate -d '{"model":"qwen2.5:7b-instruct","prompt":"Say OK","stream":false,"options":{"num_ctx":8192,"temperature":0}}' | jq -r .response
Finestra del terminale
curl -s http://localhost:11434/api/show -d '{"model":"qwen2.5:7b-instruct"}' | jq '.details'
Finestra del terminale
curl -s http://localhost:11434/v1/models | jq -r '.data[].id'
Finestra del terminale
curl -s http://localhost:11434/v1/chat/completions -H 'Content-Type: application/json' -d '{"model":"qwen2.5:7b-instruct","temperature":0,"messages":[{"role":"system","content":"You are a Kubernetes SRE."},{"role":"user","content":"Why would a Service have no endpoints?"}]}' | jq -r '.choices[0].message.content'

Test del tool calling (se il modello lo supporta, la risposta contiene tool_calls):

Finestra del terminale
curl -s http://localhost:11434/v1/chat/completions -H 'Content-Type: application/json' -d '{"model":"qwen2.5:14b-instruct","messages":[{"role":"user","content":"List pods in namespace shop-demo"}],"tools":[{"type":"function","function":{"name":"kubectl_get","description":"Run kubectl get","parameters":{"type":"object","properties":{"resource":{"type":"string"},"namespace":{"type":"string"}},"required":["resource"]}}}]}' | jq '.choices[0].message.tool_calls'

Qualsiasi client OpenAI funziona impostando base URL e una API key fittizia:

Finestra del terminale
export OPENAI_API_BASE=http://localhost:11434/v1 OPENAI_BASE_URL=http://localhost:11434/v1 OPENAI_API_KEY=ollama