Skip to main content
Open In ColabOpen on GitHub

ModelScope Endpoint

Overview

ModelScope (Home | GitHub) 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 example goes over how to use LangChain to interact with ModelScope Chat Endpoint.

Setup

Generate your sdk token from: https://modelscope.cn/my/myaccesstoken

import os

os.environ["MODELSCOPE_SDK_TOKEN"] = "YOUR_SDK_TOKEN"

Available models: https://modelscope.cn/docs/model-service/API-Inference/intro

Instantiation

pip install modelscope

Invocation

from langchain_community.llms import ModelScopeEndpoint

llm = ModelScopeEndpoint(model="Qwen/Qwen2.5-Coder-32B-Instruct")

llm.invoke("write a python program to sort an array")
API Reference:ModelScopeEndpoint
'Certainly! Sorting an array can be done in Python using various methods. One of the simplest ways is to use Python\'s built-in sorting functions. However, if you want to implement a sorting algorithm yourself, I\'ll show you how to do that using the Bubble Sort algorithm as an example.\n\nHere\'s a Python program that sorts an array using both the built-in method and the Bubble Sort algorithm:\n\n### Using Built-in Method\n\nPython provides a built-in function `sorted()` and a method `sort()` for lists which can be used to sort arrays easily.\n\n\`\`\`python\n# Using built-in sorted() function\ndef sort_array_builtin(arr):\n    return sorted(arr)\n\n# Using list\'s sort() method\ndef sort_array_in_place(arr):\n    arr.sort()\n\n# Example usage\narray = [64, 34, 25, 12, 22, 11, 90]\nprint("Original array:", array)\n\nsorted_array = sort_array_builtin(array)\nprint("Sorted array using built-in sorted():", sorted_array)\n\nsort_array_in_place(array)\nprint("Sorted array using list\'s sort():", array)\n\`\`\`\n\n### Using Bubble Sort Algorithm\n\nBubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.\n\n\`\`\`python\n# Bubble Sort implementation\ndef bubble_sort(arr):\n    n = len(arr)\n    # Traverse through all array elements\n    for i in range(n):\n        # Last i elements are already in place\n        for j in range(0, n-i-1):\n            # Traverse the array from 0 to n-i-1\n            # Swap if the element found is greater than the next element\n            if arr[j] > arr[j+1]:\n                arr[j], arr[j+1] = arr[j+1], arr[j]\n\n# Example usage\narray = [64, 34, 25, 12, 22, 11, 90]\nprint("Original array:", array)\n\nbubble_sort(array)\nprint("Sorted array using Bubble Sort:", array)\n\`\`\`\n\n### Explanation\n\n- **Built-in Method**: The `sorted()` function returns a new sorted list from the elements of any iterable. The `sort()` method sorts the list in place and returns `None`.\n  \n- **Bubble Sort**: This algorithm repeatedly compares adjacent elements and swaps them if they are in the wrong order. It continues this process until no more swaps are needed, indicating that the list is sorted.\n\nBoth methods will give you a sorted array, but using the built-in methods is generally preferred due to their efficiency and simplicity.'
for chunk in llm.stream("write a python program to sort an array"):
print(chunk, end="", flush=True)
Certainly! Sorting an array can be done in Python using various methods. One of the simplest ways is to use Python's built-in sorting functions. However, if you want to implement a sorting algorithm yourself, I'll show you how to do it using the Bubble Sort algorithm as an example.

Here's a Python program that sorts an array using both the built-in method and the Bubble Sort algorithm:

### Using Built-in Method

Python provides a built-in method called `sort()` for lists, or you can use the `sorted()` function which returns a new sorted list.

\`\`\`python
# Using built-in sort() method
def sort_array_builtin(arr):
arr.sort()
return arr

# Using built-in sorted() function
def sort_array_sorted(arr):
return sorted(arr)

# Example usage
array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", array)

sorted_array_builtin = sort_array_builtin(array.copy())
print("Sorted array using sort():", sorted_array_builtin)

sorted_array_sorted = sort_array_sorted(array.copy())
print("Sorted array using sorted():", sorted_array_sorted)
\`\`\`

### Using Bubble Sort Algorithm

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

\`\`\`python
# Using Bubble Sort algorithm
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Track if any swapping happens
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
# Swap if the element found is greater than the next element
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no two elements were swapped by inner loop, then break
if not swapped:
break
return arr

# Example usage
array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", array)

sorted_array_bubble = bubble_sort(array.copy())
print("Sorted array using Bubble Sort:", sorted_array_bubble)
\`\`\`

### Explanation

- **Built-in Methods**: These are highly optimized and should be preferred for most use cases.
- **Bubble Sort**: This is a simple algorithm but not very efficient for large datasets (O(n^2) time complexity). It's useful for educational purposes to understand basic sorting concepts.

You can choose either method based on your needs. For practical applications, the built-in methods are recommended due to their efficiency and simplicity.

Chaining

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "Chinese",
"input": "I love programming.",
}
)
API Reference:ChatPromptTemplate
'我喜欢编程。'

API reference

Refer to https://modelscope.cn/docs/model-service/API-Inference/intro for more details.


Was this page helpful?