跳到主要内容
MLX 模型可以通过 MLXPipeline 类在本地运行。 MLX 社区拥有超过 150 个模型,所有这些模型都是开源的,并在 Hugging Face 模型中心(一个人们可以轻松协作和共同构建 ML 的在线平台)上公开可用。 这些模型可以通过此本地管道封装器从 LangChain 调用,也可以通过 MlXPipeline 类调用其托管的推理端点。有关 mlx 的更多信息,请参阅示例存储库笔记。 要使用,您应该安装 mlx-lm python ,以及 transformers。您也可以安装 huggingface_hub
pip install -qU  mlx-lm transformers huggingface_hub

模型加载

可以通过使用 from_model_id 方法指定模型参数来加载模型。
from langchain_community.llms.mlx_pipeline import MLXPipeline

pipe = MLXPipeline.from_model_id(
    "mlx-community/quantized-gemma-2b-it",
    pipeline_kwargs={"max_tokens": 10, "temp": 0.1},
)
它们也可以通过直接传入现有的 transformers 管道来加载
from mlx_lm import load

model, tokenizer = load("mlx-community/quantized-gemma-2b-it")
pipe = MLXPipeline(model=model, tokenizer=tokenizer)

创建链

将模型加载到内存后,您可以将其与提示组合以形成一个链。
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)

chain = prompt | pipe

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.