安装依赖
pip install gradio==6.0.2 langchain==1.1.3 langchain-ollama==1.0.0 langchain-community==0.4.1
直接上代码
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
import gradio as gr
from langchain_ollama import ChatOllama
import os
import time
import sys
llm = ChatOllama(model=models[0], temperature=0.95, stream=isstream, max_tokens=64000000,verbose=True,reasoning=True) # ,enable_thinking=True,think=True
从langchain_ollama 代码分析 reasoning=True 就是开启思考模式
reasoning: bool | str | None = None
"""Controls the reasoning/thinking mode for [supported models](https://ollama.com/search?c=thinking).
- `True`: Enables reasoning mode. The model's reasoning process will be
captured and returned separately in the `additional_kwargs` of the
response message, under `reasoning_content`. The main response
content will not include the reasoning tags.
- `False`: Disables reasoning mode. The model will not perform any reasoning,
and the response will not include any reasoning content.
- `None` (Default): The model will use its default reasoning behavior. Note
however, if the model's default behavior *is* to perform reasoning, think tags
(`<think>` and `</think>`) will be present within the main response content
unless you set `reasoning` to `True`.
- `str`: e.g. `'low'`, `'medium'`, `'high'`. Enables reasoning with a custom
intensity level. Currently, this is only supported `gpt-oss`. See the
[Ollama docs](https://github.com/ollama/ollama-python/blob/da79e987f0ac0a4986bf396f043b36ef840370bc/ollama/_types.py#L210)
for more information.
控制[支持模型](https://ollama.com/search?c=thinking)的推理/思维模式。
- `True`: 启用推理模式。模型的推理过程将被捕获,并在响应消息的`additional_kwargs`中,以`reasoning_content`为键单独返回。主要的响应内容将不包含推理标签。
- `False`:禁用推理模式。模型将不会执行任何推理,且响应中不会包含任何推理内容。
- `None`(默认):模型将使用其默认推理行为。但请注意,如果模型的默认行为是执行推理,那么除非你将`reasoning`设置为`True`,否则主响应内容中将会包含思考标签(`<think>`和`</think>`)。
- `str`: 例如 `'low'`、`'medium'`、`'high'`。允许使用自定义的强度级别进行推理。目前,仅`gpt-oss`支持此功能。有关更多信息,请参阅[Ollama文档](https://github.com/ollama/ollama-python/blob/da79e987f0ac0a4986bf396f043b36ef840370bc/ollama/_types.py#L210)。
ollama对话
ollama_result = llm.invoke(history_langchain_format)
print(ollama_result)
gpt_think_response = ollama_result.additional_kwargs["reasoning_content"]
gpt_response = "```\n" + gpt_think_response + "\n```\n" + ollama_result.content
490




被折叠的 条评论
为什么被折叠?



