文章目录
- 关于 DashScope
- 快速上手
- 代码调用
- http 请求示例
- Python 调用
关于 DashScope
- 官方主页:https://dashscope.aliyun.com
- PYPI : https://pypi.org/project/dashscope/
- 支持模型:https://dashscope.console.aliyun.com/model
DashScope灵积模型服务建立在“模型即服务”(Model-as-a-Service,MaaS)的理念基础之上,围绕AI各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。
通过围绕模型为中心,DashScope灵积模型服务致力于为AI应用开发者提供品类丰富、数量众多的模型选择,并通过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+
pip install dashscope
3、API-KEY设置
1)通过环境变量设置
推荐通过环境变量设置api-key
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
2)通过代码设置
通常不推荐将api-key直接写在代码中
import dashscope
dashscope.api_key="YOUR_DASHSCOPE_API_KEY"
3)通过文件设置**
默认会读取~/.dashscope/api_key
作为 DASHSCOPE_API_KEY
, 您可以通过环境变量设置api_key保存文件位置。
export DASHSCOPE_API_KEY_FILE_PATH=YOUR_API_KEY_FILE_PATH
代码调用
http 请求示例
curl -X GET 'https://dashscope.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219' \
--header 'Authorization: Bearer <YOUR_API_KEY>'
响应示例:
{
"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 调用
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))
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()