Embedding Gallery¶
本节包含在 distilabel
中实现的现有 Embeddings
子类。
embeddings
¶
LlamaCppEmbeddings
¶
基类:Embeddings
, CudaDevicePlacementMixin
用于生成 embedding 的 LlamaCpp
库实现。
属性
名称 | 类型 | 描述 |
---|---|---|
model_name |
str
|
包含 GGUF 量化模型的名称,与已安装的 |
model_path |
RuntimeParameter[str]
|
包含 GGUF 量化模型的路径,与已安装的 |
repo_id |
RuntimeParameter[str]
|
Hugging Face Hub 仓库 ID。 |
verbose |
RuntimeParameter[bool]
|
是否打印详细输出。默认为 |
n_gpu_layers |
RuntimeParameter[int]
|
要在 GPU 上运行的层数。默认为 |
disable_cuda_device_placement |
RuntimeParameter[bool]
|
是否禁用 CUDA 设备放置。默认为 |
normalize_embeddings |
RuntimeParameter[bool]
|
是否标准化 embeddings。默认为 |
seed |
int
|
RNG 种子,-1 表示随机 |
n_ctx |
int
|
文本上下文,0 = 来自模型 |
n_batch |
int
|
提示处理最大批次大小 |
extra_kwargs |
Optional[RuntimeParameter[Dict[str, Any]]]
|
将传递给 |
运行时参数
n_gpu_layers
:用于 GPU 的层数。默认为-1
。verbose
:是否打印详细输出。默认为False
。normalize_embeddings
:是否标准化 embeddings。默认为False
。extra_kwargs
:将传递给llama_cpp
库的Llama
类的其他关键字参数字典。默认为{}
。
示例
使用本地模型生成句子 embeddings
from pathlib import Path
from distilabel.models.embeddings import LlamaCppEmbeddings
# You can follow along this example downloading the following model running the following
# command in the terminal, that will download the model to the `Downloads` folder:
# curl -L -o ~/Downloads/all-MiniLM-L6-v2-Q2_K.gguf https://hugging-face.cn/second-state/All-MiniLM-L6-v2-Embedding-GGUF/resolve/main/all-MiniLM-L6-v2-Q2_K.gguf
model_path = "Downloads/"
model = "all-MiniLM-L6-v2-Q2_K.gguf"
embeddings = LlamaCppEmbeddings(
model=model,
model_path=str(Path.home() / model_path),
)
embeddings.load()
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"])
print(results)
embeddings.unload()
使用 HuggingFace Hub 模型生成句子 embeddings
from distilabel.models.embeddings import LlamaCppEmbeddings
# You need to set environment variable to download private model to the local machine
repo_id = "second-state/All-MiniLM-L6-v2-Embedding-GGUF"
model = "all-MiniLM-L6-v2-Q2_K.gguf"
embeddings = LlamaCppEmbeddings(model=model,repo_id=repo_id)
embeddings.load()
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"])
print(results)
embeddings.unload()
# [
# [-0.05447685346007347, -0.01623094454407692, ...],
# [4.4889533455716446e-05, 0.044016145169734955, ...],
# ]
使用 cpu 生成句子 embeddings
from pathlib import Path
from distilabel.models.embeddings import LlamaCppEmbeddings
# You can follow along this example downloading the following model running the following
# command in the terminal, that will download the model to the `Downloads` folder:
# curl -L -o ~/Downloads/all-MiniLM-L6-v2-Q2_K.gguf https://hugging-face.cn/second-state/All-MiniLM-L6-v2-Embedding-GGUF/resolve/main/all-MiniLM-L6-v2-Q2_K.gguf
model_path = "Downloads/"
model = "all-MiniLM-L6-v2-Q2_K.gguf"
embeddings = LlamaCppEmbeddings(
model=model,
model_path=str(Path.home() / model_path),
n_gpu_layers=0,
disable_cuda_device_placement=True,
)
embeddings.load()
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"])
print(results)
embeddings.unload()
# [
# [-0.05447685346007347, -0.01623094454407692, ...],
# [4.4889533455716446e-05, 0.044016145169734955, ...],
# ]
源代码位于 src/distilabel/models/embeddings/llamacpp.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
model_name
property
¶
返回模型的名称。
load()
¶
使用路径或 Hugging Face Hub 仓库 ID 加载 gguf
模型。
源代码位于 src/distilabel/models/embeddings/llamacpp.py
unload()
¶
encode(inputs)
¶
为提供的输入生成 embeddings。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
inputs
|
List[str]
|
需要为其生成 embedding 的文本列表。 |
必需 |
返回值
类型 | 描述 |
---|---|
List[List[Union[int, float]]]
|
生成的 embeddings。 |
源代码位于 src/distilabel/models/embeddings/llamacpp.py
SentenceTransformerEmbeddings
¶
基类:Embeddings
, CudaDevicePlacementMixin
用于生成 embedding 的 sentence-transformers
库实现。
属性
名称 | 类型 | 描述 |
---|---|---|
model |
str
|
模型 Hugging Face Hub 仓库 ID 或包含模型权重和配置文件目录的路径。 |
device |
Optional[RuntimeParameter[str]]
|
用于加载模型的设备名称,例如 "cuda"、"mps" 等。默认为 |
prompts |
Optional[Dict[str, str]]
|
包含要与模型一起使用的提示的字典。默认为 |
default_prompt_name |
Optional[str]
|
将应用于输入的默认提示(在 |
trust_remote_code |
bool
|
是否允许获取和执行从 Hub 仓库获取的远程代码。默认为 |
revision |
Optional[str]
|
如果 |
token |
Optional[str]
|
将用于向 Hugging Face Hub 验证身份的 Hugging Face Hub 令牌。如果未提供,则将使用 |
truncate_dim |
Optional[int]
|
截断句子 embeddings 的维度。默认为 |
model_kwargs |
Optional[Dict[str, Any]]
|
将传递给 Hugging Face |
tokenizer_kwargs |
Optional[Dict[str, Any]]
|
将传递给 Hugging Face |
config_kwargs |
Optional[Dict[str, Any]]
|
将传递给 Hugging Face |
precision |
Optional[Literal['float32', 'int8', 'uint8', 'binary', 'ubinary']]
|
结果 embeddings 将具有的 dtype。默认为 |
normalize_embeddings |
RuntimeParameter[bool]
|
是否标准化 embeddings,使其长度为 1。默认为 |
示例
生成句子 embeddings
from distilabel.models import SentenceTransformerEmbeddings
embeddings = SentenceTransformerEmbeddings(model="mixedbread-ai/mxbai-embed-large-v1")
embeddings.load()
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"])
# [
# [-0.05447685346007347, -0.01623094454407692, ...],
# [4.4889533455716446e-05, 0.044016145169734955, ...],
# ]
源代码位于 src/distilabel/models/embeddings/sentence_transformers.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
model_name
property
¶
返回模型的名称。
load()
¶
加载 Sentence Transformer 模型
源代码位于 src/distilabel/models/embeddings/sentence_transformers.py
encode(inputs)
¶
为提供的输入生成 embeddings。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
inputs
|
List[str]
|
需要为其生成 embedding 的文本列表。 |
必需 |
返回值
类型 | 描述 |
---|---|
List[List[Union[int, float]]]
|
生成的 embeddings。 |
源代码位于 src/distilabel/models/embeddings/sentence_transformers.py
vLLMEmbeddings
¶
基类:Embeddings
, CudaDevicePlacementMixin
用于生成 embedding 的 vllm
库实现。
属性
名称 | 类型 | 描述 |
---|---|---|
model |
str
|
模型 Hugging Face Hub 仓库 ID 或包含模型权重和配置文件目录的路径。 |
dtype |
str
|
用于模型的数据类型。默认为 |
trust_remote_code |
bool
|
是否信任加载模型时的远程代码。默认为 |
quantization |
Optional[str]
|
用于模型的量化模式。默认为 |
revision |
Optional[str]
|
要加载的模型修订版。默认为 |
enforce_eager |
bool
|
是否强制执行 eager 执行。默认为 |
seed |
int
|
用于随机数生成器的种子。默认为 |
extra_kwargs |
Optional[RuntimeParameter[Dict[str, Any]]]
|
将传递给 |
_model |
LLM
|
|
示例
生成句子 embeddings
from distilabel.models import vLLMEmbeddings
embeddings = vLLMEmbeddings(model="intfloat/e5-mistral-7b-instruct")
embeddings.load()
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"])
# [
# [-0.05447685346007347, -0.01623094454407692, ...],
# [4.4889533455716446e-05, 0.044016145169734955, ...],
# ]
源代码位于 src/distilabel/models/embeddings/vllm.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
model_name
property
¶
返回模型的名称。
load()
¶
使用路径或 Hugging Face Hub 仓库 ID 加载 vLLM
模型。
源代码位于 src/distilabel/models/embeddings/vllm.py
unload()
¶
encode(inputs)
¶
为提供的输入生成 embeddings。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
inputs
|
List[str]
|
需要为其生成 embedding 的文本列表。 |
必需 |
返回值
类型 | 描述 |
---|---|
List[List[Union[int, float]]]
|
生成的 embeddings。 |