Skip to main content
Open In ColabOpen on GitHub

ModelScopeEndpoint

ModelScope(首页 | GitHub)基于“模型即服务”(MaaS)的理念构建。它旨在汇集AI社区中大多数最先进的机器学习模型,并简化在实际应用中利用AI模型的过程。此仓库中开源的核心ModelScope库提供了允许开发者执行模型推理、训练和评估的接口和实现。这将帮助您使用LangChain开始使用ModelScope完成模型(LLMs)。

概览

集成详情

提供者本地可序列化的软件包下载最新包裹
ModelScopeModelScopeEndpointlangchain-modelscope-integrationPyPI - DownloadsPyPI - Version

设置

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

凭据

前往 ModelScope 注册账号并生成 SDK令牌。完成后,请设置 MODELSCOPE_SDK_TOKEN 环境变量:

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 包中:

%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.'
for chunk in llm.stream("write a python program to sort an array"):
print(chunk, 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!

链式调用

我们可以像这样将完成模型与提示模板链接起来:

from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate(template="How to say {input} in {output_language}:\n")

chain = prompt | llm
chain.invoke(
{
"output_language": "Chinese",
"input": "I love programming.",
}
)
API 参考:PromptTemplate
'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."'

API 参考

有关更多详细信息,请参阅 https://modelscope.cn/docs/model-service/API-Inference/intro