PredictionGuardEmbeddings
Prediction Guard 是一个安全、可扩展的 GenAI 平台,可保护敏感数据,防止常见的 AI 故障,并在经济实惠的硬件上运行。
概览
集 成详情
此集成展示了如何将 Prediction Guard 嵌入集成与 Langchain 一起使用。此集成支持单独或成对组合使用文本和图像。
设置
要访问 Prediction Guard 模型,请在此处与我们联系:https://predictionguard.com/get-started 以获取 Prediction Guard API 密钥并开始使用。
凭证
获取密钥后,您可以使用以下方法进行设置:
import os
os.environ["PREDICTIONGUARD_API_KEY"] = "<Prediction Guard API Key"
安装
%pip install --upgrade --quiet langchain-predictionguard
实例化
首先,安装 Prediction Guard 和 LangChain 包。然后,设置所需的环境变量并进行包导入。
from langchain_predictionguard import PredictionGuardEmbeddings
embeddings = PredictionGuardEmbeddings(model="bridgetower-large-itm-mlm-itc")
Prediction Guard 的 embedding 生成同时支持文本和图像。此次集成将此支持融入了各项功能之中。
索引和检索
# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore
text = "LangChain is the framework for building context-aware reasoning applications."
vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)
# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()
# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is LangChain?")
# Show the retrieved document's content
retrieved_documents[0].page_content
API Reference:InMemoryVectorStore
'LangChain is the framework for building context-aware reasoning applications.'
直接使用
vectorstore 和 retriever 实现会调用 embeddings.embed_documents(...) 和 embeddings.embed_query(...) 来为 from_texts 和检索 invoke 操作所使用的文本创建 embeddings。
这些方法可以通过以下命令直接调用。
嵌入单一文本
# Embedding a single string
text = "This is an embedding example."
single_vector = embeddings.embed_query(text)
single_vector[:5]
[0.01456777285784483,
-0.08131945133209229,
-0.013045587576925755,
-0.09488929063081741,
-0.003087474964559078]
嵌入多个文本
# Embedding multiple strings
docs = [
"This is an embedding example.",
"This is another embedding example.",
]
two_vectors = embeddings.embed_documents(docs)
for vector in two_vectors:
print(vector[:5])
[0.01456777285784483, -0.08131945133209229, -0.013045587576925755, -0.09488929063081741, -0.003087474964559078]
[-0.0015021917643025517, -0.08883760124444962, -0.0025286630261689425, -0.1052245944738388, 0.014225339516997337]
嵌入单张图片
# Embedding a single image. These functions accept image URLs, image files, data URIs, and base64 encoded strings.
image = [
"https://farm4.staticflickr.com/3300/3497460990_11dfb95dd1_z.jpg",
]
single_vector = embeddings.embed_images(image)
print(single_vector[0][:5])
[0.0911610797047615, -0.034427884966135025, 0.007927080616354942, -0.03500846028327942, 0.022317267954349518]
嵌入多张图片
# Embedding multiple images
images = [
"https://fastly.picsum.photos/id/866/200/300.jpg?hmac=rcadCENKh4rD6MAp6V_ma-AyWv641M4iiOpe1RyFHeI",
"https://farm4.staticflickr.com/3300/3497460990_11dfb95dd1_z.jpg",
]
two_vectors = embeddings.embed_images(images)
for vector in two_vectors:
print(vector[:5])
[0.1593627631664276, -0.03636132553219795, -0.013229663483798504, -0.08789524435997009, 0.062290553003549576]
[0.0911610797047615, -0.034427884966135025, 0.007927080616354942, -0.03500846028327942, 0.022317267954349518]