FireworksEmbeddings
这将帮助您开始使用 FireworksEmbeddings 嵌入模型 使用 LangChain。有关 FireworksEmbeddings
功能和配置选项的详细文档,请参阅 API 参考。
概述
集成详细信息
类 | 包 | 本地 | Py 支持 | 包下载 | 包最新 |
---|---|---|---|---|---|
FireworksEmbeddings | @langchain/community | ❌ | ✅ |
设置
要访问 Fireworks 嵌入模型,您需要创建一个 Fireworks 帐户,获取 API 密钥,并安装 @langchain/community
集成包。
凭据
前往 fireworks.ai 注册 Fireworks
并生成 API 密钥。完成此操作后,设置 FIREWORKS_API_KEY
环境变量
export FIREWORKS_API_KEY="your-api-key"
如果您希望自动跟踪您的模型调用,也可以通过取消下面的注释来设置您的 LangSmith API 密钥
# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"
安装
LangChain FireworksEmbeddings
集成位于 @langchain/community
包中
请参阅 本节了解有关安装集成包的常规说明。
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
实例化
现在,我们可以实例化我们的模型对象并生成聊天补全
import { FireworksEmbeddings } from "@langchain/community/embeddings/fireworks";
const embeddings = new FireworksEmbeddings({
modelName: "nomic-ai/nomic-embed-text-v1.5",
});
索引和检索
嵌入模型通常用于检索增强生成 (RAG) 流中,既作为索引数据的组成部分,也作为稍后检索它的组成部分。有关更详细的说明,请参阅我们 使用外部知识教程 下的 RAG 教程。
下面,请查看如何使用我们在上面初始化的 embeddings
对象来索引和检索数据。在此示例中,我们将使用演示 MemoryVectorStore
来索引和检索示例文档。
// Create a vector store with a sample text
import { MemoryVectorStore } from "langchain/vectorstores/memory";
const text =
"LangChain is the framework for building context-aware reasoning applications";
const vectorstore = await MemoryVectorStore.fromDocuments(
[{ pageContent: text, metadata: {} }],
embeddings
);
// Use the vector store as a retriever that returns a single document
const retriever = vectorstore.asRetriever(1);
// Retrieve the most similar text
const retrievedDocuments = await retriever.invoke("What is LangChain?");
retrievedDocuments[0].pageContent;
"LangChain is the framework for building context-aware reasoning applications"
直接使用
在幕后,向量存储和检索器实现分别调用 embeddings.embedDocument(...)
和 embeddings.embedQuery(...)
为 fromDocuments
中使用的文本以及检索器的 invoke
操作中使用的文本创建嵌入。
您可以直接调用这些方法来获取用于您自己的用例的嵌入。
嵌入单个文本
您可以使用 embedQuery
来嵌入查询以进行搜索。这将生成一个特定于查询的向量表示
const singleVector = await embeddings.embedQuery(text);
console.log(singleVector.slice(0, 100));
[
0.01666259765625, 0.011688232421875, -0.1181640625,
-0.10205078125, 0.05438232421875, -0.08905029296875,
-0.018096923828125, 0.00952911376953125, -0.08056640625,
-0.0283050537109375, -0.01512908935546875, 0.0312042236328125,
0.08197021484375, 0.022552490234375, 0.0012683868408203125,
0.0133056640625, -0.04327392578125, -0.004322052001953125,
-0.02410888671875, -0.0012350082397460938, -0.04632568359375,
0.02996826171875, -0.0134124755859375, -0.037811279296875,
0.07672119140625, 0.021759033203125, 0.0179290771484375,
-0.0002741813659667969, -0.0582275390625, -0.0224456787109375,
0.0027675628662109375, -0.017425537109375, -0.01520538330078125,
-0.01146697998046875, -0.055267333984375, -0.083984375,
0.056793212890625, -0.003383636474609375, -0.034271240234375,
0.05108642578125, -0.01018524169921875, 0.0462646484375,
0.0012178421020507812, 0.005779266357421875, 0.0684814453125,
0.00797271728515625, -0.0176544189453125, 0.00257110595703125,
0.059539794921875, -0.06573486328125, -0.075439453125,
-0.0247344970703125, -0.0276947021484375, 0.003940582275390625,
0.02630615234375, 0.0660400390625, 0.0157470703125,
0.033050537109375, -0.0478515625, -0.03338623046875,
0.050384521484375, 0.07757568359375, -0.045166015625,
0.07586669921875, 0.0021915435791015625, 0.0237579345703125,
-0.052703857421875, 0.05023193359375, -0.0274810791015625,
-0.0025081634521484375, 0.019287109375, -0.03802490234375,
0.0216217041015625, 0.025360107421875, -0.04443359375,
-0.029205322265625, -0.002414703369140625, 0.027130126953125,
0.028961181640625, 0.078857421875, -0.0009660720825195312,
0.017608642578125, 0.05755615234375, -0.0285797119140625,
0.0039215087890625, -0.006908416748046875, -0.05364990234375,
-0.01342010498046875, -0.0247802734375, 0.08331298828125,
0.032928466796875, 0.00543975830078125, -0.0168304443359375,
-0.050018310546875, -0.05908203125, 0.031951904296875,
-0.0200347900390625, 0.019134521484375, -0.018035888671875,
-0.01178741455078125
]
嵌入多个文本
您可以使用 embedDocuments
来嵌入多个文本以进行索引。用于此方法的内部机制可能(但不必)与嵌入查询不同
const text2 =
"LangGraph is a library for building stateful, multi-actor applications with LLMs";
const vectors = await embeddings.embedDocuments([text, text2]);
console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
[
0.016632080078125, 0.01165008544921875, -0.1181640625,
-0.10186767578125, 0.05438232421875, -0.08905029296875,
-0.0180511474609375, 0.00957489013671875, -0.08056640625,
-0.0283203125, -0.0151214599609375, 0.0311279296875,
0.08184814453125, 0.0225982666015625, 0.0012750625610351562,
0.01336669921875, -0.043365478515625, -0.004322052001953125,
-0.02410888671875, -0.0012559890747070312, -0.046356201171875,
0.0298919677734375, -0.013458251953125, -0.03765869140625,
0.07672119140625, 0.0217132568359375, 0.0179290771484375,
-0.0002269744873046875, -0.0582275390625, -0.0224609375,
0.002834320068359375, -0.0174407958984375, -0.01512908935546875,
-0.01146697998046875, -0.055206298828125, -0.08404541015625,
0.0567626953125, -0.0033092498779296875, -0.034271240234375,
0.05108642578125, -0.010101318359375, 0.046173095703125,
0.0011806488037109375, 0.005706787109375, 0.06854248046875,
0.0079193115234375, -0.0176239013671875, 0.002552032470703125,
0.059539794921875, -0.06573486328125, -0.07537841796875,
-0.02484130859375, -0.027740478515625, 0.003925323486328125,
0.0263671875, 0.0660400390625, 0.0156402587890625,
0.033050537109375, -0.047821044921875, -0.0333251953125,
0.050445556640625, 0.07757568359375, -0.045257568359375,
0.07586669921875, 0.0021724700927734375, 0.0237274169921875,
-0.052703857421875, 0.050323486328125, -0.0274658203125,
-0.0024662017822265625, 0.0194244384765625, -0.03802490234375,
0.02166748046875, 0.025360107421875, -0.044464111328125,
-0.0292816162109375, -0.0025119781494140625, 0.0271148681640625,
0.028961181640625, 0.078857421875, -0.0008907318115234375,
0.017669677734375, 0.0576171875, -0.0285797119140625,
0.0039825439453125, -0.00687408447265625, -0.0535888671875,
-0.0134735107421875, -0.0247650146484375, 0.0831298828125,
0.032989501953125, 0.005443572998046875, -0.0167999267578125,
-0.050018310546875, -0.059051513671875, 0.0318603515625,
-0.0200958251953125, 0.0191192626953125, -0.0180206298828125,
-0.01175689697265625
]
[
-0.02667236328125, 0.036651611328125, -0.1630859375,
-0.0904541015625, -0.022430419921875, -0.095458984375,
-0.037628173828125, 0.00473785400390625, -0.046051025390625,
0.0109710693359375, 0.0113525390625, 0.0254364013671875,
0.09368896484375, 0.0144195556640625, -0.007564544677734375,
-0.0014705657958984375, -0.0007691383361816406, -0.015716552734375,
-0.0242156982421875, -0.024871826171875, 0.00885009765625,
0.0012922286987304688, 0.023712158203125, -0.054595947265625,
0.06329345703125, 0.0289306640625, 0.0233612060546875,
-0.0374755859375, -0.0489501953125, -0.029510498046875,
0.0173492431640625, 0.0171356201171875, -0.01629638671875,
-0.0352783203125, -0.039398193359375, -0.11138916015625,
0.0296783447265625, -0.01467132568359375, 0.0009188652038574219,
0.048187255859375, -0.010650634765625, 0.03125,
0.005214691162109375, -0.015869140625, 0.06939697265625,
-0.0428466796875, 0.0266571044921875, 0.044189453125,
0.049957275390625, -0.054290771484375, 0.0107574462890625,
-0.03265380859375, -0.0109100341796875, -0.0144805908203125,
0.03936767578125, 0.07989501953125, -0.056976318359375,
0.0308380126953125, -0.035125732421875, -0.038848876953125,
0.10748291015625, 0.01129150390625, -0.0665283203125,
0.09710693359375, 0.03143310546875, -0.0104522705078125,
-0.062469482421875, 0.021148681640625, -0.00970458984375,
-0.06756591796875, 0.01019287109375, 0.00433349609375,
0.032928466796875, 0.020233154296875, -0.01336669921875,
-0.015472412109375, -0.0175933837890625, -0.0142364501953125,
-0.007450103759765625, 0.03759765625, 0.003551483154296875,
0.0069580078125, 0.042266845703125, -0.007488250732421875,
0.01922607421875, 0.007080078125, -0.0255889892578125,
-0.007686614990234375, -0.0848388671875, 0.058563232421875,
0.021148681640625, 0.034393310546875, 0.01087188720703125,
-0.0196380615234375, -0.09515380859375, 0.0054931640625,
-0.012481689453125, 0.003322601318359375, -0.019683837890625,
-0.0307159423828125
]
API 参考
有关 FireworksEmbeddings 所有功能和配置的详细文档,请前往 API 参考:https://api.js.langchain.com/classes/langchain_community_embeddings_fireworks.FireworksEmbeddings.html