复制
向 AI 提问
# sign up for an account: https://deepinfra.com/login?utm_source=langchain
from getpass import getpass
DEEPINFRA_API_TOKEN = getpass()
复制
向 AI 提问
········
复制
向 AI 提问
import os
os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN
复制
向 AI 提问
from langchain_community.embeddings import DeepInfraEmbeddings
复制
向 AI 提问
embeddings = DeepInfraEmbeddings(
model_id="sentence-transformers/clip-ViT-B-32",
query_instruction="",
embed_instruction="",
)
复制
向 AI 提问
docs = ["Dog is not a cat", "Beta is the second letter of Greek alphabet"]
document_result = embeddings.embed_documents(docs)
复制
向 AI 提问
query = "What is the first letter of Greek alphabet"
query_result = embeddings.embed_query(query)
复制
向 AI 提问
import numpy as np
query_numpy = np.array(query_result)
for doc_res, doc in zip(document_result, docs):
document_numpy = np.array(doc_res)
similarity = np.dot(query_numpy, document_numpy) / (
np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy)
)
print(f'Cosine similarity between "{doc}" and query: {similarity}')
复制
向 AI 提问
Cosine similarity between "Dog is not a cat" and query: 0.7489097144129355
Cosine similarity between "Beta is the second letter of Greek alphabet" and query: 0.9519380640702013
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。