From 0027293f2bce8e31a644a1ac81351ffeed235377 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Wed, 18 Dec 2024 08:54:53 +0800 Subject: [PATCH] Update ollama_with_python.py --- 2024.12.11_ollama/ollama_with_python.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/2024.12.11_ollama/ollama_with_python.py b/2024.12.11_ollama/ollama_with_python.py index 5e12fd2..5b37a54 100644 --- a/2024.12.11_ollama/ollama_with_python.py +++ b/2024.12.11_ollama/ollama_with_python.py @@ -7,4 +7,10 @@ print(response['message']['content']) import ollama response = ollama.chat(model="llama3.2:latest", messages=[{"role": "user", "content": "你好"}], stream=True) for part in response: - print(part['message']['content'], end='', flush=True) \ No newline at end of file + print(part['message']['content'], end='', flush=True) + +# 流式输出,且模型后台常驻(需要手动 ollama stop 关闭) +import ollama +response = ollama.chat(model="llama3.2:latest", messages=[{"role": "user", "content": "你好"}], stream=True, keep_alive=-1) +for part in response: + print(part['message']['content'], end='', flush=True)