深度基础设施嵌入
¥DeepInfra Embeddings
DeepInfraEmbeddings
类使用 DeepInfra API 为给定文本输入生成嵌入向量。本指南将指导你设置和使用 DeepInfraEmbeddings
类,帮助你将其无缝集成到项目中。
¥The DeepInfraEmbeddings
class utilizes the DeepInfra API to generate embeddings for given text inputs. This guide will walk you through the setup and usage of the DeepInfraEmbeddings
class, helping you integrate it into your project seamlessly.
安装
¥Installation
按如下所示安装 @langchain/community
包:
¥Install the @langchain/community
package as shown below:
- npm
- Yarn
- pnpm
npm i @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
初始化
¥Initialization
通过此集成,你可以使用 DeepInfra 嵌入模型获取文本数据的嵌入。下面是嵌入模型的 link。
¥With this integration, you can use the DeepInfra embeddings model to get embeddings for your text data. Here is the link to the embeddings models.
首先,你需要在 DeepInfra 网站上注册并从 此处 获取 API 令牌。你可以从模型卡中复制名称并在代码中使用它们。
¥First, you need to sign up on the DeepInfra website and get the API token from here. You can copy names from the model cards and start using them in your code.
要使用 DeepInfraEmbeddings
类,你需要一个来自 DeepInfra 的 API 令牌。你可以将此令牌直接传递给构造函数,也可以将其设置为环境变量 (DEEPINFRA_API_TOKEN
)。
¥To use the DeepInfraEmbeddings
class, you need an API token from DeepInfra. You can pass this token directly to the constructor or set it as an environment variable (DEEPINFRA_API_TOKEN
).
基本用法
¥Basic Usage
以下是如何创建 DeepInfraEmbeddings
实例:
¥Here’s how to create an instance of DeepInfraEmbeddings
:
import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra";
const embeddings = new DeepInfraEmbeddings({
apiToken: "YOUR_API_TOKEN",
modelName: "sentence-transformers/clip-ViT-B-32", // Optional, defaults to "sentence-transformers/clip-ViT-B-32"
batchSize: 1024, // Optional, defaults to 1024
});
如果未提供 apiToken
,则会从 DEEPINFRA_API_TOKEN
环境变量中读取。
¥If the apiToken
is not provided, it will be read from the DEEPINFRA_API_TOKEN
environment variable.
生成嵌入
¥Generating Embeddings
嵌入单个查询
¥Embedding a Single Query
要为单个文本查询生成嵌入,请使用 embedQuery
方法:
¥To generate embeddings for a single text query, use the embedQuery
method:
const embedding = await embeddings.embedQuery(
"What would be a good company name for a company that makes colorful socks?"
);
console.log(embedding);
嵌入多个文档
¥Embedding Multiple Documents
要为多个文档生成嵌入,请使用 embedDocuments
方法。此方法将根据 batchSize
参数自动处理批处理:
¥To generate embeddings for multiple documents, use the embedDocuments
method. This method will handle batching automatically based on the batchSize
parameter:
const documents = [
"Document 1 text...",
"Document 2 text...",
"Document 3 text...",
];
const embeddingsArray = await embeddings.embedDocuments(documents);
console.log(embeddingsArray);
自定义请求
¥Customizing Requests
你可以通过传递 configuration
参数来自定义 SDK 发送请求的基本 URL:
¥You can customize the base URL the SDK sends requests to by passing a configuration
parameter:
const customEmbeddings = new DeepInfraEmbeddings({
apiToken: "YOUR_API_TOKEN",
configuration: {
baseURL: "https://your_custom_url.com",
},
});
如果需要,这允许你通过自定义端点路由请求。
¥This allows you to route requests through a custom endpoint if needed.
错误处理
¥Error Handling
如果未提供 API 令牌,且在环境变量中找不到该令牌,则会抛出错误:
¥If the API token is not provided and cannot be found in the environment variables, an error will be thrown:
try {
const embeddings = new DeepInfraEmbeddings();
} catch (error) {
console.error("DeepInfra API token not found");
}
示例
¥Example
以下是如何设置和使用 DeepInfraEmbeddings
类的完整示例:
¥Here’s a complete example of how to set up and use the DeepInfraEmbeddings
class:
import { DeepInfraEmbeddings } from "@langchain/community/embeddings/deepinfra";
const embeddings = new DeepInfraEmbeddings({
apiToken: "YOUR_API_TOKEN",
modelName: "sentence-transformers/clip-ViT-B-32",
batchSize: 512,
});
async function runExample() {
const queryEmbedding = await embeddings.embedQuery("Example query text.");
console.log("Query Embedding:", queryEmbedding);
const documents = ["Text 1", "Text 2", "Text 3"];
const documentEmbeddings = await embeddings.embedDocuments(documents);
console.log("Document Embeddings:", documentEmbeddings);
}
runExample();
反馈和支持
¥Feedback and Support
如有反馈或疑问,请联系 feedback@deepinfra.com。
¥For feedback or questions, please contact feedback@deepinfra.com.
相关
¥Related
嵌入模型 概念指南
¥Embedding model conceptual guide
嵌入模型 操作指南
¥Embedding model how-to guides