以编程方式使用文档

ModelScope (首页 | GitHub) 构建于“模型即服务”(MaaS)的理念之上。它致力于汇聚人工智能社区中最先进的机器学习模型,并简化在实际应用中调用 AI 模型的过程。本仓库开源的核心 ModelScope 库提供了接口和实现,使开发者能够执行模型推理、训练和评估。这将帮助您开始使用 LangChain 调用 ModelScope 的补全模型(LLM)。

概述

集成详情

ProviderClassPackageLocalSerializableDownloadsVersion
ModelScopeModelScopeEndpointlangchain-modelscope-integration!PyPI - 下载量!PyPI - 版本

设置

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

凭据

前往 ModelScope 注册 ModelScope 账户并生成 SDK token。完成此操作后,设置 MODELSCOPE_SDK_TOKEN 环境变量:

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

安装

LangChain ModelScope 集成位于 langchain-modelscope-integration package:

pip install -qU langchain-modelscope-integration

实例化

现在我们可以实例化模型对象并生成聊天补全:

from langchain_modelscope import ModelScopeEndpoint

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

调用

input_text = "Write a quick sort algorithm in python"

completion = llm.invoke(input_text)
completion
'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.'
stream = llm.stream_events("write a python program to sort an array", version="v3")
for token in stream.text:
    print(token, end="", flush=True)
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 了解更多详情。