def stream_chat(client, messages, model=MODEL, enable_think=DEFAULT_ENABLE_THINK):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
stream=True,
extra_body={
"chat_template_kwargs": {
"enable_thinking": enable_think
}
}
)
except Exception as e:
pass
def stream_chat(client, messages, model=MODEL, think=DEFAULT_THINK):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
stream=True,
extra_body={
"think": think
}
)
except Exception as e:
pass
OLLAMA_API = "http://localhost:11434/api/chat"
payload = {
"model": model,
"messages": messages,
"stream": True,
"think": enable_think # True=开启思考,False=关闭思考
}
def stream_chat(messages, model=MODEL, enable_think=True):
payload = {
"model": model,
"messages": messages,
"stream": True,
"think": enable_think
}
try:
response = requests.post(OLLAMA_API, json=payload, stream=True)
response.raise_for_status()
for line in response.iter_lines():
if line:
data = json.loads(line.decode('utf-8'))
# 输出思考内容(灰色)
if 'think' in data and data['think']:
yield f"\033[90m[思考] {data['think']}\033[0m\n"
# 输出正式回答
if 'message' in data and 'content' in data['message']:
yield data['message']['content']
except Exception as e:
print(f"错误:{e}", file=sys.stderr)
sys.exit(1)
/api/chatthink: True/False
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/678020/viewspace-3114514/ ,如需转载,请注明出处,否则将追究法律责任。