使用自定义基础模型编码 AutoAI RAG 实验

查看指南和代码示例,了解如何编写 AutoAI RAG 实验和使用自定义基础模型。

自定义模型部署使用 watsonx.ai Python 客户端库 (版本 1.3.17 或更高版本)。

请按照以下步骤在 AutoAI RAG 实验中使用自定义基础模型。

  1. 为部署自定义基础模型准备先决条件
  2. 部署模型
  3. 准备接地数据
  4. 准备评估数据
  5. 运行实验
  6. 查看模式并选择最佳模式

步骤 1:准备自定义基础模型部署的先决条件

  1. 下载模型快照。

    from pathlib import Path
    from huggingface_hub import snapshot_download
    
    byom_cache_dir = Path("your", "model", "cache", "dir")
    
    if not byom_cache_dir.exists():
        raise FileExistsError("Please use the path which exists.")
    
    if byom_cache_dir.is_file():
        raise NotADirectoryError("Please use the path which points to a directory.")
    
    snapshot_download(HUGGING_FACE_MODEL_REPOSITORY, cache_dir=byom_cache_dir)
    
  2. 使用代码初始化客户端。 例如:

    from ibm_watsonx_ai import APIClient, Credentials
    
    credentials = Credentials(
                    url=URL,
                    username=USERNAME,
                    password=PASSWORD,
                    instance_id=INSTANCE_ID,
                    version=VERSION,
                    verify=False,
                )
    
    client = APIClient(credentials=credentials,  project_id=PROJECT_ID)
    
  3. 连接至 S3Bucket。

    from ibm_watsonx_ai.helpers.connections import DataConnection, S3Location
    
    location = S3Location(bucket=BUCKET_NAME, path=BUCKET_MODEL_DIR_NAME)
    data_connection = DataConnection(location=location, connection_asset_id=DATASOURCE_CONNECTION_ASSET_ID)
    data_connection.set_client(api_client=client)
    
  4. 将模型文件上传到 S3Bucket。

    model_files = byom_cache_dir / "model_dir_name" / "snapshots" / "snapshot_id"
    
    if not model_files.exists():
        raise FileExistsError("Please use the snapshot path which exists.")
    
    if model_files.is_file():
        raise NotADirectoryError("Please use the snapshot path which points to a directory.")
    
    for model_file in model_files.iterdir():
    
        # avoid uploading unnecessary files
        if model_file.name.startswith("."):
            continue
    
        data_connection.write(data=str(model_file), remote_name=model_file.name)
    

步骤 2:部署模型

要部署自定义基础模型,请按照自定义模型文档中的步骤操作。

步骤 3:准备接地数据

准备并连接用于运行 RAG 实验的接地文件。 详情请参阅在项目中获取和准备数据

  • 支持的格式:PDF、HTML、 DOCX、Markdown、纯文本
  • 连接 Cloud Object Storage 邮筒中的数据、邮筒中的文件夹或指定多达 20 个文件。
  • AutoAI 使用文件样本进行实验

例如,当文件存储在 Cloud Object Storage 文件桶中时,创建数据连接:

from ibm_watsonx_ai.helpers import DataConnection, S3Location

datasource_name = 'bluemixcloudobjectstorage'

conn_meta_props= {
    client.connections.ConfigurationMetaNames.NAME: f"Connection to input data - {datasource_name} ",
    client.connections.ConfigurationMetaNames.DATASOURCE_TYPE: client.connections.get_datasource_type_id_by_name(datasource_name),
    client.connections.ConfigurationMetaNames.DESCRIPTION: "ibm-watsonx-ai SDK documentation",
    client.connections.ConfigurationMetaNames.PROPERTIES: {
        'bucket': <BUCKET_NAME>,
        'access_key': <ACCESS_KEY>,
        'secret_key': <SECRET_ACCESS_KEY>,
        'iam_url': 'https://iam.cloud.ibm.com/identity/token',
        'url': <ENDPOINT_URL>
    }
}

