Haystack 的 Instructor embedders 集成
模块 haystack_integrations.components.embedders.instructor_embedders.instructor_document_embedder
InstructorDocumentEmbedder
一个使用 INSTRUCTOR 嵌入模型计算 Document 嵌入的组件。每个 Document 的嵌入都存储在Document 的 embedding 字段中。
使用示例
# To use this component, install the "instructor-embedders-haystack" package.
# pip install instructor-embedders-haystack
from haystack_integrations.components.embedders.instructor_embedders import InstructorDocumentEmbedder
from haystack.dataclasses import Document
from haystack.utils import ComponentDevice
doc_embedding_instruction = "Represent the Medical Document for retrieval:"
doc_embedder = InstructorDocumentEmbedder(
model="hkunlp/instructor-base",
instruction=doc_embedding_instruction,
batch_size=32,
device=ComponentDevice.from_str("cpu"),
)
doc_embedder.warm_up()
# Text taken from PubMed QA Dataset (https://hugging-face.cn/datasets/pubmed_qa)
document_list = [
Document(
content="Oxidative stress generated within inflammatory joints can produce autoimmune phenomena and joint destruction. Radical species with oxidative activity, including reactive nitrogen species, represent mediators of inflammation and cartilage damage.",
meta={
"pubid": "25,445,628",
"long_answer": "yes",
},
),
Document(
content="Plasma levels of pancreatic polypeptide (PP) rise upon food intake. Although other pancreatic islet hormones, such as insulin and glucagon, have been extensively investigated, PP secretion and actions are still poorly understood.",
meta={
"pubid": "25,445,712",
"long_answer": "yes",
},
),
]
result = doc_embedder.run(document_list)
print(f"Document Text: {result['documents'][0].content}")
print(f"Document Embedding: {result['documents'][0].embedding}")
print(f"Embedding Dimension: {len(result['documents'][0].embedding)}")
InstructorDocumentEmbedder.__init__
def __init__(model: str = "hkunlp/instructor-base",
device: Optional[ComponentDevice] = None,
token: Optional[Secret] = Secret.from_env_var("HF_API_TOKEN",
strict=False),
instruction: str = "Represent the document",
batch_size: int = 32,
progress_bar: bool = True,
normalize_embeddings: bool = False,
meta_fields_to_embed: Optional[List[str]] = None,
embedding_separator: str = "\n")
创建一个 InstructorDocumentEmbedder 组件。
参数:
model:在 Hugging Face 模型中心中模型的本地路径或名称,例如'hkunlp/instructor-base'.device: 加载模型的设备。如果None,默认设备会自动选择。token:用于从 Hugging Face 下载私有模型的 API 令牌。如果此参数设置为True,则运行transformers-cli login时生成的令牌(存储在 ~/.huggingface)将被使用。instruction:在计算特定领域嵌入时使用的指令字符串。指令遵循统一模板,形式为:“代表 'domain' 的 'text_type' 以用于 'task_objective'”,其中- "domain" 是可选的,它指定文本的领域,例如,科学、金融、医学等。
- "text_type" 是必需的,它指定编码单元,例如,句子、文档、段落等。
- "task_objective" 是可选的,它指定嵌入的目标,例如,检索文档、对句子进行分类等。在此处查看一些指令示例:here。
batch_size:一次编码的字符串数量。progress_bar:如果为 true,则在嵌入过程中显示进度条。normalize_embeddings:如果设置为 true,则返回的向量长度将为 1。meta_fields_to_embed:应与 Document 内容一起嵌入的元字段列表。embedding_separator:用于将元字段连接到 Document 内容的分隔符。
InstructorDocumentEmbedder.to_dict
def to_dict() -> Dict[str, Any]
将组件序列化为字典。
返回值:
包含序列化数据的字典。
InstructorDocumentEmbedder.from_dict
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "InstructorDocumentEmbedder"
从字典反序列化组件。
参数:
data: 要反序列化的字典。
返回值:
反序列化后的组件。
InstructorDocumentEmbedder.warm_up
def warm_up()
Initializes the component.
InstructorDocumentEmbedder.run
@component.output_types(documents=List[Document])
def run(documents: List[Document])
嵌入 Document 列表。每个 Document 的嵌入都存储在Document 的 embedding 字段中。
param documents: 要嵌入的 Document 列表。
模块 haystack_integrations.components.embedders.instructor_embedders.instructor_text_embedder
InstructorTextEmbedder
一个使用 INSTRUCTOR 嵌入模型嵌入字符串的组件。
使用示例
# To use this component, install the "instructor-embedders-haystack" package.
# pip install instructor-embedders-haystack
from haystack.utils.device import ComponentDevice
from haystack_integrations.components.embedders.instructor_embedders import InstructorTextEmbedder
text = ("It clearly says online this will work on a Mac OS system. The disk comes and it does not, only Windows.
"Do Not order this if you have a Mac!!")
instruction = (
"Represent the Amazon comment for classifying the sentence as positive or negative"
)
text_embedder = InstructorTextEmbedder(
model="hkunlp/instructor-base", instruction=instruction,
device=ComponentDevice.from_str("cpu")
)
text_embedder.warm_up()
embedding = text_embedder.run(text)
InstructorTextEmbedder.__init__
def __init__(model: str = "hkunlp/instructor-base",
device: Optional[ComponentDevice] = None,
token: Optional[Secret] = Secret.from_env_var("HF_API_TOKEN",
strict=False),
instruction: str = "Represent the sentence",
batch_size: int = 32,
progress_bar: bool = True,
normalize_embeddings: bool = False)
创建一个 InstructorTextEmbedder 组件。
参数:
model:在 Hugging Face 模型中心中模型的本地路径或名称,例如'hkunlp/instructor-base'.device: 加载模型的设备。如果None,默认设备会自动选择。token:用于从 Hugging Face 下载私有模型的 API 令牌。instruction:在计算特定领域嵌入时使用的指令字符串。指令遵循统一模板,形式为:“代表 'domain' 的 'text_type' 以用于 'task_objective'”,其中- "domain" 是可选的,它指定文本的领域,例如,科学、金融、医学等。
- "text_type" 是必需的,它指定编码单元,例如,句子、文档、段落等。
- "task_objective" 是可选的,它指定嵌入的目标,例如,检索文档、对句子进行分类等。在此处查看一些指令示例:here。
batch_size:一次编码的字符串数量。progress_bar:如果为 true,则在嵌入过程中显示进度条。normalize_embeddings:如果设置为 true,则返回的向量长度将为 1。
InstructorTextEmbedder.to_dict
def to_dict() -> Dict[str, Any]
将组件序列化为字典。
返回值:
包含序列化数据的字典。
InstructorTextEmbedder.from_dict
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "InstructorTextEmbedder"
从字典反序列化此组件。
InstructorTextEmbedder.warm_up
def warm_up()
加载嵌入后端。
InstructorTextEmbedder.run
@component.output_types(embedding=List[float])
def run(text: str)
嵌入一个字符串。
