mcpField
W
Where On-Device and Cloud LLMs Meet

Where On-Device and Cloud LLMs Meet

github.com/shirokatsuya框架与 SDK084 周前更新

关于 Where On-Device and Cloud LLMs Meet

Minions 是一个通信协议,使小型本地模型能与云端前沿模型协作。通过仅在本地读取长上下文,可降低云成本而不降低质量。项目提供该协议的演示实现。

主要功能

  1. 01本地和云端 LLM 协作通信协议
  2. 02通过本地上下文处理降低云成本
  3. 03支持 Ollama 和 Tokasaurus 本地服务器
  4. 04支持 OpenAI 和 Together AI 云提供商
  5. 05流式输出和自定义回调

适用场景

  1. 01在成本和质量间平衡大型应用
  2. 02对长文档进行高效本地处理
  3. 03整合多个 LLM 提供商的优势
  4. 04研究本地-云混合 AI 架构

详细介绍

Minions Logo

本地设备和云端大模型的交汇处

Discord

这是什么? Minions 是一个通信协议,使小型本地设备模型能够与云端的前沿模型协作。通过仅在本地读取长上下文,我们可以在最小或不降低质量的情况下降低云成本。本仓库提供了该协议的演示。请从下方开始,或查看我们的论文和博文了解更多信息。

论文:Minions: Cost-efficient Collaboration Between On-device and Cloud Language Models

博文:https://hazyresearch.stanford.edu/blog/2025-02-24-minions

安装

我们已在 Mac 和 Ubuntu 上使用 Python 3.10-3.11 测试过以下设置(注意:不支持 Python 3.13)

可选:使用您喜爱的包管理器(如 conda、venv、uv)创建虚拟环境
conda create -n minions python=3.11

步骤 1: 克隆仓库并安装 Python 包。

git clone https://github.com/HazyResearch/minions.git
cd minions
pip install -e .  # installs the minions package in editable mode

步骤 2: 安装用于运行本地模型的服务。

我们支持两个用于运行本地模型的服务:ollama 和 tokasaurus。您需要至少安装其中一个。

  • 如果您无法访问 NVIDIA GPU,应该使用 ollama。按照此处的说明安装 ollama。要启用 Flash Attention,请运行 launchctl setenv OLLAMA_FLASH_ATTENTION 1,如果在 Mac 上,请重启 ollama 应用。
  • 如果您有权访问 NVIDIA GPU 并且运行 Minions 协议,应该使用 tokasaurus,它受益于 tokasaurus 的高吞吐量。使用以下命令安装 tokasaurus:
uv pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ tokasaurus==0.0.1.post1

步骤 3: 为以下至少一个云端大模型提供商设置您的 API 密钥。

如有需要,为云模型创建 OpenAI API 密钥或 TogetherAI API 密钥。

export OPENAI_API_KEY=<your-openai-api-key>
export TOGETHER_API_KEY=<your-together-api-key>

Minions 演示应用

Watch the video

要尝试 Minion 或 Minions 协议,请运行以下命令:

streamlit run app.py

如果您看到有关 ollama 客户端的错误,

An error occurred: Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download

请尝试运行以下命令:

OLLAMA_FLASH_ATTENTION=1 ollama serve

示例代码:Minion(单数)

以下示例适用于 ollama 本地客户端和 openai 远程客户端。 协议是 minion。

from minions.clients.ollama import OllamaClient
from minions.clients.openai import OpenAIClient
from minions.minion import Minion

local_client = OllamaClient(
        model_name="llama3.2",
    )

remote_client = OpenAIClient(
        model_name="gpt-4o",
    )

# Instantiate the Minion object with both clients
minion = Minion(local_client, remote_client)


context = """
Patient John Doe is a 60-year-old male with a history of hypertension. In his latest checkup, his blood pressure was recorded at 160/100 mmHg, and he reported occasional chest discomfort during physical activity.
Recent laboratory results show that his LDL cholesterol level is elevated at 170 mg/dL, while his HDL remains within the normal range at 45 mg/dL. Other metabolic indicators, including fasting glucose and renal function, are unremarkable.
"""

task = "Based on the patient's blood pressure and LDL cholesterol readings in the context, evaluate whether these factors together suggest an increased risk for cardiovascular complications."

# Execute the minion protocol for up to two communication rounds
output = minion(
    task=task,
    context=[context],
    max_rounds=2
)

流式输出

要启用响应输出的实时流式传输,您可以使用 stream_output 参数:

from minions.clients.openai import OpenAIClient
from minions.minion import Minion

