MLX 本地管道
MLX 模型可以通过 MLXPipeline 类在本地运行。
MLX 社区托管了超过 150 个模型,所有模型都是开源的,并且可以在 Hugging Face Model Hub 上公开获取。Hugging Face Model Hub 是一个在线平台,人们可以在那里轻松协作并共同构建机器学习项目。
它们可以通过这个本地管道包装器从 LangChain 中调用,或者通过 MLXPipeline 类调用它们托管的推理端点。有关 mlx 的更多信息,请参阅 examples repo notebook。
要使用,您应该安装 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 Reference:MLXPipeline
它们也可以通过直接传递现有的 transformers pipeline 来加载
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 Reference:PromptTemplate
Related
- LLM conceptual guide
- LLM how-to guides