Skip to main content

PAI Python SDK

English | 简体中文

PAI Python SDK是阿里云 机器学习平台 PAI(Platform for Artificial Intelligence) 提供的Python SDK,提供了更易用的HighLevel API,支持机器学习工程师简单地使用Python在PAI完成模型训练和部署,串联机器学习的流程。

🔧 安装

使用以下命令安装PAI Python SDK(支持Python版本 >= 3.8):

python -m pip install pai

📖 文档

请通过访问 PAI Python SDK文档 或是查看 docs 目录下的文件获取SDK的详细文档,包括用户指南和API文档。

🛠 使用示例

  • 提交自定义训练任务

以下代码演示了如何通过SDK提交一个自定义的训练作业:

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轻松部署:

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提供的微调脚本,提交一个模型微调任务

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

🤝 贡献代码

我们欢迎为PAI Python SDK贡献代码。请阅读 CONTRIBUTING 文件了解如何为本项目贡献代码。

📝 许可证

PAI Python SDK是由阿里云开发,并根据Apache许可证(版本2.0)授权使用。

📬 联系方式

如需支持或咨询,请在GitHub仓库中提交issue,或通过钉钉群联系我们:

DingTalkGroup

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alipai-0.4.13.tar.gz (379.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alipai-0.4.13-py3-none-any.whl (441.1 kB view details)

Uploaded Python 3

File details

Details for the file alipai-0.4.13.tar.gz.

File metadata

  • Download URL: alipai-0.4.13.tar.gz
  • Upload date:
  • Size: 379.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for alipai-0.4.13.tar.gz
Algorithm Hash digest
SHA256 c882f2b938143b798f53a91bd119f01f3f04746bf0c992e9e9b51a06bd72592a
MD5 c2b92723a51eac63e2c66c2e6cd7e3df
BLAKE2b-256 7b9d7daa1d31f648f0e049f22948d89a0abe4b98bd1100178de478d46af247e6

See more details on using hashes here.

File details

Details for the file alipai-0.4.13-py3-none-any.whl.

File metadata

  • Download URL: alipai-0.4.13-py3-none-any.whl
  • Upload date:
  • Size: 441.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for alipai-0.4.13-py3-none-any.whl
Algorithm Hash digest
SHA256 c8e4658e88e26f3d31a0ee3fc3c8cabebaf8ca8718edd0c5566460df5822ba13
MD5 65e4e20c430680242200ab0c8ed6ee41
BLAKE2b-256 8a5464ceba820fb0264170080ccd7b1be9be361b94913b9b6e4c3d0ab55d00f8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.13 This release

2 files

0.4.12.post0

2 files

0.4.12

2 files

0.4.11

2 files

0.4.10.post0

2 files

0.4.10

2 files

0.4.9.post0

2 files

0.4.9

2 files

0.4.8

2 files

0.4.7.post0

2 files

0.4.7

2 files

0.4.6

2 files

0.4.5.post1

2 files

0.4.5.post0

2 files

0.4.5

2 files

0.4.4.post0

1 file

0.4.4

1 file

0.4.3

1 file

0.4.2.post0

1 file

0.4.2

1 file

0.4.1

1 file

0.4.0.post0

1 file

0.4.0

1 file

0.3.7

1 file

0.1.7

1 file

0.1.6.post2

1 file

0.1.6.post1

1 file

0.1.6.post0

1 file

0.1.6

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page