conn_details = client.connections.create(meta_props=conn_meta_props)
cos_connection_id = client.connections.get_id(conn_details)

input_data_references = [
    DataConnection(
        connection_asset_id=cos_connection_id,
        location=S3Location(
            bucket=<BUCKET_NAME>,
            path=<BUCKET_PATH>
        )
    )
]

步骤 4:准备评估数据

  1. 下载 granite_code_models.pdf 文件。

    import wget
    
    data_url = "https://arxiv.org/pdf/2405.04324"
    byom_input_filename = "granite_code_models.pdf"
    wget.download(data_url, byom_input_filename)
    
  2. 准备评估数据。

    如需 correct_answer_document_ids ,请提供下载的文件名。

    import json
    
    local_benchmark_json_filename = "benchmark.json"
    
    benchmarking_data = [
        {
            "question": "What are the two main variants of Granite Code models?",
            "correct_answer": "The two main variants are Granite Code Base and Granite Code Instruct.",
            "correct_answer_document_ids": [byom_input_filename]
        },
        {
            "question": "What is the purpose of Granite Code Instruct models?",
            "correct_answer": "Granite Code Instruct models are finetuned for instruction-following tasks using datasets like CommitPack, OASST, HelpSteer, and synthetic code instruction datasets, aiming to improve reasoning and instruction-following capabilities.",
            "correct_answer_document_ids": [byom_input_filename]
        },
        {
            "question": "What is the licensing model for Granite Code models?",
            "correct_answer": "Granite Code models are released under the Apache 2.0 license, ensuring permissive and enterprise-friendly usage.",
            "correct_answer_document_ids": [byom_input_filename]
        },
    ]
    
    with open(local_benchmark_json_filename, mode="w", encoding="utf-8") as fp:
        json.dump(benchmarking_data, fp, indent=4)
    
  3. 将评估文件上传到 Cloud Object Storage 文件桶。

    documents_dir_location = S3Location(bucket=BUCKET_NAME, path=byom_input_filename)
    documents_dir_data_connection = DataConnection(location=documents_dir_location, connection_asset_id=DATASOURCE_CONNECTION_ASSET_ID)
    documents_dir_data_connection.set_client(api_client=client)
    documents_dir_data_connection.write(data=byom_input_filename, remote_name=byom_input_filename)
    
    benchmark_file_location = S3Location(bucket=BUCKET_NAME, path=BUCKET_BENCHMARK_JSON_FILE_PATH)
    benchmark_file_data_connection = DataConnection(location=benchmark_file_location, connection_asset_id=DATASOURCE_CONNECTION_ASSET_ID)
    benchmark_file_data_connection.set_client(api_client=client)
    benchmark_file_data_connection.write(data=local_benchmark_json_filename)
    

第 5 步:使用自定义基础模型运行 AutoAI RAG 实验

使用 Python SDK 运行实验。 对于 deployment_id ,请提供已部署的自定义基础模型的 ID。

from ibm_watsonx_ai.experiment import AutoAI
from ibm_watsonx_ai.foundation_models.schema import (
        AutoAIRAGCustomModelConfig,
        AutoAIRAGModelParams
)

experiment = AutoAI(credentials, project_id=PROJECT_ID)

custom_prompt_template_text = "Answer my question {question} related to these documents {reference_documents}."
custom_context_template_text = "My document {document}"

parameters = AutoAIRAGModelParams(max_sequence_length=32_000)

custom_foundation_model = AutoAIRAGCustomModelConfig(
    deployment_id=deployment_id,
    project_id=PROJECT_ID,
    prompt_template_text=custom_prompt_template_text,
    context_template_text=custom_context_template_text,
    parameters=parameters
)

rag_optimizer = experiment.rag_optimizer(
    name='AutoAI RAG - Custom foundation model experiment',
    description = "AutoAI RAG experiment with custom foundation model.",
    max_number_of_rag_patterns=4,
    optimization_metrics=['faithfulness'],
    foundation_models=[custom_foundation_model],
)

