ここでは ollama の処理を関数化し,その戻り値,標準出力,標準エラー出力を取得する方法を確認します.次のようなコードを準備します.
ollama.py
import subprocess
result = subprocess.run(
["ollama", "run", "qwen3:8b", "こんにちは"],
capture_output=True,
text=True,
encoding="utf-8"
)
print("=== 戻り値 ===")
print(result.returncode)
print("=== 標準出力 ===")
print(result.stdout)
print("=== 標準エラー ===")
print(result.stderr)
次のコマンドを実行すると,戻り値が0で,生成された回答は標準出力に含まれていることが分かりました.
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python ollama.py ⏎
=== 戻り値 ===
0
=== 標準出力 ===
Thinking...
Okay, the user said "こんにちは" which means "Hello" in Japanese. I should respond in Japanese since they used a Japanese greeting. I need to make sure my response is friendly and welcoming. Let me check if there's any specific context they might be referring to, but since it's just a greeting, a simple reply should be fine. I'll say "こんにちは!" and offer assistance. Keep it natural and polite.
...done thinking.
こんにちは!お手伝いできることがあれば、いつでもご相談くださいね。😊
=== 標準エラー ===
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog>