Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: sambanovacloud llm integration #27526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bf71ad9
add sambanova cloud llm integration
jhpiedrahitao Oct 21, 2024
5d868a9
Merge branch 'langchain-ai:master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Oct 21, 2024
207d035
add sambanova cloud llm integration
jhpiedrahitao Oct 21, 2024
75d06f5
docs notebook added
jhpiedrahitao Oct 21, 2024
bf45ddc
minor fix imports
jhpiedrahitao Oct 21, 2024
92466d0
docstrings updated
jhpiedrahitao Oct 21, 2024
5d7cbf9
docs updated
jhpiedrahitao Oct 21, 2024
ee9ba31
hotfix docs
jhpiedrahitao Oct 22, 2024
ac4fd79
hotfix docs
jhpiedrahitao Oct 22, 2024
12dfa2e
Merge branch 'langchain-ai:master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Oct 23, 2024
27e1d6e
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Oct 26, 2024
212de72
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 4, 2024
c8cebe4
fix deleted method in merge
jhpiedrahitao Nov 4, 2024
cf5f814
fmt
jhpiedrahitao Nov 4, 2024
eae7f03
fmt
jhpiedrahitao Nov 4, 2024
c14a1ac
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 6, 2024
e6206c3
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 7, 2024
cb5f431
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 11, 2024
6083c59
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 14, 2024
6e47d27
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 14, 2024
fcad5f1
Merge branch 'master' into snova-jorgep/sambanovacloud_llm
jhpiedrahitao Nov 14, 2024
eb02cd5
fix links
ccurme Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 253 additions & 0 deletions docs/docs/integrations/llms/sambanovacloud.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SambaNovaCloud\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",
":::caution\n",
"You are currently on a page documenting the use of SambaNovaCloud models as [text completion models](/docs/concepts/text_llms/). We recommend you to use the [chat completion models](/docs/concepts/chat_models).\n",
"\n",
"You may be looking for [SambaNovaCloud Chat Models](/docs/integrations/chat/sambanova/) .\n",
":::\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [SambaNovaCloud](https://python.langchain.com/api_reference/community/llms/langchain_community.llms.sambanova.SambaNovaCloud.html) | [langchain_community](https://python.langchain.com/api_reference/community/index.html) | ❌ | beta | ❌ | ![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",
"\n",
"This example goes over how to use LangChain to interact with SambaNovaCloud models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"### Credentials\n",
"To access ChatSambaNovaCloud models you will need to create a [SambaNovaCloud account](https://cloud.sambanova.ai/), get an API key and set it as the `SAMBANOVA_API_KEY` environment variable:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if \"SAMBANOVA_API_KEY\" not in os.environ:\n",
" os.environ[\"SAMBANOVA_API_KEY\"] = getpass.getpass()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The integration lives in the `langchain-community` package. We also need to install the [sseclient-py](https://pypi.org/project/sseclient-py/) package this is required to run streaming predictions "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --quiet -U langchain-community sseclient-py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.llms.sambanova import SambaNovaCloud\n",
"\n",
"llm = SambaNovaCloud(\n",
" model=\"Meta-Llama-3.1-70B-Instruct\",\n",
" max_tokens_to_generate=1000,\n",
" temperature=0.01,\n",
" # top_k = 50,\n",
" # top_p = 1.0\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"**Advantages of Open Source Models**\\n\\nUsing open source models can bring numerous benefits to your project or organization. Here are some reasons why you should consider using open source models:\\n\\n### 1. **Cost-Effective**\\n\\nOpen source models are free to use, modify, and distribute. This can significantly reduce the costs associated with developing and maintaining proprietary models.\\n\\n### 2. **Community Support**\\n\\nOpen source models are often maintained by a community of developers and users who contribute to their improvement. This community support can lead to faster bug fixes, new feature additions, and better documentation.\\n\\n### 3. **Transparency and Customizability**\\n\\nOpen source models provide complete transparency into their architecture and implementation. This allows you to customize and fine-tune the model to suit your specific needs.\\n\\n### 4. **Faster Development**\\n\\nBy leveraging pre-trained open source models, you can accelerate your development process. You can focus on fine-tuning the model for your specific use case rather than building one from scratch.\\n\\n### 5. **Improved Security**\\n\\nOpen source models are often reviewed and audited by a large community of developers, which can help identify and fix security vulnerabilities.\\n\\n### 6. **Interoperability**\\n\\nOpen source models can be easily integrated with other open source tools and frameworks, promoting interoperability and reducing vendor lock-in.\\n\\n### 7. **Access to State-of-the-Art Technology**\\n\\nMany open source models are developed by top researchers and institutions, providing access to state-of-the-art technology and techniques.\\n\\n### Example Use Cases\\n\\n* **Computer Vision**: Use open source models like TensorFlow's Object Detection API or OpenCV's pre-trained models for image classification, object detection, and segmentation tasks.\\n* **Natural Language Processing**: Leverage open source models like spaCy or Stanford CoreNLP for text processing, sentiment analysis, and language translation tasks.\\n* **Speech Recognition**: Utilize open source models like Kaldi or Mozilla's DeepSpeech for speech-to-text applications.\\n\\n**Getting Started**\\n\\nTo get started with open source models, explore popular repositories on GitHub or model hubs like TensorFlow Hub or PyTorch Hub. Familiarize yourself with the model's documentation, and experiment with pre-trained models before fine-tuning them for your specific use case.\\n\\nBy embracing open source models, you can accelerate your development process, reduce costs, and tap into the collective knowledge of the developer community.\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_text = \"Why should I use open source models?\"\n",
"\n",
"completion = llm.invoke(input_text)\n",
"completion"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"**Advantages of Open Source Models**\n",
"\n",
"Using open source models can bring numerous benefits to your projects. Here are some reasons why you should consider them:\n",
"\n",
"### 1. **Cost-Effective**\n",
"\n",
"Open source models are free to use, modify, and distribute. This can significantly reduce the costs associated with developing and maintaining proprietary models.\n",
"\n",
"### 2. **Community Support**\n",
"\n",
"Open source models are often maintained by a community of developers, researchers, and users. This community can provide support, fix bugs, and contribute to the model's improvement.\n",
"\n",
"### 3. **Transparency and Reproducibility**\n",
"\n",
"Open source models are transparent in their architecture, training data, and hyperparameters. This transparency allows for reproducibility, which is essential in scientific research and development.\n",
"\n",
"### 4. **Customizability**\n",
"\n",
"Open source models can be modified to suit specific use cases or requirements. This customizability enables developers to adapt the model to their needs, which can lead to better performance and accuracy.\n",
"\n",
"### 5. **Faster Development**\n",
"\n",
"Using open source models can accelerate development by providing a pre-trained foundation. This allows developers to focus on fine-tuning the model for their specific task, rather than starting from scratch.\n",
"\n",
"### 6. **Access to State-of-the-Art Models**\n",
"\n",
"Open source models often represent the state-of-the-art in their respective domains. By using these models, developers can leverage the latest advancements in AI research.\n",
"\n",
"### 7. **Reduced Vendor Lock-in**\n",
"\n",
"Open source models are not tied to a specific vendor or platform. This reduces the risk of vendor lock-in and allows developers to switch to alternative solutions if needed.\n",
"\n",
"### Example Use Cases\n",
"\n",
"* **Computer Vision**: Using open source models like YOLO (You Only Look Once) or SSD (Single Shot Detector) for object detection tasks.\n",
"* **Natural Language Processing**: Leveraging open source models like BERT (Bidirectional Encoder Representations from Transformers) or RoBERTa (Robustly Optimized BERT Pretraining Approach) for text classification, sentiment analysis, or language translation.\n",
"* **Speech Recognition**: Utilizing open source models like Kaldi or Mozilla DeepSpeech for speech-to-text applications.\n",
"\n",
"**Getting Started**\n",
"\n",
"To get started with open source models, you can explore popular repositories on GitHub or model hubs like the TensorFlow Model Garden or the PyTorch Model Zoo. These resources provide pre-trained models, documentation, and tutorials to help you integrate open source models into your projects.\n",
"\n",
"By embracing open source models, you can tap into the collective knowledge and expertise of the developer community, accelerate your development process, and create more accurate and efficient AI solutions."
]
}
],
"source": [
"# Streaming response\n",
"for chunk in llm.stream(\"Why should I use open source models?\"):\n",
" print(chunk, end=\"\", flush=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chaining\n",
"We can chain our completion model with a prompt template like so:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The translation of \"I love programming\" in German is:\\n\\n\"Ich liebe das Programmieren.\"\\n\\nHere\\'s a breakdown of the sentence:\\n\\n* \"Ich\" means \"I\"\\n* \"liebe\" is the verb \"to love\" in the first person singular (I love)\\n* \"das\" is the definite article for \"Programmieren\" (programming)\\n* \"Programmieren\" is the verb \"to program\" in the infinitive form, but in this context, it\\'s used as a noun to refer to the activity of programming.\\n\\nSo, \"Ich liebe das Programmieren\" is a common way to express your passion for programming in German.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import PromptTemplate\n",
"\n",
"prompt = PromptTemplate.from_template(\"How to say {input} in {output_language}:\\n\")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all `SambaNovaCloud` llm features and configurations head to the API reference: https://python.langchain.com/api_reference/community/llms/langchain_community.llms.sambanova.SambaNovaCloud.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "multimodalenv",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
10 changes: 10 additions & 0 deletions libs/community/langchain_community/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ def _import_sagemaker_endpoint() -> Type[BaseLLM]:
return SagemakerEndpoint


def _import_sambanovacloud() -> Type[BaseLLM]:
from langchain_community.llms.sambanova import SambaNovaCloud

return SambaNovaCloud


def _import_sambastudio() -> Type[BaseLLM]:
from langchain_community.llms.sambanova import SambaStudio

Expand Down Expand Up @@ -821,6 +827,8 @@ def __getattr__(name: str) -> Any:
return _import_rwkv()
elif name == "SagemakerEndpoint":
return _import_sagemaker_endpoint()
elif name == "SambaNovaCloud":
return _import_sambanovacloud()
elif name == "SambaStudio":
return _import_sambastudio()
elif name == "SelfHostedPipeline":
Expand Down Expand Up @@ -957,6 +965,7 @@ def __getattr__(name: str) -> Any:
"RWKV",
"Replicate",
"SagemakerEndpoint",
"SambaNovaCloud",
"SambaStudio",
"SelfHostedHuggingFaceLLM",
"SelfHostedPipeline",
Expand Down Expand Up @@ -1054,6 +1063,7 @@ def get_type_to_cls_dict() -> Dict[str, Callable[[], Type[BaseLLM]]]:
"replicate": _import_replicate,
"rwkv": _import_rwkv,
"sagemaker_endpoint": _import_sagemaker_endpoint,
"sambanovacloud": _import_sambanovacloud,
"sambastudio": _import_sambastudio,
"self_hosted": _import_self_hosted,
"self_hosted_hugging_face": _import_self_hosted_hugging_face,
Expand Down
Loading
Loading