1、安装Ollama
# 纯CPU计算 (集成显卡就用这种方式)
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
# Nvidia GPU # 英伟达显卡 安装 NVIDIA Container Toolkit https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#installation docker run -d --runtime=nvidia --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
# AMD GPU 参考 docker run -d --device /dev/kfd --device /dev/dri -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama:rocm
2、运行大模型
模型查找:https://ollama.com/search
#ollama 官方仓库
ollama pull llama3.2
#直接使用huggingface上的模型
ollama pull hf.co/Qwen/Qwen2-7B-Instruct-GGUF:Q4_K_M
# 使用魔搭社区的模型
ollama pull modelscope.cn/Qwen/Qwen2.5-3B-Instruct-GGUF
ollama run deepseek-r1
# 1.5b模型 ollama run deepseek-r1:1.5b
# 14b模型 ollama run deepseek-r1:14b
3、代码调用大模型
Ollama Api
curl http://localhost:11434/api/tags
curl http://localhost:11434/api/generate -d '{ "model": "llama3.2", "prompt":"Why is the sky blue?" }'
curl http://localhost:11434/api/chat -d '{ "model": "llama3.2", "messages": [ { "role": "user", "content": "why is the sky blue?" } ] }'
curl http://localhost:11434/api/embed -d '{ "model": "mxbai-embed-large", "input": "Why is the sky blue?" }'
curl http://localhost:11434/api/embeddings -d '{ "model": "mxbai-embed-large", "prompt": "Here is an article about llamas..." }'
client
demo:ollama
# pip install ollama
import ollama
response = ollama.chat(model='llama3.1', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', }, ])
print(response['message']['content'])
demo:langchain-ollama
#pip install -qU langchain-ollama
from langchain_ollama import ChatOllama
from langchain_core.messages import AIMessage
llm = ChatOllama( model="llama3.1", temperature=0, # other params... )
messages = [ ( "system", "You are a helpful assistant that translates English to French. Translate the user sentence.", ), ("human", "I love programming."), ]
ai_msg = llm.invoke(messages)
ai_msg
demo:OpenAi
import openai
openai.base_url = "http://localhost:11434/v1"
openai.api_key = 'ollama'
response = openai.chat.completions.create( model="llama3.1", messages=messages, tools=tools, )
demo:weather
def weather(city: str):
"""
查询天气
"""
if city == '北京':
return '北京晴朗'
elif city == '上海':
return '上海多云'
else:
return f'不知道 {city}.'
def test_agent():
from langgraph.prebuilt import create_react_agent
llm = ChatOllama(model='qwen2.5', temperature=0)
tools = [weather]
langgraph_agent_executor = create_react_agent(llm, tools)
query = '北京天气如何'
messages = langgraph_agent_executor.invoke({"messages": [("human", query)]})
print(json.dumps([message.model_dump() for message in messages['messages']], indent=2, ensure_ascii=False))
# 输出结果
[
{
"content": "北京天气如何",
"additional_kwargs": {},
"response_metadata": {},
"type": "human",
"name": null,
"id": "0d312e83-dd8a-4638-af7d-c8e47585e380",
"example": false
},
{
"content": "",
"additional_kwargs": {},
"response_metadata": {
"model": "qwen2.5",
"created_at": "2024-10-30T10:35:51.32219Z",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "weather",
"arguments": {
"city": "北京"
}
}
}
]
},
"done_reason": "stop",
"done": true,
"total_duration": 660820709,
"load_duration": 33652000,
"prompt_eval_count": 151,
"prompt_eval_duration": 216216000,
"eval_count": 19,
"eval_duration": 408378000
},
"type": "ai",
"name": null,
"id": "run-44766d01-3e92-4688-9e3c-3a7557338bce-0",
"example": false,
"tool_calls": [
{
"name": "weather",
"args": {
"city": "北京"
},
"id": "99331cef-b8f5-481a-9e10-1ea5e5c0b4f2",
"type": "tool_call"
}
],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 151,
"output_tokens": 19,
"total_tokens": 170
}
},
{
"content": "北京晴朗",
"additional_kwargs": {},
"response_metadata": {},
"type": "tool",
"name": "weather",
"id": "b74bfc72-8dd1-4541-a653-da6e8e299725",
"tool_call_id": "99331cef-b8f5-481a-9e10-1ea5e5c0b4f2",
"artifact": null,
"status": "success"
},
{
"content": "北京现在是晴朗的天气。请注意防晒哦!",
"additional_kwargs": {},
"response_metadata": {
"model": "qwen2.5",
"created_at": "2024-10-30T10:35:51.727177Z",
"message": {
"role": "assistant",
"content": "北京现在是晴朗的天气。请注意防晒哦!"
},
"done_reason": "stop",
"done": true,
"total_duration": 399748125,
"load_duration": 11164250,
"prompt_eval_count": 190,
"prompt_eval_duration": 112236000,
"eval_count": 13,
"eval_duration": 270630000
},
"type": "ai",
"name": null,
"id": "run-bafa67a6-9a7d-4fa5-b240-861e9b39e99b-0",
"example": false,
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 190,
"output_tokens": 13,
"total_tokens": 203
}
}
]
4、Open-UI
docker pull ghcr.io/open-webui/open-webui:main
docker安装细节参考:https://zhuanlan.zhihu.com/p/1902057589019251082
webui、windows安装参考:在Windows上轻松部署本地大语言模型:Ollama与Open-WebUI的完整指南_windows openwebui-CSDN博客


被折叠的 条评论
为什么被折叠?



