Baidu Qianfan SDK

repository·main·Indexed 18 days ago

https://github.com/baidubce/bce-qianfan-sdk

A multi-language toolchain (Python, Go, Java, JS) for the Baidu Qianfan Large Model Platform. It provides comprehensive capabilities for model inference, fine-tuning (SFT), and model management. Key features include RAG integration with various vector databases, autonomous agent implementation via function calling, and support for frameworks like LangChain, LlamaIndex, and Semantic Kernel. The SDK includes a Trainer for full LLMops workflows and tools for automatic inference configuration optimization.

Tokens
166.3K
Snippets
532
Records
591
Agent score
64%

What's inside bce-qianfan-sdk

  1. Overview of Qianfan SDK capabilities

    main

    The Qianfan SDK provides access to several categories of capabilities on the Baidu Intelligent Cloud Qianfan platform:

    • Model Capabilities (Inference): Chat, Completion, Embedding, Plugin calling, and Text2Image.
    • Model Fine-tuning: Training and optimizing models.
    • Model Management: Managing model lifecycle and services.
    • Dataset Management: Handling training and evaluation datasets.
    • Prompt Management: Managing prompt templates.
    • Utilities: Tokenizer and interface flow control.
  2. Overview of Qianfan SDK

    main

    The Qianfan SDK provides a best-practice toolchain for the Baidu Qianfan Large Model Platform, enabling seamless access for AI workflows and AI-native applications. The SDK's core capabilities are divided into three main areas:

    • Large Model Inference: Encapsulates interfaces for the ERNIE (一言) series and various open-source models. It supports tasks such as Chat, Completion, and Embedding.
    • Large Model Training: Supports end-to-end training processes, including training data management, fine-tuning/pre-training, and model hosting.
    • General & Extensions: Includes common AI development tools like Prompt, Debug, and Client, as well as extensions that adapt to common middleware frameworks.
  3. Perform Large Model fine-tuning and training

    main

    The Qianfan platform supports training and fine-tuning capabilities for large models. You can manage the lifecycle of a training job through the following operations:

    • Create a training task.
    • Create a task run.
    • Get details of a task run.
    • Stop a running task.
  4. Manage platform datasets

    main

    The Qianfan platform provides comprehensive APIs for dataset lifecycle management, including:

    • Creation & Deletion: Create and delete datasets.
    • Data Movement: Initiate dataset publishing, importing, and exporting tasks; get export records and import error details.
    • Status: Get dataset details and status details.
    • Data Processing: Create and manage data cleaning tasks (view list, view details, delete) and data augmentation tasks (view list, view details, delete).
    • Annotation: Entity annotation, deleting entities, and retrieving entity lists.
  5. Explore available Large Model capabilities

    main

    The Qianfan SDK provides access to several core large model prediction capabilities. For detailed implementation and usage, refer to the inference documentation. Supported capabilities include:

    • Chat: Conversational AI interactions.
    • Completion: Text completion/continuation.
    • Embedding: Converting text into vector representations.
    • Plugin: Calling external plugins.
    • Text-to-Image: Generating images from text descriptions.
  6. Manage trained models

    main

    Once training is complete, you can manage your models using the following operations:

    • Get model details.
    • Get model version details.
    • Publish a training task as a model.
    • Initiate a model evaluation task.
    • View model evaluation details.
    • View model evaluation reports.
    • Stop a model evaluation task.
  7. Use the Trainer for full fine-tuning workflows

    main

    The qianfan SDK provides a Trainer to manage the complete fine-tuning lifecycle. This includes:

    • Loading local datasets via Dataset and uploading them to the Qianfan platform.
    • Fine-tuning specific models (e.g., ERNIE-Speed-8K).
    • Running batch evaluations on the fine-tuned model.
    • Publishing the model as a service.

    The SDK also supports Event Handlers for monitoring training and Resumability, allowing you to register an EventHandler and resume a training task from a failure point.

  8. Integrate Qianfan with Semantic Kernel

    main

    The Qianfan SDK provides extensions to integrate Baidu Qianfan's large model capabilities into the Microsoft Semantic Kernel (SK) framework. This allows developers to use Qianfan models as services within the SK ecosystem.

    Supported integration types include:

    • QianfanChatCompletion: For text dialogue/chat.
    • QianfanTextCompletion: For text completion/continuation.
    • QianfanTextEmbedding: For text vectorization.
  9. Authentication via Application API Key (AK/SK)

    main

    If you cannot obtain an IAM Access Key/Secret Key, you can use Application-level API Key (AK) and Secret Key (SK).

    Warning: This method is considered legacy and will be removed from the SDK in the future. It is also limited in scope: it cannot be used for training, model management, or dataset management functions.

    Set the following environment variables for this method:

    • QIANFAN_AK
    • QIANFAN_SK
    import os
    import qianfan
    
    os.environ["QIANFAN_AK"] = "..."
    os.environ["QIANFAN_SK"] = "..."
    
    # Only supports inference-related functions
    chat = qianfan.ChatCompletion()
    resp = chat.do(messages=[{"role": "user", "content": "你好,千帆"}])
    
    print(resp["result"])