直接上 代码
python
# 01-hello-ollama.py
from langchain.chat_models import init_chat_model
model=init_chat_model(
model="ollama:deepseek-r1:1.5b",
base_url="http://localhost:11434",
temperature=0.1,
timeout=30,
max_tokens=2000
)
for chunk in model.stream("你好你谁"):
print(chunk.content,end='',flush=True)
(langchain1.0) D:\IDO\mylangchain1.0>python 01-hello-ollama.py
遇到问题:
"C:\Users\Administrator\.conda\envs\langchain1.0\Lib\site-packages\ollama\_client.py", line 179, in inner raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: (status code: 502) (langchain1.0) D:\IDO\mylangchain1.0>
报错502给我苦恼的要死,各种改ollama段端口。
解决问题思路。
1.确定ollama run deepseek-r1:1.5b 能跑起来。如果跑不起来是不是模型没有下载
2.ollama serve 重启ollama serve看到这个界面就代码ollama 服务器启动成功

3.然后重新开启一个prompt窗口 进入工程目录 激活运行环境 运行python 01-hello-ollama.py
(我的还是报502)
我又是改端口,又是改防火墙,最后发现是vpn的事情。我把vpn关了,重启服务就好了,要命了啊!!!!