dapr-agents/cookbook/llm/nvidia_embeddings_basic.ipynb

261 lines
6.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LLM: NVIDIA Embeddings Endpoint Basic Examples\n",
"\n",
"This notebook demonstrates how to use the `NVIDIAEmbedder` in `dapr-agents` for generating text embeddings. We will explore:\n",
"\n",
"* Initializing the `NVIDIAEmbedder`.\n",
"* Generating embeddings for single and multiple inputs.\n",
"* Using the class both as a direct function and via its `embed` method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install Required Libraries\n",
"Before starting, ensure the required libraries are installed:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install dapr-agents python-dotenv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Environment Variables\n",
"\n",
"Load API keys or other configuration values from your `.env` file using `dotenv`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import NVIDIAEmbedder"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from dapr_agents.document.embedder import NVIDIAEmbedder"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize the NVIDIAEmbedder\n",
"\n",
"To start, create an instance of the `NVIDIAEmbedder` class."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Initialize the embedder\n",
"embedder = NVIDIAEmbedder(\n",
" model=\"nvidia/nv-embedqa-e5-v5\", # Default embedding model\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Embedding a Single Text\n",
"\n",
"You can use the embed method to generate an embedding for a single input string."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Embedding (first 5 values): [-0.007270217100869654, -0.03521439888521964, 0.008612880489907491, 0.03619088134997443, 0.03658757735128107]\n"
]
}
],
"source": [
"# Input text\n",
"text = \"The quick brown fox jumps over the lazy dog.\"\n",
"\n",
"# Generate embedding\n",
"embedding = embedder.embed(text)\n",
"\n",
"# Display the embedding\n",
"print(f\"Embedding (first 5 values): {embedding[:5]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Embedding Multiple Texts\n",
"\n",
"The embed method also supports embedding multiple texts at once."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text 1 embedding (first 5 values): [-0.007270217100869654, -0.03521439888521964, 0.008612880489907491, 0.03619088134997443, 0.03658757735128107]\n",
"Text 2 embedding (first 5 values): [0.03491632278487177, -0.045598764196327295, 0.014955417976037734, 0.049291836798573345, 0.03741906620126992]\n"
]
}
],
"source": [
"# Input texts\n",
"texts = [\n",
" \"The quick brown fox jumps over the lazy dog.\",\n",
" \"A journey of a thousand miles begins with a single step.\"\n",
"]\n",
"\n",
"# Generate embeddings\n",
"embeddings = embedder.embed(texts)\n",
"\n",
"# Display the embeddings\n",
"for i, emb in enumerate(embeddings):\n",
" print(f\"Text {i + 1} embedding (first 5 values): {emb[:5]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using the NVIDIAEmbedder as a Callable Function\n",
"\n",
"The `NVIDIAEmbedder` class can also be used directly as a function, thanks to its `__call__` implementation."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Embedding (first 5 values): [-0.005809799816153762, -0.08734154733463988, -0.017593431879252233, 0.027511671880565285, 0.001342777107870075]\n"
]
}
],
"source": [
"# Use the class instance as a callable\n",
"text_embedding = embedder(\"A stitch in time saves nine.\")\n",
"\n",
"# Display the embedding\n",
"print(f\"Embedding (first 5 values): {text_embedding[:5]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For multiple inputs:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text 1 embedding (first 5 values): [0.021093917798446042, -0.04365205548745667, 0.02008726662368289, 0.024922242720651362, 0.024556187748010216]\n",
"Text 2 embedding (first 5 values): [-0.006683721130524534, -0.05764852452568794, 0.01164408689824411, 0.04627132894469238, 0.03458911471541276]\n"
]
}
],
"source": [
"text_list = [\"The early bird catches the worm.\", \"An apple a day keeps the doctor away.\"]\n",
"embeddings_list = embedder(text_list)\n",
"\n",
"# Display the embeddings\n",
"for i, emb in enumerate(embeddings_list):\n",
" print(f\"Text {i + 1} embedding (first 5 values): {emb[:5]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}