docs > llm > Ollama

Ollama

Ollamaインストールと簡単な使用例

test

chat

/api/chat

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma3",
    "messages": [
      { "role": "user", "content": "write a simple sample code with ruby." }
    ],
    "stream": false
  }' | jq

chat with history

/api/chat

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gemma3",
  "messages": [
    {"role": "system", "content": "あなたは旅行や季節の行事に詳しい、親切で穏やかなアシスタントです。"},
    {"role": "user", "content": "そろそろ春だね〜、お花見行きたいなあ。"},
    {"role": "assistant", "content": "いいですね!桜の季節は本当にわくわくしますよね。"},
    {"role": "user", "content": "おすすめの桜の名所ってある?できれば日本国内で!"}
  ],
  "stream": false
}' | jq

prompt

/api/generate

curl http://localhost:11434/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma3",
    "prompt": "write a simple sample code with ruby.",
    "stream": false
  }' | jq

Extract and Print Response Text from Pipe with Ruby One-liner

| ruby -r json -e 'puts JSON.parse(STDIN.read)["response"]'

ollama interface

ollama run gemma3 'hallo'

install & run server

gemma3を使用する例を記載する。

mac

brew install ollama
ollama serve
ollama run gemma3

linux

# https://ollama.com
curl -fsSL https://ollama.com/install.sh | sh
ollama run gemma3

program with ruby

require 'net/http'
require 'uri'
require 'json'

# 送信先のURL
url = URI.parse('http://localhost:11434/api/chat')

# 送信するJSONデータ
data = {
    "model": "gemma3",
    "prompt": "write a simple sample code with ruby.",
    "stream": false
}

# HTTPリクエストを作成
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")

request = Net::HTTP::Post.new(url.path, {
  'Content-Type' => 'application/json'
})

# JSONデータをボディに設定
request.body = data.to_json

# リクエスト送信 & レスポンス取得
response = http.request(request)

# レスポンス出力
puts "Status: #{response.code}"
puts "Body: #{response.body}"

Commands

コードブロックからの抽出

複数コードブロックがある場合は、複数のコードブロックの内容が連結される。

sed -n '/^```/,/^```/ {/^```/d; p}'
Reference