> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ModelScopeEndpoint 集成

> 使用 LangChain Python 集成 ModelScopeEndpoint LLM。

ModelScope ([Home](https://www.modelscope.cn/) | [GitHub](https://github.com/modelscope/modelscope)) 是基于“模型即服务”（MaaS）理念构建的。它旨在汇集 AI 社区中最先进的机器学习模型，并简化在现实世界应用中利用 AI 模型的流程。本仓库开源的核心 ModelScope 库提供了允许开发者执行模型推理、训练和评估的接口和实现。这将帮助您使用 LangChain 开始使用 ModelScope 完成模型（LLMs）。

## 概述

### 集成细节

| 提供者 | 类 | 包 | 本地 | 可序列化 | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: | :-: |
| [`ModelScope`](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/providers/modelscope/) | `ModelScopeEndpoint` | [`langchain-modelscope-integration`](https://pypi.org/project/langchain-modelscope-integration/) | ❌ | ❌ | ![PyPI - 下载](https://img.shields.io/pypi/dm/langchain-modelscope-integration?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-modelscope-integration?style=flat-square\&label=%20) |

## 设置

要访问 ModelScope 模型，您需要创建一个 ModelScope 账户，获取一个 SDK 令牌，并安装 `langchain-modelscope-integration` 集成包。

### 凭证

前往 [ModelScope](https://modelscope.cn/) 注册 ModelScope 并生成 [SDK 令牌](https://modelscope.cn/my/myaccesstoken)。完成这些步骤后，设置 `MODELSCOPE_SDK_TOKEN` 环境变量：

```python theme={null}
import getpass
import os

if not os.getenv("MODELSCOPE_SDK_TOKEN"):
    os.environ["MODELSCOPE_SDK_TOKEN"] = getpass.getpass(
        "Enter your ModelScope SDK token: "
    )
```

### 安装

LangChain ModelScope 集成位于 `langchain-modelscope-integration` 包中：

```python theme={null}
pip install -qU langchain-modelscope-integration
```

## 实例化

现在我们可以实例化我们的模型对象并生成对话完成：

```python theme={null}
from langchain_modelscope import ModelScopeEndpoint

llm = ModelScopeEndpoint(
    model="Qwen/Qwen2.5-Coder-32B-Instruct",
    temperature=0,
    max_tokens=1024,
    timeout=60,
)
```

## 调用

```python theme={null}
input_text = "Write a quick sort algorithm in python"

completion = llm.invoke(input_text)
completion
```

```text theme={null}
'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.'
```

```python theme={null}
stream = llm.stream_events("write a python program to sort an array", version="v3")
for token in stream.text:
    print(token, end="", flush=True)
```

```text theme={null}
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.

### Using `sorted()` Function

The `sorted()` function returns a new sorted list from the elements of any iterable.

\`\`\`python
def sort_array(arr):
    return sorted(arr)

# Example usage
array = [5, 2, 9, 1, 5, 6]
sorted_array = sort_array(array)
print("Original array:", array)
print("Sorted array:", sorted_array)
\`\`\`

### Using `sort()` Method

The `sort()` method sorts the list in place and returns `None`.

\`\`\`python
def sort_array_in_place(arr):
    arr.sort()

# Example usage
array = [5, 2, 9, 1, 5, 6]
sort_array_in_place(array)
print("Sorted array:", array)
\`\`\`

### Custom Sorting

If you need to sort the array based on a custom key or in descending order, you can use the `key` and `reverse` parameters.

\`\`\`python
def custom_sort_array(arr):
    # Sort in descending order
    return sorted(arr, reverse=True)

# Example usage
array = [5, 2, 9, 1, 5, 6]
sorted_array_desc = custom_sort_array(array)
print("Sorted array in descending order:", sorted_array_desc)
\`\`\`

### Sorting with a Custom Key

Suppose you have a list of tuples and you want to sort them based on the second element of each tuple:

\`\`\`python
def sort_tuples_by_second_element(arr):
    return sorted(arr, key=lambda x: x[1])

# Example usage
tuples = [(1, 3), (4, 1), (5, 2), (2, 4)]
sorted_tuples = sort_tuples_by_second_element(tuples)
print("Sorted tuples by second element:", sorted_tuples)
\`\`\`

These examples demonstrate how to sort arrays in Python using different methods and options. Choose the one that best fits your needs!
```

***

## API 参考

更多详情请参考 [modelscope.cn/docs/model-service/API-Inference/intro](https://modelscope.cn/docs/model-service/API-Inference/intro)。

***

> 官方原文：[src/oss/python/integrations/llms/modelscope\_endpoint.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/llms/modelscope_endpoint.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.