跳转到内容

Llamafile 嵌入

在本地运行大型语言模型的最简单方法之一是使用 llamafile。llamafile 将模型权重和特殊编译版本的 llama.cpp 打包成单个文件,可在大多数计算机上运行而无需额外依赖。它们还内置了嵌入式推理服务器,提供与模型交互的 API 接口。

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

这是一个展示所有3个设置步骤的简单bash脚本:

Terminal window
# 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])