什么是Ollama?
Ollama是一个专为在本地机器上便捷部署和运行大型语言模型(LLM)而设计的开源工具。
如何部署Ollama?
我是使用的云平台,大家也可以根据自己的云平台的特点进行适当的调整。
使用系统:ubuntu
首先从官网上下载https://ollama.com/download/linux对应的命令
curl -fsSL https://ollama.com/install.sh | sh
更新配置文件
vi /etc/systemd/system/ollama.service
在里面添加:
Environment=“OLLAMA_HOST=0.0.0.0:8891”
输入:wq!回车,保存此文件。
重启服务
sudo systemctl daemon-reload
sudo systemctl restart ollama
查看ollama的状态
sudo systemctl status ollama
正常返回如下
拉取模型,为了方便(如果是用云平台的话,最好带上端口号):
从这里找自己需要的模型https://ollama.com/search
OLLAMA_HOST=127.0.0.1:8891 ollama run deepseek-r1
结束之后,就可以对话了
如果想查看你都有哪些模型,可以使用如下命令
OLLAMA_HOST=127.0.0.1:8891 ollama list
如何使用langchain 调用ollama的模型
使用的langchain的版本号:
langchain 0.3.17
langchain-community 0.3.16
from langchain.prompts.chat import ChatPromptTemplate
from langchain_community.chat_models import ChatOllamatemplate="你是一个特别厉害的翻译专家,可以将{input_language}翻译成{output_language}"
human_template="{text}"
chat_prompt=ChatPromptTemplate.from_messages([("system",template),("human",human_template)])
messages=chat_prompt.format_messages(input_language="中文",output_language="英文",text="今天是周日")ollama_llm = ChatOllama(model="deepseek-r1:latest",base_url="http://localhost:8891")
result=ollama_llm.invoke(messages)
print(result.content)
然后你就可以看到答案