ModelScope Chat 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"] = "YOUR_SDK_TOKEN"
Available models: https://modelscope.cn/docs/model-service/API-Inference/intro
from langchain_community.chat_models import ModelScopeChatEndpoint
llm = ModelScopeChatEndpoint(model="Qwen/Qwen2.5-Coder-32B-Instruct")
llm.invoke("write a python program to sort an array")
API Reference:ModelScopeChatEndpoint
AIMessage(content='Certainly! Sorting an array is a common task in programming, and Python provides several ways to do it. Below is a simple example using the built-in `sorted()` function and the `sort()` method for lists. I\'ll also include a basic implementation of the Bubble Sort algorithm for educational purposes.\n\n### Using the Built-in `sorted()` Function\n\nThe `sorted()` function returns a new sorted list from the elements of any iterable.\n\n\`\`\`python\ndef sort_array(arr):\n return sorted(arr)\n\n# Example usage\narray = [5, 2, 9, 1, 5, 6]\nsorted_array = sort_array(array)\nprint("Sorted array:", sorted_array)\n\`\`\`\n\n### Using the List\'s `sort()` Method\n\nThe `sort()` method sorts the list in place and returns `None`.\n\n\`\`\`python\ndef sort_array_in_place(arr):\n arr.sort()\n\n# Example usage\narray = [5, 2, 9, 1, 5, 6]\nsort_array_in_place(array)\nprint("Sorted array:", array)\n\`\`\`\n\n### Implementing Bubble Sort\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\ndef bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n # Track if any swap was made\n swapped = False\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n # Swap if the element found is greater than the next element\n arr[j], arr[j+1] = arr[j+1], arr[j]\n swapped = True\n # If no two elements were swapped by inner loop, then break\n if not swapped:\n break\n\n# Example usage\narray = [5, 2, 9, 1, 5, 6]\nbubble_sort(array)\nprint("Sorted array:", array)\n\`\`\`\n\nThese examples demonstrate different ways to sort an array in Python. The built-in methods are highly optimized and should be preferred for most use cases. The Bubble Sort implementation is more educational and shows how a basic sorting algorithm works.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 466, 'prompt_tokens': 26, 'total_tokens': 492, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'Qwen/Qwen2.5-Coder-32B-Instruct', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-f05a1ba2-7be9-480b-b7aa-7eb3aeaf3c23-0')
for chunk in llm.stream("write a python program to sort an array"):
print(chunk)
Related
- Chat model conceptual guide
- Chat model how-to guides