You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python脚本为现有服务配置默认LLM_RAG_CRACK_AND_CHUNK_AND_EMBED?

配置LLM_RAG_CRACK_AND_CHUNK_AND_EMBED组件的步骤

你当前用msrest.Configuration的方式不适用该组件,它属于Azure ML预构建托管组件,需用Azure ML SDK v2完成配置与调用,以下是完整流程:

1. 安装依赖

先安装所需SDK包:

pip install azure-ai-ml azure-identity

2. 完整代码模板

替换你现有代码,用SDK v2加载组件并配置参数:

from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azure.ai.ml.entities import Input, Output, Job

# 初始化Azure ML客户端
ml_client = MLClient(
    credential=DefaultAzureCredential(),
    # 替换为你的Azure订阅ID、资源组名、工作区名称
    subscription_id="your-subscription-id",
    resource_group_name="your-resource-group",
    workspace_name="your-workspace-name"
)

# 从公共注册表加载预构建组件
component = ml_client.components.get(
    name="llm_rag_crack_and_chunk_and_embed",
    label="default",
    registry_name="azureml"
)

# 配置输入输出路径
input_data = Input(
    type="uri_folder",
    path="your-input-data-path"  # 示例:azureml://datastores/your-datastore/path/to/docs
)
output_chunked_data = Output(
    type="uri_folder",
    path="your-output-chunk-path"
)
output_embeddings = Output(
    type="uri_folder",
    path="your-output-embeddings-path"
)

# 配置Azure Cognitive Search参数(需提前创建对应索引)
search_config = {
    "azure_search_endpoint": "https://xyz.search.windows.net",
    "azure_search_key": credential.get_token("https://search.azure.com/.default").token,
    "azure_search_index_name": "your-search-index-name"
}

# 创建并提交处理作业
job = Job(
    component=component,
    inputs={
        "input_data": input_data,
        "chunk_size": 1000,  # 可自定义分块大小,默认值参考组件内置配置
        "chunk_overlap": 200,  # 分块重叠长度,按需调整
        "embedding_model": "text-embedding-ada-002",  # 使用的嵌入模型
        "azure_search_config": search_config
    },
    outputs={
        "chunked_data": output_chunked_data,
        "embeddings": output_embeddings
    }
)

# 提交作业到工作区
submitted_job = ml_client.jobs.create_or_update(job)
print(f"作业已提交,ID:{submitted_job.id}")

# 可选:实时监控作业状态
ml_client.jobs.stream(submitted_job.name)

3. 关键参数说明

  • 分块参数:chunk_size和chunk_overlap可根据文档类型(如长文档/短文档)调整,优化后续检索精度。
  • 嵌入模型:默认使用OpenAI的text-embedding-ada-002,也可替换为你在Azure OpenAI部署的自定义模型。
  • 搜索服务配置:若无需写入搜索服务,可移除azure_search_config参数,仅保留本地/存储账户的输出路径。

4. 注意事项

  • 确保账号拥有Azure ML工作区的操作权限,以及Azure Cognitive Search的读写权限。
  • 输入支持PDF、Word、TXT等常见文档格式,组件会自动完成文档解析(crack)。
  • 本地文件需先上传至Azure ML数据存储,或用Input(type="uri_file", path="./local-file.pdf")指定单个文件。

内容的提问来源于stack exchange,提问作者Mike B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 15:07:23