跳到主要內容
Open In ColabOpen on GitHub

PredictionGuardEmbeddings

Prediction Guard 是一個安全、可擴展的 GenAI 平台,可保護敏感資料、防止常見的 AI 故障,並在經濟實惠的硬體上運行。

概觀

整合詳細資訊

本整合示範如何將 Prediction Guard 嵌入整合與 Langchain 搭配使用。此整合支援文字和圖片,可分開或成對使用。

設定

若要存取 Prediction Guard 模型,請在此處聯絡我們以取得 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 嵌入生成同時支援文字和圖片。此整合包含跨各種功能的支援。

索引和檢索

# 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 參考:InMemoryVectorStore
'LangChain is the framework for building context-aware reasoning applications.'

直接使用

向量資料庫和檢索器實作正在呼叫 embeddings.embed_documents(...)embeddings.embed_query(...),以從 from_texts 和檢索 invoke 操作中使用的文字建立嵌入。

這些方法可以使用以下命令直接呼叫。

嵌入單個文字

# 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]

嵌入單個文字-圖片對

# Embedding a single text-image pair
inputs = [
{
"text": "This is an embedding example.",
"image": "https://farm4.staticflickr.com/3300/3497460990_11dfb95dd1_z.jpg",
},
]
single_vector = embeddings.embed_image_text(inputs)

print(single_vector[0][:5])
[0.0363212488591671, -0.10172265768051147, -0.014760786667466164, -0.046511903405189514, 0.03860781341791153]

嵌入多個文字-圖片對

# Embedding multiple text-image pairs
inputs = [
{
"text": "This is an embedding example.",
"image": "https://fastly.picsum.photos/id/866/200/300.jpg?hmac=rcadCENKh4rD6MAp6V_ma-AyWv641M4iiOpe1RyFHeI",
},
{
"text": "This is another embedding example.",
"image": "https://farm4.staticflickr.com/3300/3497460990_11dfb95dd1_z.jpg",
},
]
two_vectors = embeddings.embed_image_text(inputs)

for vector in two_vectors:
print(vector[:5])
[0.11867266893386841, -0.05898813530802727, -0.026179173961281776, -0.10747235268354416, 0.07684746384620667]
[0.026654226705431938, -0.10080841928720474, -0.012732953764498234, -0.04365091398358345, 0.036743905395269394]

API 參考

如需 PredictionGuardEmbeddings 所有功能和配置的詳細文件,請查看 API 參考: https://langchain-python.dev.org.tw/api_reference/community/embeddings/langchain_community.embeddings.predictionguard.PredictionGuardEmbeddings.html


此頁面是否對您有幫助?