Jina
安装依赖项
pip install -U langchain-community
导入库
import requests
from langchain_community.embeddings import JinaEmbeddings
from numpy import dot
from numpy.linalg import norm
from PIL import Image
API 参考:JinaEmbeddings
通过 JinaAI API 使用 Jina 嵌入模型嵌入文本和查询
text_embeddings = JinaEmbeddings(
jina_api_key="jina_*", model_name="jina-embeddings-v2-base-en"
)
text = "This is a test document."
query_result = text_embeddings.embed_query(text)
print(query_result)
doc_result = text_embeddings.embed_documents([text])
print(doc_result)
通过 JinaAI API 使用 Jina CLIP 嵌入图像和查询
multimodal_embeddings = JinaEmbeddings(jina_api_key="jina_*", model_name="jina-clip-v1")
image = "https://avatars.githubusercontent.com/u/126733545?v=4"
description = "Logo of a parrot and a chain on green background"
im = Image.open(requests.get(image, stream=True).raw)
print("Image:")
display(im)
image_result = multimodal_embeddings.embed_images([image])
print(image_result)
description_result = multimodal_embeddings.embed_documents([description])
print(description_result)
cosine_similarity = dot(image_result[0], description_result[0]) / (
norm(image_result[0]) * norm(description_result[0])
)
print(cosine_similarity)