# Enable streaming in the client
openai_client = OpenAIClient(
    model_name="gpt-4o",
    stream=True  # Enable streaming in the client
)

# Create the minion with streaming enabled
minion = Minion(
    local_client=openai_client,
    remote_client=openai_client,
    stream_output=True  # Enable streaming in the Minion
)

# Now responses will print incrementally in real-time
result = minion(
    task="Generate a short poem about artificial intelligence",
    context=["Make it thoughtful but accessible to general audiences"]
)

您也可以提供自定义回调函数以更好地控制流式内容的显示方式:

def custom_callback(agent_type, chunk, is_streaming=False, is_final=False):
    """Custom callback to handle streaming output."""
    if is_streaming:
        # Process streaming chunks in real-time
        # For example, you could format or colorize the output
        pass
    elif chunk:
        # Handle complete responses
        print(f"\n[{agent_type}] COMPLETE RESPONSE: {chunk}\n")

# Create the minion with custom callback
minion = Minion(
    local_client=openai_client,
    remote_client=openai_client,
    callback=custom_callback,
    stream_output=True
)

有关完整示例,请参阅 examples/stream_example.py 文件。

示例代码:Minions(复数)

以下示例适用于 ollama 本地客户端和 openai 远程客户端。 协议是 minions。

from minions.clients.ollama import OllamaClient
from minions.clients.openai import OpenAIClient
from minions.minions import Minions
from pydantic import BaseModel

class StructuredLocalOutput(BaseModel):
    explanation: str
    citation: str | None
    answer: str | None

local_client = OllamaClient(
        model_name="llama3.2",
        temperature=0.0,
        structured_output_schema=StructuredLocalOutput
)

remote_client = OpenAIClient(
        model_name="gpt-4o",
)


# Instantiate the Minion object with both clients
minion = Minions(local_client, remote_client)


context = """
Patient John Doe is a 60-year-old male with a history of hypertension. In his latest checkup, his blood pressure was recorded at 160/100 mmHg, and he reported occasional chest discomfort during physical activity.
Recent laboratory results show that his LDL cholesterol level is elevated at 170 mg/dL, while his HDL remains within the normal range at 45 mg/dL. Other metabolic indicators, including fasting glucose and renal function, are unremarkable.
"""

task = "Based on the patient's blood pressure and LDL cholesterol readings in the context, evaluate whether these factors together suggest an increased risk for cardiovascular complications."

# Execute the minion protocol for up to two communication rounds
output = minion(
    task=task,
    doc_metadata="Medical Report",
    context=[context],
    max_rounds=2
)

Python 笔记本

要在笔记本中运行 Minion/Minions,请查看 minions.ipynb。

命令行界面

要在命令行中运行 Minion/Minions,请查看 minions_cli.py。

minions --help
minions --context <path_to_context> --protocol <minion|minions>

维护者

通过神经架构搜索实现自主大模型改进

该项目实现了一个神经架构搜索(NAS)系统,使大模型能够通过进化技术自主改进其自身架构。

概览

该系统使用演化算法搜索用于微调大模型的最优超参数和架构配置。它会自动执行:

  1. 生成一个包含多样化架构配置的种群
  2. 训练和评估每个架构
  3. 选择表现最好的配置
  4. 通过变异创建新一代
  5. 重复该过程多代,以找到最优配置

关键功能

  • 演化搜索:使用选择和变异来探索架构空间
  • 参数高效微调:利用 LoRA(低秩自适应)技术
  • 自动化评估:模型会自动被训练和评估
  • 进度跟踪:在每代之后保存结果
  • 自我改进:系统持续改进其自身的架构

搜索空间

NAS 探索以下架构参数:

  • LoRA rank(r)
  • LoRA alpha 值
  • 微调的目标模块
  • 学习率
  • 批次大小
  • 梯度累积步数
  • 秩稳定化 LoRA 的使用

用法

使用以下命令运行 NAS 系统:

python nas.py

你可以修改 nas.py 文件的 __main__ 部分中的参数以自定义:

  • 基础模型
  • 微调所用的数据集
  • 种群大小
  • 代数
  • 序列长度
  • 输出目录

工作原理

  1. 初始化:创建一个包含随机架构配置的种群
  2. 训练:每个架构都在数据集上进行训练
  3. 评估:基于困惑度评估模型
  4. 选择:为下一代选择表现最好的配置
  5. 变异:通过随机修改参数创建新的架构
  6. 迭代:该过程重复多代

最佳模型在每次改进后都会被保存,完整的结果历史记录以 JSON 格式维护。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。