|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "afaf8039", |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "raw" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "---\n", |
| 13 | + "sidebar_label: Fireworks\n", |
| 14 | + "---" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "9a3d6f34", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "# FireworksEmbeddings\n", |
| 23 | + "\n", |
| 24 | + "This will help you get started with FireworksEmbeddings [embedding models](/docs/concepts#embedding-models) using LangChain. For detailed documentation on `FireworksEmbeddings` features and configuration options, please refer to the [API reference](https://api.js.langchain.com/classes/langchain_community_embeddings_fireworks.FireworksEmbeddings.html).\n", |
| 25 | + "\n", |
| 26 | + "## Overview\n", |
| 27 | + "### Integration details\n", |
| 28 | + "\n", |
| 29 | + "| Class | Package | Local | [Py support](https://python.langchain.com/docs/integrations/text_embedding/fireworks/) | Package downloads | Package latest |\n", |
| 30 | + "| :--- | :--- | :---: | :---: | :---: | :---: |\n", |
| 31 | + "| [FireworksEmbeddings](https://api.js.langchain.com/classes/langchain_community_embeddings_fireworks.FireworksEmbeddings.html) | [@langchain/community](https://api.js.langchain.com/modules/langchain_community_embeddings_fireworks.html) | ❌ | ✅ |  |  |\n", |
| 32 | + "\n", |
| 33 | + "## Setup\n", |
| 34 | + "\n", |
| 35 | + "To access Fireworks embedding models you'll need to create a Fireworks account, get an API key, and install the `@langchain/community` integration package.\n", |
| 36 | + "\n", |
| 37 | + "### Credentials\n", |
| 38 | + "\n", |
| 39 | + "Head to [fireworks.ai](https://fireworks.ai/) to sign up to `Fireworks` and generate an API key. Once you've done this set the `FIREWORKS_API_KEY` environment variable:\n", |
| 40 | + "\n", |
| 41 | + "```bash\n", |
| 42 | + "export FIREWORKS_API_KEY=\"your-api-key\"\n", |
| 43 | + "```\n", |
| 44 | + "\n", |
| 45 | + "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:\n", |
| 46 | + "\n", |
| 47 | + "```bash\n", |
| 48 | + "# export LANGCHAIN_TRACING_V2=\"true\"\n", |
| 49 | + "# export LANGCHAIN_API_KEY=\"your-api-key\"\n", |
| 50 | + "```\n", |
| 51 | + "\n", |
| 52 | + "### Installation\n", |
| 53 | + "\n", |
| 54 | + "The LangChain `FireworksEmbeddings` integration lives in the `@langchain/community` package:\n", |
| 55 | + "\n", |
| 56 | + "```{=mdx}\n", |
| 57 | + "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n", |
| 58 | + "import Npm2Yarn from \"@theme/Npm2Yarn\";\n", |
| 59 | + "\n", |
| 60 | + "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n", |
| 61 | + "\n", |
| 62 | + "<Npm2Yarn>\n", |
| 63 | + " @langchain/community\n", |
| 64 | + "</Npm2Yarn>\n", |
| 65 | + "```" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "id": "45dd1724", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "## Instantiation\n", |
| 74 | + "\n", |
| 75 | + "Now we can instantiate our model object and generate chat completions:" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 1, |
| 81 | + "id": "9ea7a09b", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "import { FireworksEmbeddings } from \"@langchain/community/embeddings/fireworks\";\n", |
| 86 | + "\n", |
| 87 | + "const embeddings = new FireworksEmbeddings({\n", |
| 88 | + " modelName: \"nomic-ai/nomic-embed-text-v1.5\",\n", |
| 89 | + "});" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "id": "77d271b6", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "## Indexing and Retrieval\n", |
| 98 | + "\n", |
| 99 | + "Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the [working with external knowledge tutorials](/docs/tutorials/#working-with-external-knowledge).\n", |
| 100 | + "\n", |
| 101 | + "Below, see how to index and retrieve data using the `embeddings` object we initialized above. In this example, we will index and retrieve a sample document using the demo [`MemoryVectorStore`](/docs/integrations/vectorstores/memory)." |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 2, |
| 107 | + "id": "d817716b", |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "data": { |
| 112 | + "text/plain": [ |
| 113 | + "\u001b[32m\"LangChain is the framework for building context-aware reasoning applications\"\u001b[39m" |
| 114 | + ] |
| 115 | + }, |
| 116 | + "execution_count": 2, |
| 117 | + "metadata": {}, |
| 118 | + "output_type": "execute_result" |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "// Create a vector store with a sample text\n", |
| 123 | + "import { MemoryVectorStore } from \"langchain/vectorstores/memory\";\n", |
| 124 | + "\n", |
| 125 | + "const text = \"LangChain is the framework for building context-aware reasoning applications\";\n", |
| 126 | + "\n", |
| 127 | + "const vectorstore = await MemoryVectorStore.fromDocuments(\n", |
| 128 | + " [{ pageContent: text, metadata: {} }],\n", |
| 129 | + " embeddings,\n", |
| 130 | + ");\n", |
| 131 | + "\n", |
| 132 | + "// Use the vector store as a retriever that returns a single document\n", |
| 133 | + "const retriever = vectorstore.asRetriever(1);\n", |
| 134 | + "\n", |
| 135 | + "// Retrieve the most similar text\n", |
| 136 | + "const retrievedDocuments = await retriever.invoke(\"What is LangChain?\");\n", |
| 137 | + "\n", |
| 138 | + "retrievedDocuments[0].pageContent;" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "id": "e02b9855", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "## Direct Usage\n", |
| 147 | + "\n", |
| 148 | + "Under the hood, the vectorstore and retriever implementations are calling `embeddings.embedDocument(...)` and `embeddings.embedQuery(...)` to create embeddings for the text(s) used in `fromDocuments` and the retriever's `invoke` operations, respectively.\n", |
| 149 | + "\n", |
| 150 | + "You can directly call these methods to get embeddings for your own use cases.\n", |
| 151 | + "\n", |
| 152 | + "### Embed single texts\n", |
| 153 | + "\n", |
| 154 | + "You can embed queries for search with `embedQuery`. This generates a vector representation specific to the query:" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 3, |
| 160 | + "id": "0d2befcd", |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [ |
| 163 | + { |
| 164 | + "name": "stdout", |
| 165 | + "output_type": "stream", |
| 166 | + "text": [ |
| 167 | + "[\n", |
| 168 | + " 0.01666259765625, 0.011688232421875, -0.1181640625,\n", |
| 169 | + " -0.10205078125, 0.05438232421875, -0.08905029296875,\n", |
| 170 | + " -0.018096923828125, 0.00952911376953125, -0.08056640625,\n", |
| 171 | + " -0.0283050537109375, -0.01512908935546875, 0.0312042236328125,\n", |
| 172 | + " 0.08197021484375, 0.022552490234375, 0.0012683868408203125,\n", |
| 173 | + " 0.0133056640625, -0.04327392578125, -0.004322052001953125,\n", |
| 174 | + " -0.02410888671875, -0.0012350082397460938, -0.04632568359375,\n", |
| 175 | + " 0.02996826171875, -0.0134124755859375, -0.037811279296875,\n", |
| 176 | + " 0.07672119140625, 0.021759033203125, 0.0179290771484375,\n", |
| 177 | + " -0.0002741813659667969, -0.0582275390625, -0.0224456787109375,\n", |
| 178 | + " 0.0027675628662109375, -0.017425537109375, -0.01520538330078125,\n", |
| 179 | + " -0.01146697998046875, -0.055267333984375, -0.083984375,\n", |
| 180 | + " 0.056793212890625, -0.003383636474609375, -0.034271240234375,\n", |
| 181 | + " 0.05108642578125, -0.01018524169921875, 0.0462646484375,\n", |
| 182 | + " 0.0012178421020507812, 0.005779266357421875, 0.0684814453125,\n", |
| 183 | + " 0.00797271728515625, -0.0176544189453125, 0.00257110595703125,\n", |
| 184 | + " 0.059539794921875, -0.06573486328125, -0.075439453125,\n", |
| 185 | + " -0.0247344970703125, -0.0276947021484375, 0.003940582275390625,\n", |
| 186 | + " 0.02630615234375, 0.0660400390625, 0.0157470703125,\n", |
| 187 | + " 0.033050537109375, -0.0478515625, -0.03338623046875,\n", |
| 188 | + " 0.050384521484375, 0.07757568359375, -0.045166015625,\n", |
| 189 | + " 0.07586669921875, 0.0021915435791015625, 0.0237579345703125,\n", |
| 190 | + " -0.052703857421875, 0.05023193359375, -0.0274810791015625,\n", |
| 191 | + " -0.0025081634521484375, 0.019287109375, -0.03802490234375,\n", |
| 192 | + " 0.0216217041015625, 0.025360107421875, -0.04443359375,\n", |
| 193 | + " -0.029205322265625, -0.002414703369140625, 0.027130126953125,\n", |
| 194 | + " 0.028961181640625, 0.078857421875, -0.0009660720825195312,\n", |
| 195 | + " 0.017608642578125, 0.05755615234375, -0.0285797119140625,\n", |
| 196 | + " 0.0039215087890625, -0.006908416748046875, -0.05364990234375,\n", |
| 197 | + " -0.01342010498046875, -0.0247802734375, 0.08331298828125,\n", |
| 198 | + " 0.032928466796875, 0.00543975830078125, -0.0168304443359375,\n", |
| 199 | + " -0.050018310546875, -0.05908203125, 0.031951904296875,\n", |
| 200 | + " -0.0200347900390625, 0.019134521484375, -0.018035888671875,\n", |
| 201 | + " -0.01178741455078125\n", |
| 202 | + "]\n" |
| 203 | + ] |
| 204 | + } |
| 205 | + ], |
| 206 | + "source": [ |
| 207 | + "const singleVector = await embeddings.embedQuery(text);\n", |
| 208 | + "\n", |
| 209 | + "console.log(singleVector.slice(0, 100));" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "id": "1b5a7d03", |
| 215 | + "metadata": {}, |
| 216 | + "source": [ |
| 217 | + "### Embed multiple texts\n", |
| 218 | + "\n", |
| 219 | + "You can embed multiple texts for indexing with `embedDocuments`. The internals used for this method may (but do not have to) differ from embedding queries:" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": 4, |
| 225 | + "id": "2f4d6e97", |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [ |
| 228 | + { |
| 229 | + "name": "stdout", |
| 230 | + "output_type": "stream", |
| 231 | + "text": [ |
| 232 | + "[\n", |
| 233 | + " 0.016632080078125, 0.01165008544921875, -0.1181640625,\n", |
| 234 | + " -0.10186767578125, 0.05438232421875, -0.08905029296875,\n", |
| 235 | + " -0.0180511474609375, 0.00957489013671875, -0.08056640625,\n", |
| 236 | + " -0.0283203125, -0.0151214599609375, 0.0311279296875,\n", |
| 237 | + " 0.08184814453125, 0.0225982666015625, 0.0012750625610351562,\n", |
| 238 | + " 0.01336669921875, -0.043365478515625, -0.004322052001953125,\n", |
| 239 | + " -0.02410888671875, -0.0012559890747070312, -0.046356201171875,\n", |
| 240 | + " 0.0298919677734375, -0.013458251953125, -0.03765869140625,\n", |
| 241 | + " 0.07672119140625, 0.0217132568359375, 0.0179290771484375,\n", |
| 242 | + " -0.0002269744873046875, -0.0582275390625, -0.0224609375,\n", |
| 243 | + " 0.002834320068359375, -0.0174407958984375, -0.01512908935546875,\n", |
| 244 | + " -0.01146697998046875, -0.055206298828125, -0.08404541015625,\n", |
| 245 | + " 0.0567626953125, -0.0033092498779296875, -0.034271240234375,\n", |
| 246 | + " 0.05108642578125, -0.010101318359375, 0.046173095703125,\n", |
| 247 | + " 0.0011806488037109375, 0.005706787109375, 0.06854248046875,\n", |
| 248 | + " 0.0079193115234375, -0.0176239013671875, 0.002552032470703125,\n", |
| 249 | + " 0.059539794921875, -0.06573486328125, -0.07537841796875,\n", |
| 250 | + " -0.02484130859375, -0.027740478515625, 0.003925323486328125,\n", |
| 251 | + " 0.0263671875, 0.0660400390625, 0.0156402587890625,\n", |
| 252 | + " 0.033050537109375, -0.047821044921875, -0.0333251953125,\n", |
| 253 | + " 0.050445556640625, 0.07757568359375, -0.045257568359375,\n", |
| 254 | + " 0.07586669921875, 0.0021724700927734375, 0.0237274169921875,\n", |
| 255 | + " -0.052703857421875, 0.050323486328125, -0.0274658203125,\n", |
| 256 | + " -0.0024662017822265625, 0.0194244384765625, -0.03802490234375,\n", |
| 257 | + " 0.02166748046875, 0.025360107421875, -0.044464111328125,\n", |
| 258 | + " -0.0292816162109375, -0.0025119781494140625, 0.0271148681640625,\n", |
| 259 | + " 0.028961181640625, 0.078857421875, -0.0008907318115234375,\n", |
| 260 | + " 0.017669677734375, 0.0576171875, -0.0285797119140625,\n", |
| 261 | + " 0.0039825439453125, -0.00687408447265625, -0.0535888671875,\n", |
| 262 | + " -0.0134735107421875, -0.0247650146484375, 0.0831298828125,\n", |
| 263 | + " 0.032989501953125, 0.005443572998046875, -0.0167999267578125,\n", |
| 264 | + " -0.050018310546875, -0.059051513671875, 0.0318603515625,\n", |
| 265 | + " -0.0200958251953125, 0.0191192626953125, -0.0180206298828125,\n", |
| 266 | + " -0.01175689697265625\n", |
| 267 | + "]\n", |
| 268 | + "[\n", |
| 269 | + " -0.02667236328125, 0.036651611328125, -0.1630859375,\n", |
| 270 | + " -0.0904541015625, -0.022430419921875, -0.095458984375,\n", |
| 271 | + " -0.037628173828125, 0.00473785400390625, -0.046051025390625,\n", |
| 272 | + " 0.0109710693359375, 0.0113525390625, 0.0254364013671875,\n", |
| 273 | + " 0.09368896484375, 0.0144195556640625, -0.007564544677734375,\n", |
| 274 | + " -0.0014705657958984375, -0.0007691383361816406, -0.015716552734375,\n", |
| 275 | + " -0.0242156982421875, -0.024871826171875, 0.00885009765625,\n", |
| 276 | + " 0.0012922286987304688, 0.023712158203125, -0.054595947265625,\n", |
| 277 | + " 0.06329345703125, 0.0289306640625, 0.0233612060546875,\n", |
| 278 | + " -0.0374755859375, -0.0489501953125, -0.029510498046875,\n", |
| 279 | + " 0.0173492431640625, 0.0171356201171875, -0.01629638671875,\n", |
| 280 | + " -0.0352783203125, -0.039398193359375, -0.11138916015625,\n", |
| 281 | + " 0.0296783447265625, -0.01467132568359375, 0.0009188652038574219,\n", |
| 282 | + " 0.048187255859375, -0.010650634765625, 0.03125,\n", |
| 283 | + " 0.005214691162109375, -0.015869140625, 0.06939697265625,\n", |
| 284 | + " -0.0428466796875, 0.0266571044921875, 0.044189453125,\n", |
| 285 | + " 0.049957275390625, -0.054290771484375, 0.0107574462890625,\n", |
| 286 | + " -0.03265380859375, -0.0109100341796875, -0.0144805908203125,\n", |
| 287 | + " 0.03936767578125, 0.07989501953125, -0.056976318359375,\n", |
| 288 | + " 0.0308380126953125, -0.035125732421875, -0.038848876953125,\n", |
| 289 | + " 0.10748291015625, 0.01129150390625, -0.0665283203125,\n", |
| 290 | + " 0.09710693359375, 0.03143310546875, -0.0104522705078125,\n", |
| 291 | + " -0.062469482421875, 0.021148681640625, -0.00970458984375,\n", |
| 292 | + " -0.06756591796875, 0.01019287109375, 0.00433349609375,\n", |
| 293 | + " 0.032928466796875, 0.020233154296875, -0.01336669921875,\n", |
| 294 | + " -0.015472412109375, -0.0175933837890625, -0.0142364501953125,\n", |
| 295 | + " -0.007450103759765625, 0.03759765625, 0.003551483154296875,\n", |
| 296 | + " 0.0069580078125, 0.042266845703125, -0.007488250732421875,\n", |
| 297 | + " 0.01922607421875, 0.007080078125, -0.0255889892578125,\n", |
| 298 | + " -0.007686614990234375, -0.0848388671875, 0.058563232421875,\n", |
| 299 | + " 0.021148681640625, 0.034393310546875, 0.01087188720703125,\n", |
| 300 | + " -0.0196380615234375, -0.09515380859375, 0.0054931640625,\n", |
| 301 | + " -0.012481689453125, 0.003322601318359375, -0.019683837890625,\n", |
| 302 | + " -0.0307159423828125\n", |
| 303 | + "]\n" |
| 304 | + ] |
| 305 | + } |
| 306 | + ], |
| 307 | + "source": [ |
| 308 | + "const text2 = \"LangGraph is a library for building stateful, multi-actor applications with LLMs\";\n", |
| 309 | + "\n", |
| 310 | + "const vectors = await embeddings.embedDocuments([text, text2]);\n", |
| 311 | + "\n", |
| 312 | + "console.log(vectors[0].slice(0, 100));\n", |
| 313 | + "console.log(vectors[1].slice(0, 100));" |
| 314 | + ] |
| 315 | + }, |
| 316 | + { |
| 317 | + "cell_type": "markdown", |
| 318 | + "id": "8938e581", |
| 319 | + "metadata": {}, |
| 320 | + "source": [ |
| 321 | + "## API reference\n", |
| 322 | + "\n", |
| 323 | + "For detailed documentation of all FireworksEmbeddings features and configurations head to the API reference: https://api.js.langchain.com/classes/langchain_community_embeddings_fireworks.FireworksEmbeddings.html" |
| 324 | + ] |
| 325 | + } |
| 326 | + ], |
| 327 | + "metadata": { |
| 328 | + "kernelspec": { |
| 329 | + "display_name": "Deno", |
| 330 | + "language": "typescript", |
| 331 | + "name": "deno" |
| 332 | + }, |
| 333 | + "language_info": { |
| 334 | + "file_extension": ".ts", |
| 335 | + "mimetype": "text/x.typescript", |
| 336 | + "name": "typescript", |
| 337 | + "nb_converter": "script", |
| 338 | + "pygments_lexer": "typescript", |
| 339 | + "version": "5.3.3" |
| 340 | + } |
| 341 | + }, |
| 342 | + "nbformat": 4, |
| 343 | + "nbformat_minor": 5 |
| 344 | +} |
0 commit comments