当前位置: 首页 > news >正文

Ollama按照与使用

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_agentllm = 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博客

http://www.lryc.cn/news/573850.html

相关文章:

  • openapi-generator-maven-plugin自动生成HTTP远程调用客户端
  • Java面试复习指南:基础、面向对象、Java 8新特性及并发编程
  • ASP.NET Core API文档与测试实战指南
  • 编程江湖-Git
  • 分库分表下的 ID 冲突问题与雪花算法讲解
  • 【数据结构】_二叉树部分特征统计
  • python基础(3)
  • 【论文阅读 | CVPR 2024 |Fusion-Mamba :用于跨模态目标检测】
  • 利用通义大模型构建个性化推荐系统——从数据预处理到实时API部署
  • 算法-动态规划-钢条切割问题
  • 简单工厂模式,工厂模式和注册工厂模式
  • Go 循环依赖的依赖注入解决方案详解
  • Cache Travel-09-从零开始手写redis(17)v1.0.0 全新版本架构优化+拓展性增强
  • AI三步诊断心理:比ChatGPT更懂人心
  • C#Halcon从零开发_Day14_AOI缺陷检测策略1_Bolb分析+特征分析_饼干破损检测
  • JavaScript性能优化实战
  • MySQL索引分类有哪些?
  • RA4M2开发IOT(9)----动态显示MEMS数据
  • 基于python代码的通过爬虫方式实现TK下载视频(2025年6月)
  • 支付宝携手HarmonyOS SDK实况窗,开启便捷停车生活
  • 湖北理元理律师事务所:构建可持续债务优化的双轨解法
  • all()函数和any()函数
  • Linux->进程概念(精讲)
  • JavaEE-Mybatis进阶
  • 图灵完备之路(数电学习三分钟)----门的多路化
  • 创客匠人行业洞察:创始人 IP 的核心能力构建与长期主义实践
  • YSYX学习记录(十一)
  • Python中使用RK45方法求解微分方程的详细指南
  • mysql 加锁算法 详解
  • OC—多界面传值