From 3b886cdbb2b22dbab6d05cb1d502077fddd550df Mon Sep 17 00:00:00 2001 From: Jorge Piedrahita Ortiz Date: Mon, 27 Jan 2025 15:34:55 -0500 Subject: [PATCH] libs: add sambanova-lagchain integration package (#29417) - **Description:**: Add sambanova-langchain integration package as suggested in previous PRs --------- Co-authored-by: Chester Curme --- docs/docs/integrations/chat/sambanova.ipynb | 39 ++-- docs/docs/integrations/chat/sambastudio.ipynb | 38 ++-- .../integrations/providers/sambanova.ipynb | 147 ++++++++++++ .../text_embedding/sambanova.ipynb | 210 ++++++++++++++---- .../chat_models/sambanova.py | 11 + .../embeddings/sambanova.py | 6 + libs/packages.yml | 4 + 7 files changed, 370 insertions(+), 85 deletions(-) create mode 100644 docs/docs/integrations/providers/sambanova.ipynb diff --git a/docs/docs/integrations/chat/sambanova.ipynb b/docs/docs/integrations/chat/sambanova.ipynb index aa8b9154accab..4ea2b151dfb66 100644 --- a/docs/docs/integrations/chat/sambanova.ipynb +++ b/docs/docs/integrations/chat/sambanova.ipynb @@ -19,7 +19,7 @@ "source": [ "# ChatSambaNovaCloud\n", "\n", - "This will help you getting started with SambaNovaCloud [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatSambaNovaCloud features and configurations head to the [API reference](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaNovaCloud.html).\n", + "This will help you getting started with SambaNovaCloud [chat models](/docs/concepts/chat_models/). For detailed documentation of all ChatSambaNovaCloud features and configurations head to the [API reference](https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.ChatSambaNovaCloud.html).\n", "\n", "**[SambaNova](https://sambanova.ai/)'s** [SambaNova Cloud](https://cloud.sambanova.ai/) is a platform for performing inference with open-source models\n", "\n", @@ -28,21 +28,20 @@ "\n", "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", - "| [ChatSambaNovaCloud](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaNovaCloud.html) | [langchain-community](https://python.langchain.com/api_reference/community/index.html) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_community?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_community?style=flat-square&label=%20) |\n", + "| [ChatSambaNovaCloud](https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.ChatSambaNovaCloud.html) | [langchain-community](https://python.langchain.com/api_reference/community/index.html) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_sambanova?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_sambanova?style=flat-square&label=%20) |\n", "\n", "### Model features\n", "\n", - "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", + "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](//docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", - "| ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n", + "| ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | \n", "\n", "## Setup\n", "\n", - "To access ChatSambaNovaCloud models you will need to create a [SambaNovaCloud](https://cloud.sambanova.ai/) account, get an API key, install the `langchain_community` integration package, and install the `SSEClient` Package.\n", + "To access ChatSambaNovaCloud models you will need to create a [SambaNovaCloud](https://cloud.sambanova.ai/) account, get an API key, install the `langchain_sambanova` integration package.\n", "\n", "```bash\n", - "pip install langchain-community\n", - "pip install sseclient-py\n", + "pip install langchain-sambanova\n", "```\n", "\n", "### Credentials\n", @@ -82,8 +81,8 @@ "metadata": {}, "outputs": [], "source": [ - "# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n", - "# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" + "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" ] }, { @@ -92,7 +91,7 @@ "source": [ "### Installation\n", "\n", - "The LangChain __SambaNovaCloud__ integration lives in the `langchain_community` package:" + "The LangChain __SambaNovaCloud__ integration lives in the `langchain_sambanova` package:" ] }, { @@ -101,8 +100,7 @@ "metadata": {}, "outputs": [], "source": [ - "%pip install -qU langchain-community\n", - "%pip install -qu sseclient-py" + "%pip install -qU langchain-sambanova" ] }, { @@ -120,13 +118,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_community.chat_models.sambanova import ChatSambaNovaCloud\n", + "from langchain_sambanova import ChatSambaNovaCloud\n", "\n", "llm = ChatSambaNovaCloud(\n", - " model=\"Meta-Llama-3.1-70B-Instruct\",\n", + " model=\"Meta-Llama-3.3-70B-Instruct\",\n", " max_tokens=1024,\n", " temperature=0.7,\n", - " top_k=1,\n", " top_p=0.01,\n", ")" ] @@ -158,7 +155,8 @@ "messages = [\n", " (\n", " \"system\",\n", - " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", + " \"You are a helpful assistant that translates English to French. \"\n", + " \"Translate the user sentence.\",\n", " ),\n", " (\"human\", \"I love programming.\"),\n", "]\n", @@ -215,7 +213,8 @@ " [\n", " (\n", " \"system\",\n", - " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", + " \"You are a helpful assistant that translates {input_language} \"\n", + " \"to {output_language}.\",\n", " ),\n", " (\"human\", \"{input}\"),\n", " ]\n", @@ -412,7 +411,8 @@ "llm_with_tools = llm.bind_tools(tools=tools)\n", "messages = [\n", " HumanMessage(\n", - " content=\"I need to schedule a meeting for two weeks from today. Can you tell me the exact date of the meeting?\"\n", + " content=\"I need to schedule a meeting for two weeks from today. \"\n", + " \"Can you tell me the exact date of the meeting?\"\n", " )\n", "]" ] @@ -499,7 +499,6 @@ " model=\"Llama-3.2-11B-Vision-Instruct\",\n", " max_tokens=1024,\n", " temperature=0.7,\n", - " top_k=1,\n", " top_p=0.01,\n", ")" ] @@ -546,7 +545,7 @@ "source": [ "## API reference\n", "\n", - "For detailed documentation of all ChatSambaNovaCloud features and configurations head to the API reference: https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaNovaCloud.html" + "For detailed documentation of all ChatSambaNovaCloud features and configurations head to the API reference: https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.ChatSambaNovaCloud.html" ] } ], diff --git a/docs/docs/integrations/chat/sambastudio.ipynb b/docs/docs/integrations/chat/sambastudio.ipynb index d75734890e9d8..87660522ebdd7 100644 --- a/docs/docs/integrations/chat/sambastudio.ipynb +++ b/docs/docs/integrations/chat/sambastudio.ipynb @@ -19,7 +19,7 @@ "source": [ "# ChatSambaStudio\n", "\n", - "This will help you getting started with SambaStudio [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatStudio features and configurations head to the [API reference](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaStudio.html).\n", + "This will help you getting started with SambaStudio [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatStudio features and configurations head to the [API reference](https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.chat_models.sambanova.ChatSambaStudio.html).\n", "\n", "**[SambaNova](https://sambanova.ai/)'s** [SambaStudio](https://docs.sambanova.ai/sambastudio/latest/sambastudio-intro.html) SambaStudio is a rich, GUI-based platform that provides the functionality to train, deploy, and manage models in SambaNova [DataScale](https://sambanova.ai/products/datascale) systems.\n", "\n", @@ -28,21 +28,20 @@ "\n", "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", - "| [ChatSambaStudio](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaStudio.html) | [langchain-community](https://python.langchain.com/api_reference/community/index.html) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_community?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_community?style=flat-square&label=%20) |\n", + "| [ChatSambaStudio](https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.chat_models.sambanova.ChatSambaStudio.html) | [langchain-community](https://python.langchain.com/api_reference/community/index.html) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_sambanova?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_sambanova?style=flat-square&label=%20) |\n", "\n", "### Model features\n", "\n", "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", - "| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n", + "| ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | \n", "\n", "## Setup\n", "\n", - "To access ChatSambaStudio models you will need to [deploy an endpoint](https://docs.sambanova.ai/sambastudio/latest/language-models.html) in your SambaStudio platform, install the `langchain_community` integration package, and install the `SSEClient` Package.\n", + "To access ChatSambaStudio models you will need to [deploy an endpoint](https://docs.sambanova.ai/sambastudio/latest/language-models.html) in your SambaStudio platform, install the `langchain_sambanova` integration package.\n", "\n", "```bash\n", - "pip install langchain-community\n", - "pip install sseclient-py\n", + "pip install langchain-sambanova\n", "```\n", "\n", "### Credentials\n", @@ -50,7 +49,7 @@ "Get the URL and API Key from your SambaStudio deployed endpoint and add them to your environment variables:\n", "\n", "``` bash\n", - "export SAMBASTUDIO_URL=\"your-api-key-here\"\n", + "export SAMBASTUDIO_URL=\"sambastudio-url-key-here\"\n", "export SAMBASTUDIO_API_KEY=\"your-api-key-here\"\n", "```" ] @@ -85,8 +84,8 @@ "metadata": {}, "outputs": [], "source": [ - "# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n", - "# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" + "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" ] }, { @@ -95,7 +94,7 @@ "source": [ "### Installation\n", "\n", - "The LangChain __SambaStudio__ integration lives in the `langchain_community` package:" + "The LangChain __SambaStudio__ integration lives in the `langchain_sambanova` package:" ] }, { @@ -104,8 +103,7 @@ "metadata": {}, "outputs": [], "source": [ - "%pip install -qU langchain-community\n", - "%pip install -qu sseclient-py" + "%pip install -qU langchain-sambanova" ] }, { @@ -123,13 +121,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_community.chat_models.sambanova import ChatSambaStudio\n", + "from langchain_sambanova import ChatSambaStudio\n", "\n", "llm = ChatSambaStudio(\n", " model=\"Meta-Llama-3-70B-Instruct-4096\", # set if using a Bundle endpoint\n", " max_tokens=1024,\n", " temperature=0.7,\n", - " top_k=1,\n", " top_p=0.01,\n", " do_sample=True,\n", " process_prompt=\"True\", # set if using a Bundle endpoint\n", @@ -163,7 +160,8 @@ "messages = [\n", " (\n", " \"system\",\n", - " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", + " \"You are a helpful assistant that translates English to French.\"\n", + " \"Translate the user sentence.\",\n", " ),\n", " (\"human\", \"I love programming.\"),\n", "]\n", @@ -220,7 +218,8 @@ " [\n", " (\n", " \"system\",\n", - " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", + " \"You are a helpful assistant that translates {input_language} \"\n", + " \"to {output_language}.\",\n", " ),\n", " (\"human\", \"{input}\"),\n", " ]\n", @@ -364,7 +363,7 @@ "source": [ "from datetime import datetime\n", "\n", - "from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage\n", + "from langchain_core.messages import HumanMessage, ToolMessage\n", "from langchain_core.tools import tool\n", "\n", "\n", @@ -408,7 +407,8 @@ "llm_with_tools = llm.bind_tools(tools=tools)\n", "messages = [\n", " HumanMessage(\n", - " content=\"I need to schedule a meeting for two weeks from today. Can you tell me the exact date of the meeting?\"\n", + " content=\"I need to schedule a meeting for two weeks from today. \"\n", + " \"Can you tell me the exact date of the meeting?\"\n", " )\n", "]" ] @@ -483,7 +483,7 @@ "source": [ "## API reference\n", "\n", - "For detailed documentation of all ChatSambaStudio features and configurations head to the API reference: https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.sambanova.ChatSambaStudio.html" + "For detailed documentation of all ChatSambaStudio features and configurations head to the API reference: https://python.langchain.com/api_reference/sambanova/chat_models/langchain_sambanova.sambanova.chat_models.ChatSambaStudio.html" ] } ], diff --git a/docs/docs/integrations/providers/sambanova.ipynb b/docs/docs/integrations/providers/sambanova.ipynb new file mode 100644 index 0000000000000..572bd7b3d081a --- /dev/null +++ b/docs/docs/integrations/providers/sambanova.ipynb @@ -0,0 +1,147 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SambaNova\n", + "\n", + "Customers are turning to [SambaNova](https://sambanova.ai/) to quickly deploy state-of-the-art AI capabilities to gain competitive advantage. Our purpose-built enterprise-scale AI platform is the technology backbone for the next generation of AI computing. We power the foundation models that unlock the valuable business insights trapped in data.\n", + "\n", + "Designed for AI, the SambaNova RDU was built with a revolutionary dataflow architecture. This design makes the RDU significantly more efficient for these workloads than GPUs as it eliminates redundant calls to memory, which are an inherent limitation of how GPUs function. This built-in efficiency is one of the features that makes the RDU capable of much higher performance than GPUs in a fraction of the footprint.\n", + "\n", + "On top of our architecture We have developed some platforms that allow companies and developers to get full advantage of the RDU processors and open source models." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### SambaNovaCloud\n", + "\n", + "SambaNova's [SambaNova Cloud](https://cloud.sambanova.ai/) is a platform for performing inference with open-source models\n", + "\n", + "You can obtain a free SambaNovaCloud API key [here](https://cloud.sambanova.ai/)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### SambaStudio\n", + "\n", + "SambaNova's [SambaStudio](https://docs.sambanova.ai/sambastudio/latest/sambastudio-intro.html) is a rich, GUI-based platform that provides the functionality to train, deploy, and manage models in SambaNova DataScale systems." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Installation and Setup\n", + "\n", + "Install the integration package:\n", + "\n", + "```bash\n", + "pip install langchain-sambanova\n", + "```\n", + "\n", + "set your API key it as an environment variable:\n", + "\n", + "If you are a SambaNovaCloud user:\n", + "\n", + "```bash\n", + "export SAMBANOVA_API_KEY=\"your-sambanova-cloud-api-key-here\"\n", + "```\n", + "\n", + "or if you are SambaStudio User\n", + "\n", + "```bash\n", + "export SAMBASTUDIO_API_KEY=\"your-sambastudio-api-key-here\"\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Chat models" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "y8ku6X96sebl" + }, + "outputs": [], + "source": [ + "from langchain_sambanova import ChatSambaNovaCloud\n", + "\n", + "llm = ChatSambaNovaCloud(model=\"Meta-Llama-3.3-70B-Instruct\", temperature=0.7)\n", + "llm.invoke(\"Tell me a joke about artificial intelligence.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_sambanova import ChatSambaStudio\n", + "\n", + "llm = ChatSambaStudio(model=\"Meta-Llama-3.3-70B-Instruct\", temperature=0.7)\n", + "llm.invoke(\"Tell me a joke about artificial intelligence.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Embedding Models" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_sambanova import SambaStudioEmbeddings\n", + "\n", + "embeddings = SambaStudioEmbeddings(model=\"e5-mistral-7b-instruct\")\n", + "embeddings.embed_query(\"What is the meaning of life?\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "API Reference [langchain-sambanova](https://python.langchain.com/api_reference/sambanova/index.html)" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/docs/docs/integrations/text_embedding/sambanova.ipynb b/docs/docs/integrations/text_embedding/sambanova.ipynb index 6d3e81c317b2c..2f4fe9c8512ac 100644 --- a/docs/docs/integrations/text_embedding/sambanova.ipynb +++ b/docs/docs/integrations/text_embedding/sambanova.ipynb @@ -1,137 +1,255 @@ { "cells": [ + { + "cell_type": "raw", + "id": "afaf8039", + "metadata": { + "vscode": { + "languageId": "raw" + } + }, + "source": [ + "---\n", + "sidebar_label: SambaStudio\n", + "---" + ] + }, { "cell_type": "markdown", + "id": "9a3d6f34", "metadata": {}, "source": [ - "# SambaNova\n", + "# SambaStudioEmbeddings\n", + "\n", + "This will help you get started with SambaNova's SambaStudio embedding models using LangChain. For detailed documentation on `SambaStudioEmbeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/api_reference/sambanova/embeddings/langchain_sambanova.embeddingsSambaStudioEmbeddings.html).\n", "\n", "**[SambaNova](https://sambanova.ai/)'s** [Sambastudio](https://sambanova.ai/technology/full-stack-ai-platform) is a platform for running your own open-source models\n", "\n", - "This example goes over how to use LangChain to interact with SambaNova embedding models" + "## Overview\n", + "### Integration details\n", + "\n", + "| Provider | Package |\n", + "|:--------:|:-------:|\n", + "| [SambaNova](/docs/integrations/providers/sambanova/) | [langchain-sambanova](https://python.langchain.com/api_reference/langchain_sambanova/embeddings/langchain_sambanova.embeddings.SambaStudioEmbeddings.html) |\n", + "\n", + "## Setup\n", + "\n", + "To access ChatSambaStudio models you will need to [deploy an endpoint](https://docs.sambanova.ai/sambastudio/latest/language-models.html) in your SambaStudio platform, install the `langchain_sambanova` integration package.\n", + "\n", + "```bash\n", + "pip install langchain-sambanova\n", + "```\n", + "\n", + "### Credentials\n", + "\n", + "Get the URL and API Key from your SambaStudio deployed endpoint and add them to your environment variables:\n", + "\n", + "``` bash\n", + "export SAMBASTUDIO_URL=\"sambastudio-url-key-here\"\n", + "export SAMBASTUDIO_API_KEY=\"your-api-key-here\"\n", + "```" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, + "id": "36521c2a", "metadata": {}, + "outputs": [], "source": [ - "## SambaStudio\n", + "import getpass\n", + "import os\n", "\n", - "**SambaStudio** allows you to train, run batch inference jobs, and deploy online inference endpoints to run open source models that you fine tuned yourself." + "if not os.getenv(\"SAMBASTUDIO_API_KEY\"):\n", + " os.environ[\"SAMBASTUDIO_API_KEY\"] = getpass.getpass(\n", + " \"Enter your SambaNova API key: \"\n", + " )" ] }, { "cell_type": "markdown", + "id": "c84fb993", + "metadata": {}, + "source": [ + "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:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39a4953b", "metadata": {}, + "outputs": [], "source": [ - "A SambaStudio environment is required to deploy a model. Get more information at [sambanova.ai/products/enterprise-ai-platform-sambanova-suite](https://sambanova.ai/products/enterprise-ai-platform-sambanova-suite)" + "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" ] }, { "cell_type": "markdown", + "id": "d9664366", "metadata": {}, "source": [ - "Register your environment variables:" + "### Installation\n", + "\n", + "The LangChain SambaNova integration lives in the `langchain-sambanova` package:" ] }, { "cell_type": "code", "execution_count": null, + "id": "64853226", "metadata": {}, "outputs": [], "source": [ - "import os\n", + "%pip install -qU langchain-sambanova" + ] + }, + { + "cell_type": "markdown", + "id": "45dd1724", + "metadata": {}, + "source": [ + "## Instantiation\n", "\n", - "sambastudio_base_url = \"\"\n", - "sambastudio_base_uri = \"\"\n", - "sambastudio_project_id = \"\"\n", - "sambastudio_endpoint_id = \"\"\n", - "sambastudio_api_key = \"\"\n", + "Now we can instantiate our model object and generate chat completions:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ea7a09b", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_sambanova import SambaStudioEmbeddings\n", "\n", - "# Set the environment variables\n", - "os.environ[\"SAMBASTUDIO_EMBEDDINGS_BASE_URL\"] = sambastudio_base_url\n", - "os.environ[\"SAMBASTUDIO_EMBEDDINGS_BASE_URI\"] = sambastudio_base_uri\n", - "os.environ[\"SAMBASTUDIO_EMBEDDINGS_PROJECT_ID\"] = sambastudio_project_id\n", - "os.environ[\"SAMBASTUDIO_EMBEDDINGS_ENDPOINT_ID\"] = sambastudio_endpoint_id\n", - "os.environ[\"SAMBASTUDIO_EMBEDDINGS_API_KEY\"] = sambastudio_api_key" + "embeddings = SambaStudioEmbeddings(\n", + " model=\"e5-mistral-7b-instruct\",\n", + ")" ] }, { "cell_type": "markdown", + "id": "77d271b6", "metadata": {}, "source": [ - "Call SambaStudio hosted embeddings directly from LangChain!" + "## Indexing and Retrieval\n", + "\n", + "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](/docs/tutorials/).\n", + "\n", + "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 in the `InMemoryVectorStore`." ] }, { "cell_type": "code", "execution_count": null, + "id": "d817716b", "metadata": {}, "outputs": [], "source": [ - "from langchain_community.embeddings.sambanova import SambaStudioEmbeddings\n", + "# Create a vector store with a sample text\n", + "from langchain_core.vectorstores import InMemoryVectorStore\n", + "\n", + "text = \"LangChain is the framework for building context-aware reasoning applications\"\n", "\n", - "embeddings = SambaStudioEmbeddings()\n", + "vectorstore = InMemoryVectorStore.from_texts(\n", + " [text],\n", + " embedding=embeddings,\n", + ")\n", "\n", - "text = \"Hello, this is a test\"\n", - "result = embeddings.embed_query(text)\n", - "print(result)\n", + "# Use the vectorstore as a retriever\n", + "retriever = vectorstore.as_retriever()\n", "\n", - "texts = [\"Hello, this is a test\", \"Hello, this is another test\"]\n", - "results = embeddings.embed_documents(texts)\n", - "print(results)" + "# Retrieve the most similar text\n", + "retrieved_documents = retriever.invoke(\"What is LangChain?\")\n", + "\n", + "# show the retrieved document's content\n", + "retrieved_documents[0].page_content" ] }, { "cell_type": "markdown", + "id": "e02b9855", "metadata": {}, "source": [ - "You can manually pass the endpoint parameters and manually set the batch size you have in your SambaStudio embeddings endpoint" + "## Direct Usage\n", + "\n", + "Under the hood, the vectorstore and retriever implementations are calling `embeddings.embed_documents(...)` and `embeddings.embed_query(...)` to create embeddings for the text(s) used in `from_texts` and retrieval `invoke` operations, respectively.\n", + "\n", + "You can directly call these methods to get embeddings for your own use cases.\n", + "\n", + "### Embed single texts\n", + "\n", + "You can embed single texts or documents with `embed_query`:" ] }, { "cell_type": "code", "execution_count": null, + "id": "0d2befcd", "metadata": {}, "outputs": [], "source": [ - "embeddings = SambaStudioEmbeddings(\n", - " sambastudio_embeddings_base_url=sambastudio_base_url,\n", - " sambastudio_embeddings_base_uri=sambastudio_base_uri,\n", - " sambastudio_embeddings_project_id=sambastudio_project_id,\n", - " sambastudio_embeddings_endpoint_id=sambastudio_endpoint_id,\n", - " sambastudio_embeddings_api_key=sambastudio_api_key,\n", - " batch_size=32, # set depending on the deployed endpoint configuration\n", - ")" + "single_vector = embeddings.embed_query(text)\n", + "print(str(single_vector)[:100]) # Show the first 100 characters of the vector" ] }, { "cell_type": "markdown", + "id": "1b5a7d03", "metadata": {}, "source": [ - "Or You can use an embedding model expert included in your deployed CoE" + "### Embed multiple texts\n", + "\n", + "You can embed multiple texts with `embed_documents`:" ] }, { "cell_type": "code", "execution_count": null, + "id": "2f4d6e97", "metadata": {}, "outputs": [], "source": [ - "embeddings = SambaStudioEmbeddings(\n", - " batch_size=1,\n", - " model_kwargs={\n", - " \"select_expert\": \"e5-mistral-7b-instruct\",\n", - " },\n", - ")" + "text2 = (\n", + " \"LangGraph is a library for building stateful, multi-actor applications with LLMs\"\n", + ")\n", + "two_vectors = embeddings.embed_documents([text, text2])\n", + "for vector in two_vectors:\n", + " print(str(vector)[:100]) # Show the first 100 characters of the vector" + ] + }, + { + "cell_type": "markdown", + "id": "98785c12", + "metadata": {}, + "source": [ + "## API Reference\n", + "\n", + "For detailed documentation on `SambaNovaEmbeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/api_reference/langchain_sambanova/embeddings/langchain_sambanova.embeddings.SambaStudioEmbeddings.html).\n" ] } ], "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.5" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 5 } diff --git a/libs/community/langchain_community/chat_models/sambanova.py b/libs/community/langchain_community/chat_models/sambanova.py index e345c0f6920b2..0e21395f4ff09 100644 --- a/libs/community/langchain_community/chat_models/sambanova.py +++ b/libs/community/langchain_community/chat_models/sambanova.py @@ -16,6 +16,7 @@ ) import requests +from langchain_core._api.deprecation import deprecated from langchain_core.callbacks import ( CallbackManagerForLLMRun, ) @@ -107,6 +108,11 @@ def _is_pydantic_class(obj: Any) -> bool: return isinstance(obj, type) and is_basemodel_subclass(obj) +@deprecated( + since="0.3.16", + removal="1.0", + alternative_import="langchain_sambanova.ChatSambaNovaCloud", +) class ChatSambaNovaCloud(BaseChatModel): """ SambaNova Cloud chat model. @@ -952,6 +958,11 @@ def _stream( yield chunk +@deprecated( + since="0.3.16", + removal="1.0", + alternative_import="langchain_sambanova.ChatSambaStudio", +) class ChatSambaStudio(BaseChatModel): """ SambaStudio chat model. diff --git a/libs/community/langchain_community/embeddings/sambanova.py b/libs/community/langchain_community/embeddings/sambanova.py index e23760b43182c..e9d76cae87835 100644 --- a/libs/community/langchain_community/embeddings/sambanova.py +++ b/libs/community/langchain_community/embeddings/sambanova.py @@ -2,11 +2,17 @@ from typing import Dict, Generator, List, Optional import requests +from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings from langchain_core.utils import get_from_dict_or_env, pre_init from pydantic import BaseModel, ConfigDict +@deprecated( + since="0.3.16", + removal="1.0", + alternative_import="langchain_sambanova.SambaStudioEmbeddings", +) class SambaStudioEmbeddings(BaseModel, Embeddings): """SambaNova embedding models. diff --git a/libs/packages.yml b/libs/packages.yml index bdfa36a9375ff..d63ab580bb284 100644 --- a/libs/packages.yml +++ b/libs/packages.yml @@ -361,3 +361,7 @@ packages: repo: paymanai/langchain-payman-tool downloads: 0 downloads_updated_at: "2025-01-22T00:00:00+00:00" +- name: langchain-sambanova + repo: sambanova/langchain-sambanova + path: . + downloads: 0