Ollama 快速参考指南

编译了一些以备将来使用……

目录

以下是目前最有用的 Ollama 命令列表和示例([Ollama 命令速查表](https://www.glukhov.org/zh-cn/post/2024/12/ollama-cheatsheet/ “ollama 命令速查表)"),我之前整理过一些。
希望它对你有用(对你有用)。

ollama 命令速查表

此 Ollama 命令速查表专注于 CLI 命令、模型管理和自定义设置。

安装

  • 选项 1:从网站下载
    • 访问 ollama.com 并下载适用于你操作系统的安装程序(Mac、Linux 或 Windows)。
  • 选项 2:通过命令行安装
    • 对于 Mac 和 Linux 用户,使用以下命令:
      curl https://ollama.ai/install.sh | sh
      
    • 按照屏幕上的提示操作,如果需要输入密码,请输入[3]。

系统要求

  • 操作系统: Mac 或 Linux(Windows 版本正在开发中)
  • 内存(RAM): 最低 8GB,推荐 16GB 或更多
  • 存储空间: 至少 ~10GB 空闲空间
  • 处理器: 较新的 CPU(过去 5 年内)[3]。

基本的 Ollama CLI 命令

命令 描述
ollama serve 在你的本地系统上启动 Ollama。
ollama create <new_model> 从现有模型创建一个新模型,用于自定义或训练。
ollama show <model> 显示特定模型的详细信息,例如其配置和发布日期。
ollama run <model> 运行指定模型,使其准备好进行交互。
ollama pull <model> 将指定模型下载到你的系统中。
ollama list 列出所有已下载的模型。
ollama ps 显示当前正在运行的模型。
ollama stop <model> 停止指定的正在运行的模型。
ollama rm <model> 从你的系统中删除指定的模型。
ollama help 提供有关任何命令的帮助。

模型管理

  • 下载模型:

    ollama pull mistral-nemo:12b-instruct-2407-q6_K
    

    此命令将指定模型(例如 Gemma 2B)下载到你的系统中。

  • 运行模型:

    ollama run qwen2.5:32b-instruct-q3_K_S
    

    此命令启动指定模型并打开交互式 REPL 以进行交互。

  • 列出模型:

    ollama list
    

    此命令列出所有已下载到你系统的模型。

  • 停止模型:

    ollama stop llama3.1:8b-instruct-q8_0
    

    此命令停止指定的正在运行的模型。

自定义模型

  • 设置系统提示: 在 Ollama 的 REPL 中,你可以设置一个系统提示以自定义模型的行为:

    >>> /set system 对所有问题的回答尽量使用通俗易懂的英文,避免使用技术术语
    >>> /save ipe
    >>> /bye
    

    然后运行自定义模型:

    ollama run ipe
    

    这设置了系统提示并保存模型以供以后使用。

  • 创建自定义模型文件: 创建一个文本文件(例如 custom_model.txt),结构如下:

    FROM llama3.1
    SYSTEM [在此处输入你的自定义指令]
    

    然后运行:

    ollama create mymodel -f custom_model.txt
    ollama run mymodel
    

    这将根据文件中的指令创建一个自定义模型[3]。

使用 Ollama 处理文件

  • 从文件中总结文本:

    ollama run llama3.2 "Summarize the content of this file in 50 words." < input.txt
    

    此命令使用指定模型总结 input.txt 的内容。

  • 将模型响应记录到文件中:

    ollama run llama3.2 "Tell me about renewable energy." > output.txt
    

    此命令将模型的响应保存到 output.txt 中。

常见使用场景

  • 文本生成:

    • 总结大文本文件:
      ollama run llama3.2 "Summarize the following text:" < long-document.txt
      
    • 生成内容:
      ollama run llama3.2 "Write a short article on the benefits of using AI in healthcare." > article.txt
      
    • 回答特定问题:
      ollama run llama3.2 "What are the latest trends in AI, and how will they affect healthcare?"
      

    .

  • 数据处理与分析:

    • 将文本分类为积极、消极或中性情感:
      ollama run llama3.2 "Analyze the sentiment of this customer review: 'The product is fantastic, but delivery was slow.'"
      
    • 将文本分类到预定义类别中: 使用类似的命令根据预定义的标准对文本进行分类或归类。

使用 Ollama 与 Python

  • 安装 Ollama Python 库:
    pip install ollama
    
  • 使用 Python 生成文本:
    import ollama
    
    response = ollama.generate(model='gemma:2b', prompt='what is a qubit?')
    print(response['response'])
    
    此代码片段使用指定模型和提示生成文本。

有用的链接