MLX 本地管道
MLX 模型可以通过 MLXPipeline
类在本地运行。
MLX 社区 拥有 150 多个模型,所有模型都是开源的,并在 Hugging Face 模型中心(一个人们可以轻松协作和共同构建 ML 的在线平台)上公开可用。
这些模型可以通过此本地管道封装器从 LangChain 调用,或者通过 MlXPipeline 类调用其托管推理端点。有关 mlx 的更多信息,请参阅 示例仓库 笔记本。
要使用它,您应该安装 mlx-lm
Python 包,以及 transformers。您还可以安装 huggingface_hub
。
%pip install --upgrade --quiet 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},
)
API 参考:MLXPipeline
它们也可以通过直接传入现有 `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}))
API 参考:PromptTemplate