主题
在本地运行 LLM 最简单的方法之一是使用 llamafile。llamafiles 将模型权重和特别编译的 llama.cpp 版本打包到一个文件中,该文件几乎可以在任何计算机上运行,无需额外依赖。它们还附带一个嵌入式推理服务器,提供用于与模型交互的 API。
设置
- 从 HuggingFace 下载 llamafile
- 使文件可执行
- 运行该文件
下面是一个简单的 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 --embedding1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
模型的推理服务器默认监听 localhost:8080。
%pip install llama-index-embeddings-llamafile1
!pip install llama-index1
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])1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14