rag_optimizer.run(
    test_data_references=[benchmark_file_data_connection],
    input_data_references=[documents_dir_data_connection],
)

如需了解工作详情,请使用

rag_optimizer.get_details()

状态完成后,您就可以进入下一步。

步骤 6:审查模式并选择最佳模式

AutoAI RAG 实验成功完成后,您可以查看图案。 使用 summary 方法,以 Pandas DataFrame 的形式列出已完成的模式和评估指标信息,这样您就可以根据针对优化指标的性能排序来查看模式。

summary = rag_optimizer.summary()
summary

例如,模式结果显示如下:

模式 平均正确率 平均忠实度 平均语境正确性 chunking.chunk_size embeddings.model_id vector_store.distance_metric retrieval.method retrieval.number_of_chunks generation.deployment_id
Pattern1 0.6802 0.5407 1.0000 512 ibm/slate-125m-english-rtrvr 欧几里得 窗口 5 38aeef16-c69c-4858-ba69-42f97d965abc
Pattern2 0.7172 0.5950 1.0000 1024 intfloat/multilingual-e5-large 欧几里得 窗口 5 38aeef16-c69c-4858-ba69-42f97d965abc
Pattern3 0.6543 0.5144 1.0000 1024 intfloat/multilingual-e5-large 欧几里得 简式 5 38aeef16-c69c-4858-ba69-42f97d965abc
Pattern4 0.6216 0.5030 1.0000 1024 intfloat/multilingual-e5-large 余弦 窗口 5 38aeef16-c69c-4858-ba69-42f97d965abc
Pattern5 0.7369 0.5630 1.0000 1024 intfloat/multilingual-e5-large 余弦 窗口 3 38aeef16-c69c-4858-ba69-42f97d965abc

选择一个模式进行本地测试

  1. 重新创建文档索引,然后才能选择模式并在本地进行测试。

    提示:

    在下面的示例代码中,索引是通过文档 core_api.htmlfm_embeddings.html 建立的。

    from langchain_community.document_loaders import WebBaseLoader
    
    best_pattern = rag_optimizer.get_pattern()
    
    urls = [
        "https://ibm.github.io/watsonx-ai-python-sdk/core_api.html",
        "https://ibm.github.io/watsonx-ai-python-sdk/fm_embeddings.html",
    ]
    docs_list = WebBaseLoader(urls).load()
    doc_splits = best_pattern.chunker.split_documents(docs_list)
    best_pattern.indexing_function(doc_splits)
    
  2. 在本地查询 RAG 模式。

    from ibm_watsonx_ai.deployments import RuntimeContext
    
    runtime_context = RuntimeContext(api_client=client)
    inference_service_function = best_pattern.inference_service(runtime_context)[0]
    
    question = "How to use new approach of providing credentials to APIClient?"
    
    context = RuntimeContext(
        api_client=client,
        request_payload_json={"messages": [{"role": "user", "content": question}]},
    )
    print(inference_service_function(context)["body"]["choices"][0]["message"]["content"])
    

模型的反应是这样的

According to the document, the new approach to provide credentials to APIClient is by using the Credentials class. Here's an example:


from ibm_watsonx_ai import APIClient
from ibm_watsonx_ai import Credentials

credentials = Credentials(
                   url = "https://us-south.ml.cloud.ibm.com",
                   token = "***********",
                  )

client = APIClient(credentials)


This replaces the old approach of passing a dictionary with credentials to the APIClient constructor.
提示:

要检索特定模式,请将模式编号传给 rag_optimizer.get_pattern()

获取推理和索引笔记本

要下载指定的推理笔记,请使用 get_inference_notebook()。 如果 pattern_name 留空,该方法将下载计算结果最佳的图案笔记本。

rag_optimizer.get_inference_notebook(pattern_name='Pattern3')

有关详细信息和代码示例,请参阅使用 AutoAI RAG 与自定义基础模型笔记本