Skip to main content

MosaicML

MosaicML 提供了一个托管的推理服务。您可以使用各种开源模型,或部署您自己的模型。

本示例介绍了如何使用 LangChain 与 MosaicML 推理进行文本嵌入。

# sign up for an account: https://forms.mosaicml.com/demo?utm_source=langchain

from getpass import getpass

MOSAICML_API_TOKEN = getpass()
import os

os.environ["MOSAICML_API_TOKEN"] = MOSAICML_API_TOKEN
<!--IMPORTS:[{"imported": "MosaicMLInstructorEmbeddings", "source": "langchain_community.embeddings", "docs": "https://python.langchain.com/api_reference/community/embeddings/langchain_community.embeddings.mosaicml.MosaicMLInstructorEmbeddings.html", "title": "MosaicML"}]-->
from langchain_community.embeddings import MosaicMLInstructorEmbeddings
embeddings = MosaicMLInstructorEmbeddings(
query_instruction="Represent the query for retrieval: "
)
query_text = "This is a test query."
query_result = embeddings.embed_query(query_text)
document_text = "This is a test document."
document_result = embeddings.embed_documents([document_text])
import numpy as np

query_numpy = np.array(query_result)
document_numpy = np.array(document_result[0])
similarity = np.dot(query_numpy, document_numpy) / (
np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy)
)
print(f"Cosine similarity between document and query: {similarity}")

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书