安装与设置
安装要求复制
向 AI 提问
pip install -U langchain-community
复制
向 AI 提问
import requests
from langchain_community.embeddings import JinaEmbeddings
from numpy import dot
from numpy.linalg import norm
from PIL import Image
通过 JinaAI API 使用 Jina 嵌入模型嵌入文本和查询
复制
向 AI 提问
text_embeddings = JinaEmbeddings(
jina_api_key="jina_*", model_name="jina-embeddings-v2-base-en"
)
复制
向 AI 提问
text = "This is a test document."
复制
向 AI 提问
query_result = text_embeddings.embed_query(text)
复制
向 AI 提问
print(query_result)
复制
向 AI 提问
doc_result = text_embeddings.embed_documents([text])
复制
向 AI 提问
print(doc_result)
通过 JinaAI API 使用 Jina CLIP 嵌入图像和查询
复制
向 AI 提问
multimodal_embeddings = JinaEmbeddings(jina_api_key="jina_*", model_name="jina-clip-v1")
复制
向 AI 提问
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)
复制
向 AI 提问
image_result = multimodal_embeddings.embed_images([image])
复制
向 AI 提问
print(image_result)
复制
向 AI 提问
description_result = multimodal_embeddings.embed_documents([description])
复制
向 AI 提问
print(description_result)
复制
向 AI 提问
cosine_similarity = dot(image_result[0], description_result[0]) / (
norm(image_result[0]) * norm(description_result[0])
)
复制
向 AI 提问
print(cosine_similarity)
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。