01.引言
通过API接口进行标准化,能让开源模型以更加轻量和迅速的方式被开发者使用起来,并集成到不同的AI应用中。魔搭通过API-Inference,支持广大开发者无需本地的GPU和环境设置,就能轻松的依托不同开源模型的能力,展开富有创造力的尝试,与工具结合调用,来构建多种多样的AI应用原型。
历史文章:开发者福利,魔搭推出免费模型推理API,注册就送每日2000次调用!
魔搭社区现在已经支持了近3000个模型的推理API 欢迎使用!
当前魔搭免费模型推理API-Inference已经覆盖了包括大语言模型(包括R1等推理模型),多模态模型,文生图等多个领域:
模型类型 |
典型模型 |
大语言模型(Reasoning) |
deepseek-ai/DeepSeek-R1, Qwen/QwQ-32B-Preview |
多模态理解模型 |
Qwen/Qwen2.5-VL-72B-Instruct |
文生图(基础模型) |
MAILAND/majicflus_v1 |
文生图(lora) |
ChaosMY/MYkawaii4MJ |
02.最佳实践
对于支持API-Inference的模型,在页面右侧就可以直接看到范例API调用代码:
以各个模态的典型模型为例:
- 大语言模型(以DeepSeek-R1 Reasoning模型为例)
from openai import OpenAI client = OpenAI( base_url='https://api-inference.modelscope.cn/v1/', api_key='Your_SDK_Token', # ModelScope Token ) response = client.chat.completions.create( model='deepseek-ai/DeepSeek-R1', # ModelScope Model-Id messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'user', 'content': '你好' } ], stream=True ) reasoning_content = '' answer_content = '' done_reasoning = False for chunk in response: # for reaonsing model, output may include both reasoning_content and content reasoning_chunk = chunk.choices[0].delta.reasoning_content answer_chunk = chunk.choices[0].delta.content if reasoning_chunk != '': print(reasoning_chunk, end='',flush=True) elif answer_chunk != '': if not done_reasoning: print("\n\n === Final Answer ===\n") done_reasoning = True print(answer_chunk, end='',flush=True) 好的,用户用中文打招呼“你好”,我需要回应。首先,确定用户的需求是什么。可能只是简单的问候,或者有后续问题。考虑到用户之前可能切换了语言,现在用中文,可能需要中文回答。我应该保持友好,询问有什么可以帮助的,同时保持简洁。避免使用复杂的句子,让用户感到轻松。另外,检查是否有拼写错误,确保回应自然。最后,确保符合OpenAI的内容政策,不涉及敏感话题。准备好回应后,发送即可。 === Final Answer === 你好!很高兴见到你,有什么我可以帮忙的吗?
- 多模态理解模型
from openai import OpenAI client = OpenAI( base_url='https://api-inference.modelscope.cn/v1/', api_key='Your_SDK_Token', # ModelScope Token ) response = client.chat.completions.create( model='Qwen/Qwen2.5-VL-72B-Instruct', # ModelScope Model-Id messages=[{ 'role': 'user', 'content': [{ 'type': 'text', 'text': '描述这幅图', }, { 'type': 'image_url', 'image_url': { 'url': 'https://modelscope.oss-cn-beijing.aliyuncs.com/demo/images/audrey_hepburn.jpg', }, }], }], stream=True ) for chunk in response: print(chunk.choices[0].delta.content, end='', flush=True) 这是一张黑白照片,展示了一位女性在厨房里蹲着打开烤箱门的场景。她穿着一件带有花纹的吊带连衣裙,头发梳成一个整齐的发型。她的表情专注,似乎在检查烤箱内部的东西。厨房的橱柜和烤箱都是白色的,背景中可以看到一些厨房用具和装饰品。整体氛围显得非常生活化和自然。
文生图模型(基础模型)
import requests import json from PIL import Image from io import BytesIO url = 'https://api-inference.modelscope.cn/v1/images/generations' payload = { 'model': 'MAILAND/majicflus_v1',#ModelScope Model-Id,required 'prompt': 'a cute girl in festive chinese new year clothing'# required } headers = { 'Authorization': 'Bearer Your_SDK_Token', 'Content-Type': 'application/json' } response = requests.post(url, data=json.dumps(payload, ensure_ascii=False).encode('utf-8'), headers=headers) response_data = response.json() image = Image.open(BytesIO(requests.get(response_data['images'][0]['url']).content)) image.save('result_image.jpg')
- 文生图模型(lora)
import requests import json from PIL import Image from io import BytesIO url = 'https://api-inference.modelscope.cn/v1/images/generations' payload = { 'model': 'ChaosMY/MYkawaii4MJ',#ModelScope Model-Id,required 'prompt': 'a cute black cat and a beautiful girl with long black hair and glasses'# required } headers = { 'Authorization': 'Bearer Your_SDK_Token', 'Content-Type': 'application/json' } response = requests.post(url, data=json.dumps(payload, ensure_ascii=False).encode('utf-8'), headers=headers) response_data = response.json() image = Image.open(BytesIO(requests.get(response_data['images'][0]['url']).content)) image.save('result_image.jpg')
各位开发者小伙伴有希望尽快支持的开源模型,欢迎在留言区积极留言模型id,点赞排名靠前的开源模型,我们将会优先支持。
点击链接,即可跳转API-Inference页面~