本文最后更新于:2025年2月14日 上午
ollama 本地模型可以通过终端和 Web 端调用,自己编程时还是需要代码调用,本文记录Python 调用本地大模型的方法。
简介
本地大模型部署
本文记录三种调用方式:
python ollama 库调用
langchain 库调用
request 调用
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" 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 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. }, "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-12 T14:49:56.691882 8Z', '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/