本文最后更新于:2025年2月14日 上午

ollama 本地模型可以通过终端和 Web 端调用,自己编程时还是需要代码调用,本文记录Python 调用本地大模型的方法。

简介

本地大模型部署

本文记录三种调用方式:

  1. python ollama 库调用
  2. langchain 库调用
  3. request 调用

ollama库

环境准备:

1
pip install ollama

调用示例:

如果你都是按照默认设置安装的Ollama,即host和port等均未设置,那执行以下代码即可

1
2
3
4
import ollama

res=ollama.chat(model="huihui_ai/deepseek-r1-abliterated:7b",stream=False,messages=[{"role": "user","content": "你是谁"}],options={"temperature":0})
print(res)

输出:

1
model='huihui_ai/deepseek-r1-abliterated:7b' created_at='2025-02-12T14:38:39.8568659Z' done=True done_reason='stop' total_duration=2020750300 load_duration=1359223900 prompt_eval_count=5 prompt_eval_duration=109000000 eval_count=40 eval_duration=551000000 message=Message(role='assistant', content='<think>\n\n</think>\n\n您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-R1。如您有任何任何问题,我会尽我所能为您提供帮助。', images=None, tool_calls=None)
  • 如果你更改了Ollama的配置,比如更改了监听端口,则执行下边代码:
1
2
3
4
5
6
7
8
import ollama

host="xxx"
port="xxx"
client= ollama.Client(host=f"http://{host}:{port}")
res=client.chat(model="qwen2:1.5b",messages=[{"role": "user","content": "你是谁"}],options={"temperature":0})

print(res)

langchain

安装依赖:

1
2
pip install langchain
pip install langchain_community

调用示例

1
2
3
4
5
6
7
from langchain_community.llms import Ollama

host="localhost"
port="11434" #默认的端口号为11434
llm=Ollama(base_url=f"http://{host}:{port}", model="huihui_ai/deepseek-r1-abliterated:7b",temperature=0)
res=llm.invoke("你是谁")
print(res)

输出:

1
2
3
4
5
6
7
K:\lab\pytorch\ollama_test.py:5: LangChainDeprecationWarning: The class `Ollama` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0. An updated version of the class exists in the :class:`~langchain-ollama package and should be used instead. To use it run `pip install -U :class:`~langchain-ollama` and import as `from :class:`~langchain_ollama import OllamaLLM``.
llm=Ollama(base_url=f"http://{host}:{port}", model="huihui_ai/deepseek-r1-abliterated:7b",temperature=0)
<think>

</think>

您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-R1。如您有任何任何问题,我会尽我所能为您提供帮助。

requests 调用

安装依赖

1
pip install requests

调用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import requests
host="localhost"
port="11434"
url = f"http://{host}:{port}/api/chat"
model = "huihui_ai/deepseek-r1-abliterated:7b"
headers = {"Content-Type": "application/json"}
data = {
"model": model, #模型选择
"options": {
"temperature": 0. #为0表示不让模型自由发挥,输出结果相对较固定,>0的话,输出的结果会比较放飞自我
},
"stream": False, #流式输出
"messages": [{
"role": "system",
"content":"你是谁?"
}] #对话列表
}
response=requests.post(url,json=data,headers=headers,timeout=60)
res=response.json()
print(res)

输出:

1
{'model': 'huihui_ai/deepseek-r1-abliterated:7b', 'created_at': '2025-02-12T14:49:56.6918828Z', 'message': {'role': 'assistant', 'content': '<think>\n\n</think>\n\n您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-R1。如您有任何任何问题,我会尽我所能为您提供帮助。'}, 'done_reason': 'stop', 'done': True, 'total_duration': 849898500, 'load_duration': 20248600, 'prompt_eval_count': 5, 'prompt_eval_duration': 25000000, 'eval_count': 40, 'eval_duration': 803000000}

相关参数说明

上述几个调用方式中所涉及到的比较重要的参数介绍如下:

  • temperature:用于调整生成结果的创造性程度,设置越高,生成的文本越新颖、越独特,设置越低,结果更集中
  • stream:默认false,是否流式传输回部分进度。
  • format: 转录输出的格式,可选项包括 json、str 等。

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/python/python-ollama/python-ollama/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Python 调用本地 ollama 大模型
https://www.zywvvd.com/notes/coding/python/python-ollama/python-ollama/
作者
Yiwei Zhang
发布于
2025年2月12日
许可协议