Jina 嵌入
¥Jina Embeddings
JinaEmbeddings
类使用 Jina API 为给定文本输入生成嵌入向量。本指南将指导你设置和使用 JinaEmbeddings
类,帮助你将其无缝集成到项目中。
¥The JinaEmbeddings
class utilizes the Jina API to generate embeddings for given text inputs. This guide will walk you through the setup and usage of the JinaEmbeddings
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
通过此集成,你可以使用 Jina 嵌入模型获取文本数据的嵌入。下面是嵌入模型的 link。
¥With this integration, you can use the Jina embeddings model to get embeddings for your text data. Here is the link to the embeddings models.
首先,你需要在 Jina 网站上注册并从 此处 获取 API 令牌。你可以从 api 测试环境中的下拉菜单中复制模型名称。
¥First, you need to sign up on the Jina website and get the API token from here. You can copy model names from the dropdown in the api playground.
要使用 JinaEmbeddings
类,你需要一个来自 Jina 的 API 令牌。你可以将此令牌直接传递给构造函数,也可以将其设置为环境变量 (JINA_API_KEY
)。
¥To use the JinaEmbeddings
class, you need an API token from Jina. You can pass this token directly to the constructor or set it as an environment variable (JINA_API_KEY
).
基本用法
¥Basic Usage
以下是如何创建 JinaEmbeddings
实例:
¥Here’s how to create an instance of JinaEmbeddings
:
import { JinaEmbeddings } from "@langchain/community/embeddings/jina";
const embeddings = new JinaEmbeddings({
apiKey: "YOUR_API_TOKEN",
model: "jina-clip-v2", // Optional, defaults to "jina-clip-v2"
});
如果未提供 apiKey
,则会从 JINA_API_KEY
环境变量中读取。
¥If the apiKey
is not provided, it will be read from the JINA_API_KEY
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
方法。
¥To generate embeddings for multiple documents, use the embedDocuments
method.
import { localImageToBase64 } from "@langchain/community/utils/local_image_to_base64";
const documents = [
"hello",
{
text: "hello",
},
{
image: "https://i.ibb.co/nQNGqL0/beach1.jpg",
},
{
image: await localImageToBase64("beach1.jpg"),
},
];
const embeddingsArray = await embeddings.embedDocuments(documents);
console.log(embeddingsArray);
错误处理
¥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 JinaEmbeddings();
} catch (error) {
console.error("Jina API token not found");
}
示例
¥Example
以下是如何设置和使用 JinaEmbeddings
类的完整示例:
¥Here’s a complete example of how to set up and use the JinaEmbeddings
class:
import { JinaEmbeddings } from "@langchain/community/embeddings/jina";
import { localImageToBase64 } from "@langchain/community/embeddings/jina/util";
const embeddings = new JinaEmbeddings({
apiKey: "YOUR_API_TOKEN",
model: "jina-embeddings-v2-base-en",
});
async function runExample() {
const queryEmbedding = await embeddings.embedQuery("Example query text.");
console.log("Query Embedding:", queryEmbedding);
const documents = [
"hello",
{
text: "hello",
},
{
image: "https://i.ibb.co/nQNGqL0/beach1.jpg",
},
{
image: await localImageToBase64("beach1.jpg"),
},
];
const documentEmbeddings = await embeddings.embedDocuments(documents);
console.log("Document Embeddings:", documentEmbeddings);
}
runExample();
反馈和支持
¥Feedback and Support
如有反馈或疑问,请联系 support@jina.ai。
¥For feedback or questions, please contact support@jina.ai.
相关
¥Related
嵌入模型 概念指南
¥Embedding model conceptual guide
嵌入模型 操作指南
¥Embedding model how-to guides