ModelScopeEndpoint
ModelScope(首頁 | GitHub)建立在「模型即服務」(Model-as-a-Service, MaaS) 的概念之上。它旨在匯集 AI 社群中最先進的機器學習模型,並簡化在真實世界應用中利用 AI 模型的流程。此儲存庫中開源的核心 ModelScope 庫提供了介面和實作,讓開發人員能夠執行模型推論、訓練和評估。這將幫助您開始使用 LangChain 的 ModelScope 完成模型 (LLM)。
概觀
整合詳細資訊
供應商 | 類別 | 套件 | 本地 | 可序列化 | 套件下載 | 套件最新版 |
---|---|---|---|---|---|---|
ModelScope | ModelScopeEndpoint | langchain-modelscope-integration | ❌ | ❌ |
設定
要存取 ModelScope 模型,您需要建立 ModelScope 帳戶、取得 SDK 令牌,並安裝 langchain-modelscope-integration
整合套件。
憑證
前往 ModelScope 註冊 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。