DashScope - 阿里模型服务灵积


文章目录

    • [关于 DashScope](#关于 DashScope)
      • [关于 DashScope 和 ModelScope](#关于 DashScope 和 ModelScope)
    • 快速上手
    • 代码调用
      • [http 请求示例](#http 请求示例)
      • [Python 调用](#Python 调用)

关于 DashScope

DashScope灵积模型服务建立在"模型即服务"(Model-as-a-Service,MaaS)的理念基础之上,围绕AI各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。

通过围绕模型为中心,DashScope灵积模型服务致力于为AI应用开发者提供品类丰富、数量众多的模型选择,并通过API接口为其提供开箱即用、能力卓越、成本经济的模型服务。



关于 DashScope 和 ModelScope

参考文章:https://developer.aliyun.com/article/1377012

ModelScope是一个开源技术社区,从其立场来看,它并不承担营收的使命。DashScope可以看作是ModelScope的"孪生兄弟",它们有着相同的底层架构。

两者的区别在于,ModelScope上的许多开发者是基于模型的checkpoint进行Fine-tune,而DashScope更多地为模型提供商(如百川智能、智谱AI、Stability.AI等)提供服务,通过API的方式向下游厂商提供Fine-tune和Influence链路服务。

ModelScope和DashScope是模型的一体两面,都是MaaS(Model as a Service)的一部分。

相对较小的小模型走开源路线,相对较大的大模型则走商业路线。

例如,智谱AI的ChatGLM-6B模型就在ModelScope上进行了开源,并且已经形成了一定的用户规模和影响力。

未来,它的13B、50B、130B模型将通过DashScope进行商业化落地。

无独有偶,阿里云的通义千问也是同样的情况,Qwen-7B模型是开源的,而Qwen-50B模型未来可能会通过DashScope去做API模式的商业化。


快速上手

参考官方文档:
https://help.aliyun.com/zh/dashscope/create-a-chat-foundation-model

1、开通DashScope并创建API-KEY

https://dashscope.console.aliyun.com/overview


2、安装DashScope SDK

基于 Python 3.8+

shell 复制代码
pip install dashscope

3、API-KEY设置

1)通过环境变量设置

推荐通过环境变量设置api-key

bash 复制代码
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

2)通过代码设置

通常不推荐将api-key直接写在代码中

python 复制代码
import dashscope
dashscope.api_key="YOUR_DASHSCOPE_API_KEY"

3)通过文件设置**

默认会读取~/.dashscope/api_key 作为 DASHSCOPE_API_KEY, 您可以通过环境变量设置api_key保存文件位置。

bash 复制代码
export DASHSCOPE_API_KEY_FILE_PATH=YOUR_API_KEY_FILE_PATH

代码调用

http 请求示例

shell 复制代码
curl -X GET 'https://dashscope.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219' \
--header 'Authorization: Bearer <YOUR_API_KEY>'

响应示例:

json 复制代码
{
    "request_id": "45ac7f13-xxxx-xxxx-xxxx-e03c35068d83",
    "output": {
        "task_id": "73205176-xxxx-xxxx-xxxx-16bd5d902219",
        "task_status": "SUCCEEDED",
        "submit_time": "2023-12-20 21:36:31.896",
        "scheduled_time": "2023-12-20 21:36:39.009",
        "end_time": "2023-12-20 21:36:45.913",
        "results": [
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx1.png"
            },
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx2.png"
            },
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx3.png"
            },
            {
                "code": "DataInspectionFailed",
                "message": "Output data may contain inappropriate content.",
            }
        ],
        "task_metrics": {
            "TOTAL": 4,
            "SUCCEEDED": 3,
            "FAILED": 1
        }
    },
    "usage": {
        "image_count": 3
    }
}

Python 调用

python 复制代码
from http import HTTPStatus
import dashscope
from dashscope import Generation

dashscope.api_key = 'YOUR-DASHSCOPE-API-KEY'

responses = Generation.call(model=Generation.Models.qwen_turbo,
                            prompt='今天天气好吗?')

# 方式二
messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': '如何做西红柿鸡蛋?'}]
            
response = Generation.call(
    model='qwen-turbo',
    messages=messages,
    result_format='message',  # set the result to be "message" format.
)

if responses.status_code == HTTPStatus.OK:
    print('Result is: %s' % responses.output)
else:
    print('Failed request_id: %s, status_code: %s, code: %s, message:%s' %
            (responses.request_id, responses.status_code, responses.code,
            responses.message))

python 复制代码
def tokenizer():
    response = dashscope.Tokenization.call(model='qwen-turbo',
                                 messages=[{'role': 'user', 'content': '你好?'}],
                                 )
    if response.status_code == HTTPStatus.OK:
        print('Result is: %s' % response)
    else:
        print('Failed request_id: %s, status_code: %s, code: %s, message:%s' %
              (response.request_id, response.status_code, response.code,
               response.message))


if __name__ == '__main__':
    tokenizer()
相关推荐
小北的AI科技分享2 天前
2026年GEO排名优化公司哪家强?五大服务商深度盘点
科技·模型·青山
艺杯羹3 天前
全网首发!Claude Code 国内用法保姆级教程:API配置+VS Code插件,15分钟轻松上手
vscode·ai·ai编程·模型·claude code
魔士于安6 天前
Unity完整小球迷宫项目
前端·unity·游戏引擎·贴图·模型
魔士于安7 天前
Unity 超市总动员 超市收银台 超市货架 超市购物手推车 超市常见商品
游戏·unity·游戏引擎·贴图·模型
七夜zippoe7 天前
OpenClaw 多模型配置与切换详解
人工智能·配置·模型·切换·openclaw
魔士于安8 天前
Unity windows 同步 异步 打开文件文件夹工具
游戏·unity·游戏引擎·贴图·模型
魔士于安8 天前
unity lowpoly 风格 城市 建筑 道路 交通标志
游戏·unity·游戏引擎·贴图·模型
魔士于安9 天前
unity 卡通风整套资源 小鸟N套带动作+一套卡通风村落 和 相关道具+落叶粒子效果 buildin
游戏·unity·游戏引擎·贴图·模型
张老师带你学10 天前
Unity 食物 农产品相关
科技·游戏·unity·游戏引擎·模型
魔士于安10 天前
Unity类似博物馆场景
前端·unity·游戏引擎·贴图·模型