# PAI Python SDK
[English](./README_CN.md) \| 简体中文
PAI Python SDK是阿里云 [机器学习平台 PAI(Platform for Artificial Intelligence)](https://www.aliyun.com/product/bigdata/learn) 提供的Python SDK,提供了更易用的HighLevel API,支持机器学习工程师简单地使用Python在PAI完成模型训练和部署,串联机器学习的流程。
## 🔧 安装
使用以下命令安装PAI Python SDK(支持Python版本 \>= 3.8):
```shell
python -m pip install pai
```
## 📖 文档
请通过访问 [PAI Python SDK文档](https://pai.readthedocs.io/) 或是查看 [docs](./docs) 目录下的文件获取SDK的详细文档,包括用户指南和API文档。
## 🛠 使用示例
- 提交自定义训练任务
以下代码演示了如何通过SDK提交一个自定义的训练作业:
```python
from pai.estimator import Estimator
from pai.image import retrieve
est = Estimator(
# 获取PAI提供的最新PyTorch镜像
image_uri=retrieve(
framework_name="PyTorch", framework_version="latest"
).image_uri,
command="echo hello",
# 可选,指定source_dir上传你的训练代码:
# source_dir="./train_src",
instance_type="ecs.c6.large",
)
# 提交训练任务
est.fit()
print(est.model_data())
```
- 部署大语言模型
PAI提供了大量预训练模型,可以使用PAI Python SDK轻松部署:
```python
from pai.model import RegisteredModel
# 获取PAI提供的QWen1.5-7b模型
qwen_model = RegisteredModel("qwen1.5-7b-chat", model_provider="pai")
# 部署模型
p = qwen_model.deploy(service_name="qwen_service")
# 调用服务
p.predict(
data={
"prompt": "What is the purpose of life?",
"system_prompt": "You are helpful assistant.",
"temperature": 0.8,
}
)
# PAI提供的大语言模型支持OpenAI API,可以通过openai SDK调用
openai_client = p.openai()
res = openai_client.chat.completions.create(
model="default",
max_tokens=1024,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the purpose of life?"}
]
)
print(res.choices[0].message.content)
```
- 微调预训练模型
通过PAI提供的微调脚本,提交一个模型微调任务
```python
from pai.model import ModelTrainingRecipe
training_recipe = ModelTrainingRecipe(
model_name="qwen2-0.5b-instruct",
model_provider="pai",
instance_type="ecs.gn6e-c12g1.3xlarge",
)
training_recipe.train(
inputs={
# 本地或是阿里云OSS上的数据路径(oss://<bucketname>/path/to/data)
"train": "<YourTrainingDataPath>"
}
)
```
通过访问PAI提供的示例仓库,可以了解更多使用示例:[pai-examples](https://github.com/aliyun/pai-examples/tree/master/pai-python-sdk)
## 🤝 贡献代码
我们欢迎为PAI Python SDK贡献代码。请阅读 [CONTRIBUTING](./CONTRIBUTING.md) 文件了解如何为本项目贡献代码。
## 📝 许可证
PAI Python SDK是由阿里云开发,并根据Apache许可证(版本2.0)授权使用。
## 📬 联系方式
如需支持或咨询,请在GitHub仓库中提交issue,或通过钉钉群联系我们:
<img src="./assets/dingtalk-group.png" alt="DingTalkGroup" width="500"/>
Raw data
{
"_id": null,
"home_page": "https://www.aliyun.com/product/bigdata/product/learn",
"name": "pai",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "ML Alibaba Cloud PAI Training Inference Pipeline",
"author": "Alibaba PAI team",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/6a/cf/c66a206457217cc10f7a99da0dd15da5ad7ee2148230dfef7e5a06e840b2/pai-0.4.10.post0.tar.gz",
"platform": null,
"description": "# PAI Python SDK\n\n[English](./README_CN.md) \\| \u7b80\u4f53\u4e2d\u6587\n\nPAI Python SDK\u662f\u963f\u91cc\u4e91 [\u673a\u5668\u5b66\u4e60\u5e73\u53f0 PAI(Platform for Artificial Intelligence)](https://www.aliyun.com/product/bigdata/learn) \u63d0\u4f9b\u7684Python SDK\uff0c\u63d0\u4f9b\u4e86\u66f4\u6613\u7528\u7684HighLevel API\uff0c\u652f\u6301\u673a\u5668\u5b66\u4e60\u5de5\u7a0b\u5e08\u7b80\u5355\u5730\u4f7f\u7528Python\u5728PAI\u5b8c\u6210\u6a21\u578b\u8bad\u7ec3\u548c\u90e8\u7f72\uff0c\u4e32\u8054\u673a\u5668\u5b66\u4e60\u7684\u6d41\u7a0b\u3002\n\n## \ud83d\udd27 \u5b89\u88c5\n\n\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PAI Python SDK\uff08\u652f\u6301Python\u7248\u672c \\>= 3.8\uff09\uff1a\n\n```shell\npython -m pip install pai\n```\n\n## \ud83d\udcd6 \u6587\u6863\n\n\u8bf7\u901a\u8fc7\u8bbf\u95ee [PAI Python SDK\u6587\u6863](https://pai.readthedocs.io/) \u6216\u662f\u67e5\u770b [docs](./docs) \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u83b7\u53d6SDK\u7684\u8be6\u7ec6\u6587\u6863\uff0c\u5305\u62ec\u7528\u6237\u6307\u5357\u548cAPI\u6587\u6863\u3002\n\n## \ud83d\udee0 \u4f7f\u7528\u793a\u4f8b\n\n- \u63d0\u4ea4\u81ea\u5b9a\u4e49\u8bad\u7ec3\u4efb\u52a1\n\n\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u901a\u8fc7SDK\u63d0\u4ea4\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u8bad\u7ec3\u4f5c\u4e1a:\n\n```python\nfrom pai.estimator import Estimator\nfrom pai.image import retrieve\n\nest = Estimator(\n # \u83b7\u53d6PAI\u63d0\u4f9b\u7684\u6700\u65b0PyTorch\u955c\u50cf\n image_uri=retrieve(\n framework_name=\"PyTorch\", framework_version=\"latest\"\n ).image_uri,\n command=\"echo hello\",\n # \u53ef\u9009\uff0c\u6307\u5b9asource_dir\u4e0a\u4f20\u4f60\u7684\u8bad\u7ec3\u4ee3\u7801\uff1a\n # source_dir=\"./train_src\",\n instance_type=\"ecs.c6.large\",\n)\n# \u63d0\u4ea4\u8bad\u7ec3\u4efb\u52a1\nest.fit()\nprint(est.model_data())\n\n```\n\n- \u90e8\u7f72\u5927\u8bed\u8a00\u6a21\u578b\n\nPAI\u63d0\u4f9b\u4e86\u5927\u91cf\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528PAI Python SDK\u8f7b\u677e\u90e8\u7f72\uff1a\n\n```python\nfrom pai.model import RegisteredModel\n\n# \u83b7\u53d6PAI\u63d0\u4f9b\u7684QWen1.5-7b\u6a21\u578b\nqwen_model = RegisteredModel(\"qwen1.5-7b-chat\", model_provider=\"pai\")\n\n# \u90e8\u7f72\u6a21\u578b\np = qwen_model.deploy(service_name=\"qwen_service\")\n\n# \u8c03\u7528\u670d\u52a1\np.predict(\n data={\n \"prompt\": \"What is the purpose of life?\",\n \"system_prompt\": \"You are helpful assistant.\",\n \"temperature\": 0.8,\n }\n)\n\n# PAI\u63d0\u4f9b\u7684\u5927\u8bed\u8a00\u6a21\u578b\u652f\u6301OpenAI API\uff0c\u53ef\u4ee5\u901a\u8fc7openai SDK\u8c03\u7528\nopenai_client = p.openai()\nres = openai_client.chat.completions.create(\n model=\"default\",\n max_tokens=1024,\n messages=[\n {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n {\"role\": \"user\", \"content\": \"What is the purpose of life?\"}\n ]\n)\nprint(res.choices[0].message.content)\n\n```\n\n- \u5fae\u8c03\u9884\u8bad\u7ec3\u6a21\u578b\n\n\u901a\u8fc7PAI\u63d0\u4f9b\u7684\u5fae\u8c03\u811a\u672c\uff0c\u63d0\u4ea4\u4e00\u4e2a\u6a21\u578b\u5fae\u8c03\u4efb\u52a1\n\n```python\n\nfrom pai.model import ModelTrainingRecipe\n\ntraining_recipe = ModelTrainingRecipe(\n model_name=\"qwen2-0.5b-instruct\",\n model_provider=\"pai\",\n instance_type=\"ecs.gn6e-c12g1.3xlarge\",\n)\n\ntraining_recipe.train(\n inputs={\n # \u672c\u5730\u6216\u662f\u963f\u91cc\u4e91OSS\u4e0a\u7684\u6570\u636e\u8def\u5f84(oss://<bucketname>/path/to/data)\n \"train\": \"<YourTrainingDataPath>\"\n }\n)\n\n\n```\n\n\u901a\u8fc7\u8bbf\u95eePAI\u63d0\u4f9b\u7684\u793a\u4f8b\u4ed3\u5e93\uff0c\u53ef\u4ee5\u4e86\u89e3\u66f4\u591a\u4f7f\u7528\u793a\u4f8b\uff1a[pai-examples](https://github.com/aliyun/pai-examples/tree/master/pai-python-sdk)\n\n## \ud83e\udd1d \u8d21\u732e\u4ee3\u7801\n\n\u6211\u4eec\u6b22\u8fce\u4e3aPAI Python SDK\u8d21\u732e\u4ee3\u7801\u3002\u8bf7\u9605\u8bfb [CONTRIBUTING](./CONTRIBUTING.md) \u6587\u4ef6\u4e86\u89e3\u5982\u4f55\u4e3a\u672c\u9879\u76ee\u8d21\u732e\u4ee3\u7801\u3002\n\n## \ud83d\udcdd \u8bb8\u53ef\u8bc1\n\nPAI Python SDK\u662f\u7531\u963f\u91cc\u4e91\u5f00\u53d1\uff0c\u5e76\u6839\u636eApache\u8bb8\u53ef\u8bc1\uff08\u7248\u672c2.0\uff09\u6388\u6743\u4f7f\u7528\u3002\n\n## \ud83d\udcec \u8054\u7cfb\u65b9\u5f0f\n\n\u5982\u9700\u652f\u6301\u6216\u54a8\u8be2\uff0c\u8bf7\u5728GitHub\u4ed3\u5e93\u4e2d\u63d0\u4ea4issue\uff0c\u6216\u901a\u8fc7\u9489\u9489\u7fa4\u8054\u7cfb\u6211\u4eec\uff1a\n\n<img src=\"./assets/dingtalk-group.png\" alt=\"DingTalkGroup\" width=\"500\"/>\n\n\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "Alibaba Cloud PAI Python SDK",
"version": "0.4.10.post0",
"project_urls": {
"Homepage": "https://www.aliyun.com/product/bigdata/product/learn"
},
"split_keywords": [
"ml",
"alibaba",
"cloud",
"pai",
"training",
"inference",
"pipeline"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a37cce2f82d7a9d20603e5528ca2effa26a1e62bce23f707df5fddb5c7f191ad",
"md5": "56b1bde92b14c0df3450dddeabeb69a1",
"sha256": "9aaa22d1076f4e359be995b3e4df0fe4825b97c657cc3bd5efbd0d9cac3dac55"
},
"downloads": -1,
"filename": "pai-0.4.10.post0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "56b1bde92b14c0df3450dddeabeb69a1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 417243,
"upload_time": "2024-10-21T07:16:44",
"upload_time_iso_8601": "2024-10-21T07:16:44.487586Z",
"url": "https://files.pythonhosted.org/packages/a3/7c/ce2f82d7a9d20603e5528ca2effa26a1e62bce23f707df5fddb5c7f191ad/pai-0.4.10.post0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6acfc66a206457217cc10f7a99da0dd15da5ad7ee2148230dfef7e5a06e840b2",
"md5": "0b6ab7b86e98cbbd8d24dafa4096769f",
"sha256": "98c11a9a6879c30cbd053f8c7c675a86a8fbc66242b1194a7a60191ac1d7253b"
},
"downloads": -1,
"filename": "pai-0.4.10.post0.tar.gz",
"has_sig": false,
"md5_digest": "0b6ab7b86e98cbbd8d24dafa4096769f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 10265045,
"upload_time": "2024-10-21T07:16:46",
"upload_time_iso_8601": "2024-10-21T07:16:46.305429Z",
"url": "https://files.pythonhosted.org/packages/6a/cf/c66a206457217cc10f7a99da0dd15da5ad7ee2148230dfef7e5a06e840b2/pai-0.4.10.post0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-21 07:16:46",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pai"
}