简介
本文记录三种调用方式:
- python ollama 库调用
- langchain 库调用
- request 调用
ollama库
环境准备:
pip install ollama
调用示例:
如果你都是按照默认设置安装的Ollama,即host和port等均未设置,那执行以下代码即可
import ollama
res=ollama.chat(model="huihui_ai/deepseek-r1-abliterated:7b",stream=False,messages=[{"role": "user","content": "你是谁"}],options={"temperature":0})
print(res)
输出:
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的配置,比如更改了监听端口,则执行下边代码:
-
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
安装依赖:
pip install langchain
pip install langchain_community
调用示例
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)
输出:
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 调用
安装依赖
pip install requests
调用示例
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)
输出:
{'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 等。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容