diff --git a/docs/docs/integrations/chat/modelscope_chat_endpoint.ipynb b/docs/docs/integrations/chat/modelscope_chat_endpoint.ipynb new file mode 100644 index 0000000000000..4cbf9e959d699 --- /dev/null +++ b/docs/docs/integrations/chat/modelscope_chat_endpoint.ipynb @@ -0,0 +1,247 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: ModelScope\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "e49f1e0d", + "metadata": {}, + "source": [ + "# ModelScopeChatEndpoint\n", + "\n", + "\n", + "ModelScope ([Home](https://www.modelscope.cn/) | [GitHub](https://github.com/modelscope/modelscope)) is built upon the notion of “Model-as-a-Service” (MaaS). It seeks to bring together most advanced machine learning models from the AI community, and streamlines the process of leveraging AI models in real-world applications. The core ModelScope library open-sourced in this repository provides the interfaces and implementations that allow developers to perform model inference, training and evaluation. \n", + "\n", + "This will help you getting started with ModelScope Chat Endpoint.\n", + "\n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "|Provider| Class | Package | Local | Serializable | Package downloads | Package latest |\n", + "|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n", + "|[ModelScope](/docs/integrations/providers/modelscope/)| ModelScopeChatEndpoint | [langchain-modelscope-integration](https://pypi.org/project/langchain-modelscope-integration/) | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-modelscope-integration?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-modelscope-integration?style=flat-square&label=%20) |\n", + "\n", + "\n", + "## Setup\n", + "\n", + "To access ModelScope chat endpoint you'll need to create a ModelScope account, get an SDK token, and install the `langchain-modelscope-integration` integration package.\n", + "\n", + "### Credentials\n", + "\n", + "Head to [ModelScope](https://modelscope.cn/) to sign up to ModelScope and generate an [SDK token](https://modelscope.cn/my/myaccesstoken). Once you've done this set the `MODELSCOPE_SDK_TOKEN` environment variable:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "if not os.getenv(\"MODELSCOPE_SDK_TOKEN\"):\n", + " os.environ[\"MODELSCOPE_SDK_TOKEN\"] = getpass.getpass(\n", + " \"Enter your ModelScope SDK token: \"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "0730d6a1-c893-4840-9817-5e5251676d5d", + "metadata": {}, + "source": [ + "### Installation\n", + "\n", + "The LangChain ModelScope integration lives in the `langchain-modelscope-integration` package:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "652d6238-1f87-422a-b135-f5abbb8652fc", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -qU langchain-modelscope-integration" + ] + }, + { + "cell_type": "markdown", + "id": "a38cde65-254d-4219-a441-068766c0d4b5", + "metadata": {}, + "source": [ + "## Instantiation\n", + "\n", + "Now we can instantiate our model object and generate chat completions:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_modelscope import ModelScopeChatEndpoint\n", + "\n", + "llm = ModelScopeChatEndpoint(\n", + " model=\"Qwen/Qwen2.5-Coder-32B-Instruct\",\n", + " temperature=0,\n", + " max_tokens=1024,\n", + " timeout=60,\n", + " max_retries=2,\n", + " # other params...\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "2b4f3e15", + "metadata": {}, + "source": [ + "## Invocation\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "62e0dbc3", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='我喜欢编程。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 3, 'prompt_tokens': 33, 'total_tokens': 36, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'qwen2.5-coder-32b-instruct', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-60bb3461-60ae-4c0b-8997-ab55ef77fcd6-0', usage_metadata={'input_tokens': 33, 'output_tokens': 3, 'total_tokens': 36, 'input_token_details': {}, 'output_token_details': {}})" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "messages = [\n", + " (\n", + " \"system\",\n", + " \"You are a helpful assistant that translates English to Chinese. Translate the user sentence.\",\n", + " ),\n", + " (\"human\", \"I love programming.\"),\n", + "]\n", + "ai_msg = llm.invoke(messages)\n", + "ai_msg" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "我喜欢编程。\n" + ] + } + ], + "source": [ + "print(ai_msg.content)" + ] + }, + { + "cell_type": "markdown", + "id": "18e2bfc0-7e78-4528-a73f-499ac150dca8", + "metadata": {}, + "source": [ + "## Chaining\n", + "\n", + "We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='我喜欢编程。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 3, 'prompt_tokens': 28, 'total_tokens': 31, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'qwen2.5-coder-32b-instruct', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-9f011a3a-9a11-4759-8d16-5b1843a78862-0', usage_metadata={'input_tokens': 28, 'output_tokens': 3, 'total_tokens': 31, 'input_token_details': {}, 'output_token_details': {}})" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate\n", + "\n", + "prompt = ChatPromptTemplate(\n", + " [\n", + " (\n", + " \"system\",\n", + " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", + " ),\n", + " (\"human\", \"{input}\"),\n", + " ]\n", + ")\n", + "\n", + "chain = prompt | llm\n", + "chain.invoke(\n", + " {\n", + " \"input_language\": \"English\",\n", + " \"output_language\": \"Chinese\",\n", + " \"input\": \"I love programming.\",\n", + " }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3", + "metadata": {}, + "source": [ + "## API reference\n", + "\n", + "For detailed documentation of all ModelScopeChatEndpoint features and configurations head to the reference: https://modelscope.cn/docs/model-service/API-Inference/intro\n" + ] + } + ], + "metadata": { + "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.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/docs/integrations/llms/modelscope_endpoint.ipynb b/docs/docs/integrations/llms/modelscope_endpoint.ipynb new file mode 100644 index 0000000000000..e4bd19a368dd1 --- /dev/null +++ b/docs/docs/integrations/llms/modelscope_endpoint.ipynb @@ -0,0 +1,294 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "67db2992", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: ModelScope\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "9597802c", + "metadata": {}, + "source": [ + "# ModelScopeEndpoint\n", + "\n", + "ModelScope ([Home](https://www.modelscope.cn/) | [GitHub](https://github.com/modelscope/modelscope)) is built upon the notion of “Model-as-a-Service” (MaaS). It seeks to bring together most advanced machine learning models from the AI community, and streamlines the process of leveraging AI models in real-world applications. The core ModelScope library open-sourced in this repository provides the interfaces and implementations that allow developers to perform model inference, training and evaluation. This will help you get started with ModelScope completion models (LLMs) using LangChain.\n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "| Provider | Class | Package | Local | Serializable | Package downloads | Package latest |\n", + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", + "| [ModelScope](/docs/integrations/providers/modelscope/) | ModelScopeEndpoint | [langchain-modelscope-integration](https://pypi.org/project/langchain-modelscope-integration/) | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-modelscope-integration?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-modelscope-integration?style=flat-square&label=%20) |\n", + "\n", + "\n", + "## Setup\n", + "\n", + "To access ModelScope models you'll need to create a ModelScope account, get an SDK token, and install the `langchain-modelscope-integration` integration package.\n", + "\n", + "### Credentials\n", + "\n", + "\n", + "Head to [ModelScope](https://modelscope.cn/) to sign up to ModelScope and generate an [SDK token](https://modelscope.cn/my/myaccesstoken). Once you've done this set the `MODELSCOPE_SDK_TOKEN` environment variable:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bc51e756", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "if not os.getenv(\"MODELSCOPE_SDK_TOKEN\"):\n", + " os.environ[\"MODELSCOPE_SDK_TOKEN\"] = getpass.getpass(\n", + " \"Enter your ModelScope SDK token: \"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "809c6577", + "metadata": {}, + "source": [ + "### Installation\n", + "\n", + "The LangChain ModelScope integration lives in the `langchain-modelscope-integration` package:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59c710c4", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -qU langchain-modelscope-integration" + ] + }, + { + "cell_type": "markdown", + "id": "0a760037", + "metadata": {}, + "source": [ + "## Instantiation\n", + "\n", + "Now we can instantiate our model object and generate chat completions:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a0562a13", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_modelscope import ModelScopeEndpoint\n", + "\n", + "llm = ModelScopeEndpoint(\n", + " model=\"Qwen/Qwen2.5-Coder-32B-Instruct\",\n", + " temperature=0,\n", + " max_tokens=1024,\n", + " timeout=60,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "0ee90032", + "metadata": {}, + "source": [ + "## Invocation\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "035dea0f", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Certainly! Quick sort is a popular and efficient sorting algorithm that uses a divide-and-conquer approach to sort elements. Below is a simple implementation of the Quick Sort algorithm in Python:\\n\\n```python\\ndef quick_sort(arr):\\n # Base case: if the array is empty or has one element, it\\'s already sorted\\n if len(arr) <= 1:\\n return arr\\n else:\\n # Choose a pivot element from the array\\n pivot = arr[len(arr) // 2]\\n \\n # Partition the array into three parts:\\n # - elements less than the pivot\\n # - elements equal to the pivot\\n # - elements greater than the pivot\\n less_than_pivot = [x for x in arr if x < pivot]\\n equal_to_pivot = [x for x in arr if x == pivot]\\n greater_than_pivot = [x for x in arr if x > pivot]\\n \\n # Recursively apply quick_sort to the less_than_pivot and greater_than_pivot subarrays\\n return quick_sort(less_than_pivot) + equal_to_pivot + quick_sort(greater_than_pivot)\\n\\n# Example usage:\\narr = [3, 6, 8, 10, 1, 2, 1]\\nsorted_arr = quick_sort(arr)\\nprint(\"Sorted array:\", sorted_arr)\\n```\\n\\n### Explanation:\\n1. **Base Case**: If the array has one or zero elements, it is already sorted, so we return it as is.\\n2. **Pivot Selection**: We choose the middle element of the array as the pivot. This is a simple strategy, but there are other strategies for choosing a pivot.\\n3. **Partitioning**: We partition the array into three lists:\\n - `less_than_pivot`: Elements less than the pivot.\\n - `equal_to_pivot`: Elements equal to the pivot.\\n - `greater_than_pivot`: Elements greater than the pivot.\\n4. **Recursive Sorting**: We recursively sort the `less_than_pivot` and `greater_than_pivot` lists and concatenate them with the `equal_to_pivot` list to get the final sorted array.\\n\\nThis implementation is straightforward and easy to understand, but it may not be the most efficient in terms of space complexity due to the use of additional lists. For an in-place version of Quick Sort, you can modify the algorithm to sort the array within its own memory space.'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input_text = \"Write a quick sort algorithm in python\"\n", + "\n", + "completion = llm.invoke(input_text)\n", + "completion" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d5431620", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Certainly! Sorting an array is a common task in programming, and Python provides several ways to do it. Below is a simple example using Python's built-in sorting functions. We'll use the `sorted()` function and the `sort()` method of a list.\n", + "\n", + "### Using `sorted()` Function\n", + "\n", + "The `sorted()` function returns a new sorted list from the elements of any iterable.\n", + "\n", + "```python\n", + "def sort_array(arr):\n", + " return sorted(arr)\n", + "\n", + "# Example usage\n", + "array = [5, 2, 9, 1, 5, 6]\n", + "sorted_array = sort_array(array)\n", + "print(\"Original array:\", array)\n", + "print(\"Sorted array:\", sorted_array)\n", + "```\n", + "\n", + "### Using `sort()` Method\n", + "\n", + "The `sort()` method sorts the list in place and returns `None`.\n", + "\n", + "```python\n", + "def sort_array_in_place(arr):\n", + " arr.sort()\n", + "\n", + "# Example usage\n", + "array = [5, 2, 9, 1, 5, 6]\n", + "sort_array_in_place(array)\n", + "print(\"Sorted array:\", array)\n", + "```\n", + "\n", + "### Custom Sorting\n", + "\n", + "If you need to sort the array based on a custom key or in descending order, you can use the `key` and `reverse` parameters.\n", + "\n", + "```python\n", + "def custom_sort_array(arr):\n", + " # Sort in descending order\n", + " return sorted(arr, reverse=True)\n", + "\n", + "# Example usage\n", + "array = [5, 2, 9, 1, 5, 6]\n", + "sorted_array_desc = custom_sort_array(array)\n", + "print(\"Sorted array in descending order:\", sorted_array_desc)\n", + "```\n", + "\n", + "### Sorting with a Custom Key\n", + "\n", + "Suppose you have a list of tuples and you want to sort them based on the second element of each tuple:\n", + "\n", + "```python\n", + "def sort_tuples_by_second_element(arr):\n", + " return sorted(arr, key=lambda x: x[1])\n", + "\n", + "# Example usage\n", + "tuples = [(1, 3), (4, 1), (5, 2), (2, 4)]\n", + "sorted_tuples = sort_tuples_by_second_element(tuples)\n", + "print(\"Sorted tuples by second element:\", sorted_tuples)\n", + "```\n", + "\n", + "These examples demonstrate how to sort arrays in Python using different methods and options. Choose the one that best fits your needs!" + ] + } + ], + "source": [ + "for chunk in llm.stream(\"write a python program to sort an array\"):\n", + " print(chunk, end=\"\", flush=True)" + ] + }, + { + "cell_type": "markdown", + "id": "add38532", + "metadata": {}, + "source": [ + "## Chaining\n", + "\n", + "We can [chain](/docs/how_to/sequence/) our completion model with a prompt template like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "078e9db2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'In Chinese, you can say \"我喜欢编程\" (Wǒ xǐ huān biān chéng) to express \"I love programming.\" Here\\'s a breakdown of the sentence:\\n\\n- 我 (Wǒ) means \"I\"\\n- 喜欢 (xǐ huān) means \"love\" or \"like\"\\n- 编程 (biān chéng) means \"programming\"\\n\\nSo, when you put it all together, it translates to \"I love programming.\"'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.prompts import PromptTemplate\n", + "\n", + "prompt = PromptTemplate(template=\"How to say {input} in {output_language}:\\n\")\n", + "\n", + "chain = prompt | llm\n", + "chain.invoke(\n", + " {\n", + " \"output_language\": \"Chinese\",\n", + " \"input\": \"I love programming.\",\n", + " }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "e9bdfcef", + "metadata": {}, + "source": [ + "## API reference\n", + "\n", + "Refer to https://modelscope.cn/docs/model-service/API-Inference/intro for more details." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.11.1 64-bit", + "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.16" + }, + "vscode": { + "interpreter": { + "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1" + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/docs/integrations/providers/modelscope.mdx b/docs/docs/integrations/providers/modelscope.mdx index 34c421ea707e8..30c50e33bd58a 100644 --- a/docs/docs/integrations/providers/modelscope.mdx +++ b/docs/docs/integrations/providers/modelscope.mdx @@ -5,20 +5,46 @@ This page covers how to use the modelscope ecosystem within LangChain. It is broken into two parts: installation and setup, and then references to specific modelscope wrappers. -## Installation and Setup +## Installation -Install the `modelscope` package. - ```bash -pip install modelscope +pip install -U langchain-modelscope-integration ``` +Head to [ModelScope](https://modelscope.cn/) to sign up to ModelScope and generate an [SDK token](https://modelscope.cn/my/myaccesstoken). Once you've done this set the `MODELSCOPE_SDK_TOKEN` environment variable: -## Text Embedding Models +```bash +export MODELSCOPE_SDK_TOKEN= +``` + +## Chat Models + +`ModelScopeChatEndpoint` class exposes chat models from ModelScope. See available models [here](https://www.modelscope.cn/docs/model-service/API-Inference/intro). + +```python +from langchain_modelscope import ModelScopeChatEndpoint + +llm = ModelScopeChatEndpoint(model="Qwen/Qwen2.5-Coder-32B-Instruct") +llm.invoke("Sing a ballad of LangChain.") +``` + +## Embeddings +`ModelScopeEmbeddings` class exposes embeddings from ModelScope. ```python -from langchain_community.embeddings import ModelScopeEmbeddings +from langchain_modelscope import ModelScopeEmbeddings + +embeddings = ModelScopeEmbeddings(model_id="damo/nlp_corom_sentence-embedding_english-base") +embeddings.embed_query("What is the meaning of life?") ``` -For a more detailed walkthrough of this, see [this notebook](/docs/integrations/text_embedding/modelscope_hub) +## LLMs +`ModelScopeLLM` class exposes LLMs from ModelScope. + +```python +from langchain_modelscope import ModelScopeLLM + +llm = ModelScopeLLM(model="Qwen/Qwen2.5-Coder-32B-Instruct") +llm.invoke("The meaning of life is") +``` diff --git a/docs/docs/integrations/text_embedding/modelscope_embedding.ipynb b/docs/docs/integrations/text_embedding/modelscope_embedding.ipynb new file mode 100644 index 0000000000000..b5db8dbab9a19 --- /dev/null +++ b/docs/docs/integrations/text_embedding/modelscope_embedding.ipynb @@ -0,0 +1,285 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: ModelScope\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "9a3d6f34", + "metadata": {}, + "source": [ + "# ModelScopeEmbeddings\n", + "\n", + "ModelScope ([Home](https://www.modelscope.cn/) | [GitHub](https://github.com/modelscope/modelscope)) is built upon the notion of “Model-as-a-Service” (MaaS). It seeks to bring together most advanced machine learning models from the AI community, and streamlines the process of leveraging AI models in real-world applications. The core ModelScope library open-sourced in this repository provides the interfaces and implementations that allow developers to perform model inference, training and evaluation. \n", + "\n", + "This will help you get started with ModelScope embedding models using LangChain.\n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "| Provider | Package |\n", + "|:--------:|:-------:|\n", + "| [ModelScope](/docs/integrations/providers/modelscope/) | [langchain-modelscope-integration](https://pypi.org/project/langchain-modelscope-integration/) |\n", + "\n", + "## Setup\n", + "\n", + "To access ModelScope embedding models you'll need to create a/an ModelScope account, get an API key, and install the `langchain-modelscope-integration` integration package.\n", + "\n", + "### Credentials\n", + "\n", + "Head to [ModelScope](https://modelscope.cn/) to sign up to ModelScope." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36521c2a", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "if not os.getenv(\"MODELSCOPE_SDK_TOKEN\"):\n", + " os.environ[\"MODELSCOPE_SDK_TOKEN\"] = getpass.getpass(\n", + " \"Enter your ModelScope SDK token: \"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "d9664366", + "metadata": {}, + "source": [ + "### Installation\n", + "\n", + "The LangChain ModelScope integration lives in the `langchain-modelscope-integration` package:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64853226", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -qU langchain-modelscope-integration" + ] + }, + { + "cell_type": "markdown", + "id": "45dd1724", + "metadata": {}, + "source": [ + "## Instantiation\n", + "\n", + "Now we can instantiate our model object:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9ea7a09b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Downloading Model to directory: /root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-12-27 16:15:11,175 - modelscope - WARNING - Model revision not specified, use revision: v1.0.0\n", + "2024-12-27 16:15:11,443 - modelscope - INFO - initiate model from /root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base\n", + "2024-12-27 16:15:11,444 - modelscope - INFO - initiate model from location /root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base.\n", + "2024-12-27 16:15:11,445 - modelscope - INFO - initialize model from /root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base\n", + "2024-12-27 16:15:12,115 - modelscope - WARNING - No preprocessor field found in cfg.\n", + "2024-12-27 16:15:12,116 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n", + "2024-12-27 16:15:12,116 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base'}. trying to build by task and model information.\n", + "2024-12-27 16:15:12,318 - modelscope - WARNING - No preprocessor field found in cfg.\n", + "2024-12-27 16:15:12,319 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n", + "2024-12-27 16:15:12,319 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/root/.cache/modelscope/hub/damo/nlp_corom_sentence-embedding_english-base', 'sequence_length': 128}. trying to build by task and model information.\n" + ] + } + ], + "source": [ + "from langchain_modelscope import ModelScopeEmbeddings\n", + "\n", + "embeddings = ModelScopeEmbeddings(\n", + " model_id=\"damo/nlp_corom_sentence-embedding_english-base\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "77d271b6", + "metadata": {}, + "source": [ + "## 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": 4, + "id": "d817716b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/root/miniconda3/envs/langchain/lib/python3.10/site-packages/transformers/modeling_utils.py:1113: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.\n", + " warnings.warn(\n", + "/root/miniconda3/envs/langchain/lib/python3.10/site-packages/transformers/modeling_utils.py:1113: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.\n", + " warnings.warn(\n" + ] + }, + { + "data": { + "text/plain": [ + "'LangChain is the framework for building context-aware reasoning applications'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 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", + "vectorstore = InMemoryVectorStore.from_texts(\n", + " [text],\n", + " embedding=embeddings,\n", + ")\n", + "\n", + "# Use the vectorstore as a retriever\n", + "retriever = vectorstore.as_retriever()\n", + "\n", + "# 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": [ + "## 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": 5, + "id": "0d2befcd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-0.6046376824378967, -0.3595953583717346, 0.11333226412534714, -0.030444221571087837, 0.23397332429\n" + ] + } + ], + "source": [ + "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": [ + "### Embed multiple texts\n", + "\n", + "You can embed multiple texts with `embed_documents`:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "2f4d6e97", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-0.6046381592750549, -0.3595949709415436, 0.11333223432302475, -0.030444379895925522, 0.23397321999\n", + "[-0.36103254556655884, -0.7602502107620239, 0.6505364775657654, 0.000658963865134865, 1.185304522514\n" + ] + } + ], + "source": [ + "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 `ModelScopeEmbeddings` features and configuration options, please refer to the [API reference](https://www.modelscope.cn/docs/sdk/pipelines).\n" + ] + } + ], + "metadata": { + "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.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/docs/integrations/text_embedding/modelscope_hub.ipynb b/docs/docs/integrations/text_embedding/modelscope_hub.ipynb deleted file mode 100644 index b7d404e7beb4b..0000000000000 --- a/docs/docs/integrations/text_embedding/modelscope_hub.ipynb +++ /dev/null @@ -1,90 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# ModelScope\n", - "\n", - ">[ModelScope](https://www.modelscope.cn/home) is big repository of the models and datasets.\n", - "\n", - "Let's load the ModelScope Embedding class." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_community.embeddings import ModelScopeEmbeddings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "model_id = \"damo/nlp_corom_sentence-embedding_english-base\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "embeddings = ModelScopeEmbeddings(model_id=model_id)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "text = \"This is a test document.\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "query_result = embeddings.embed_query(text)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "doc_results = embeddings.embed_documents([\"foo\"])" - ] - } - ], - "metadata": { - "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.12" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/libs/packages.yml b/libs/packages.yml index 810e54c5a857b..8d9ebb8678564 100644 --- a/libs/packages.yml +++ b/libs/packages.yml @@ -308,3 +308,7 @@ packages: repo: crate/langchain-cratedb downloads: 362 downloads_updated_at: '2024-12-23T20:53:27.001852+00:00' +- name: langchain-modelscope + path: . + repo: modelscope/langchain-modelscope + downloads: 0 \ No newline at end of file