Skip to main content

DeepInfra

DeepInfra 是一个无服务器推理服务,提供对各种LLM模型嵌入模型的访问。本笔记本介绍如何在DeepInfra中使用LangChain进行文本嵌入。

# 注册账号:https://deepinfra.com/login?utm_source=langchain

from getpass import getpass

DEEPINFRA_API_TOKEN = getpass()
     ········
import os

os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN
from langchain.embeddings import DeepInfraEmbeddings
embeddings = DeepInfraEmbeddings(
model_id="sentence-transformers/clip-ViT-B-32",
query_instruction="",
embed_instruction="",
)
docs = ["Dog is not a cat", "Beta is the second letter of Greek alphabet"]
document_result = embeddings.embed_documents(docs)
query = "What is the first letter of Greek alphabet"
query_result = embeddings.embed_query(query)
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'"{doc}"与查询的余弦相似度:{similarity}')
    "Dog is not a cat"与查询的余弦相似度:0.7489097144129355
"Beta is the second letter of Greek alphabet"与查询的余弦相似度:0.9519380640702013