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()
相关推荐
WarghostWu11 天前
模型情景制作-冰镇啤酒
模型·场景·啤酒·制作
亚图跨际24 天前
Python政府短期或长期债务李嘉图等价模型状态矩阵
python·矩阵·消费·模型·预算·税收·债务
Android技术栈1 个月前
鸿蒙开发:【线程模型】
移动开发·线程·harmonyos·鸿蒙系统·openharmony·模型·鸿蒙开发
heeheeai1 个月前
使用TensorFlow和Keras对以ResNet50模型进行微调
人工智能·微调·tensorflow·keras·模型
GpuMall2 个月前
GPT-4o:重塑人机交互与未来智能的新里程碑
人工智能·python·ai作画·stable diffusion·sd·模型·tornado
亚图跨际2 个月前
R和Python市场篮分析算法及行为分析模型
python·算法·r语言·模型·市场·购物·行为分析
老子云平台2 个月前
Blender游戏资产优化技巧
游戏·3d·blender·数字孪生·模型·模型轻量化·模型引擎
老子云平台3 个月前
Unity AR开发环境搭建
3d·unity·游戏引擎·ar·数字孪生·模型·数字孪生引擎
Muzzik3 个月前
【muzzik 分享】3D模型平面切割
3d·cocos creator·cocos2d·模型·cocos·mesh·切割
老子云平台3 个月前
12个建筑数据分析典型用例
3d·数字孪生·模型·bim·三维引擎·amrt·amrt3d