Skip to content
LlamaIndex 文档版本 跟随上游 源快照 23afb9ce 更新于 2026-10-18

在本地运行 LLM 最简单的方法之一是使用 llamafile。llamafiles 将模型权重和特别编译的 llama.cpp 版本打包到一个文件中,该文件几乎可以在任何计算机上运行,无需额外依赖。它们还附带一个嵌入式推理服务器,提供用于与模型交互的 API。

设置

  1. 从 HuggingFace 下载 llamafile
  2. 使文件可执行
  3. 运行该文件

下面是一个简单的 bash 脚本,展示了全部 3 个设置步骤:

终端窗口

# Download a llamafile from HuggingFace
wget https://huggingface.co/jartine/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile

# Make the file executable. On Windows, instead just rename the file to end in ".exe".
chmod +x TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile

# Start the model server. Listens at http://localhost:8080 by default.
./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser --embedding

模型的推理服务器默认监听 localhost:8080。

%pip install llama-index-embeddings-llamafile
!pip install llama-index
from llama_index.embeddings.llamafile import LlamafileEmbedding

embedding = LlamafileEmbedding(
    base_url="http://localhost:8080",
)

pass_embedding = embedding.get_text_embedding_batch(
    ["This is a passage!", "This is another passage"], show_progress=True
)
print(len(pass_embedding), len(pass_embedding[0]))

query_embedding = embedding.get_query_embedding("Where is blue?")
print(len(query_embedding))
print(query_embedding[:10])

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。