ModelScope Endpoint
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"] = "bc34909d-af9a-42e5-b483-ce4dd246f368"
Available models: https://modelscope.cn/docs/model-service/API-Inference/intro
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. Below are examples of how you can sort an array using both the `sort()` method and the `sorted()` function.\n\n### Using the `sort()` Method\nThe `sort()` method sorts the list in place and modifies the original list.\n\n\`\`\`python\n# Define the array\narray = [5, 2, 9, 1, 5, 6]\n\n# Sort the array in ascending order\narray.sort()\n\n# Print the sorted array\nprint("Sorted array (ascending):", array)\n\n# Sort the array in descending order\narray.sort(reverse=True)\n\n# Print the sorted array\nprint("Sorted array (descending):", array)\n\`\`\`\n\n### Using the `sorted()` Function\nThe `sorted()` function returns a new list that is sorted, leaving the original list unchanged.\n\n\`\`\`python\n# Define the array\narray = [5, 2, 9, 1, 5, 6]\n\n# Get a new sorted array in ascending order\nsorted_array_asc = sorted(array)\n\n# Print the sorted array\nprint("Sorted array (ascending):", sorted_array_asc)\n\n# Get a new sorted array in descending order\nsorted_array_desc = sorted(array, reverse=True)\n\n# Print the sorted array\nprint("Sorted array (descending):", sorted_array_desc)\n\`\`\`\n\n### Custom Sorting\nYou can also sort based on custom criteria using a key function.\n\n\`\`\`python\n# Define the array of tuples\narray_of_tuples = [(1, \'one\'), (3, \'three\'), (2, \'two\')]\n\n# Sort the array of tuples based on the second element of each tuple\nsorted_by_second_element = sorted(array_of_tuples, key=lambda x: x[1])\n\n# Print the sorted array\nprint("Sorted array by second element:", sorted_by_second_element)\n\`\`\`\n\nThese examples demonstrate how to sort arrays in Python using different methods. You can choose the one that best fits your needs.'
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.
Related
- LLM conceptual guide
- LLM how-to guides