Volcengine Python SDK

repository·main·Indexed 42 days ago

https://github.com/volcengine/volc-sdk-python

A Python SDK providing programmatic access to Volcengine cloud services, including Visual Intelligence, NLP, BioOS, and Log Service (TLS). It features tools for text privatization using local differential privacy for LLM fine-tuning, as well as comprehensive log management capabilities including project and topic creation, index definition, and high-throughput log writing via put_logs_v2.

Tokens
11.2K
Snippets
38
Records
44
Agent score
53%

What's inside Volcengine

  1. How text privatization works

    main

    Text privatization is designed to mitigate privacy leakage risks when uploading private training data for Large Language Model (LLM) fine-tuning.

    It utilizes local differential privacy techniques to perturb training data locally before it is used. Currently, the feature only supports text classification tasks and employs a dynamic privacy budget allocation mechanism based on token contribution to balance utility and privacy.

  2. Consume logs using Consumer Groups in TLS

    main

    The TLS (Log Service) provides a ConsumerGroup feature to simplify log consumption. Unlike the standard ConsumeLogs OpenAPI, which requires manual management of Shard IDs, Cursors, and load balancing, ConsumerGroup automatically handles shard allocation, load balancing, and fault tolerance across multiple consumers.

    To consume logs, you must implement a custom LogProcessor by inheriting from the LogProcessor class and overriding its process method. This method receives a LogGroupList containing the logs to be processed.

    from volcengine.tls.consumer.consumer import LogProcessor
    from volcengine.tls.log_pb2 import LogGroupList
    
    class MyLogProcessor(LogProcessor):
        def process(self, topic_id: str, shard_id: int, log_group_list: LogGroupList):
            # Implement your business logic here
            for log_group in log_group_list.log_groups:
                for log in log_group.logs:
                    for content in log.contents:
                        print(f"{content.key}: {content.value}")
  3. Quickstart: Initialize BioOsService and list workspaces

    main

    To use BioOS, initialize the BioOsService class. You can specify the endpoint and region if the defaults are insufficient. If you have not configured your credentials in $HOME/.volc/config, use set_ak() and set_sk() to provide your Access Key and Secret Key manually.

    from __future__ import print_function
    
    from volcengine.bioos.BioOsService import BioOsService
    
    if __name__ == '__main__':
        # set endpoint/region here if the default value is unsatisfied
        bioos_service = BioOsService(endpoint='endpoint', region='region')
    
        # call below method if you don't set ak and sk in $HOME/.volc/config
        bioos_service.set_ak('ak')
        bioos_service.set_sk('sk')
    
        params = {}
    
        resp = bioos_service.list_workspaces(params)
        print(resp)
  4. Configure Access Key (AK) and Secret Key (SK) permissions

    main

    If you are using a sub-account (RAM user), ensure it has the necessary permissions to access BioOS services.

    1. Log in to the Volcengine console.
    2. Click your avatar in the top right and select "Access Control" (访问控制).
    3. Click "Policy Management" (策略管理) on the left.
    4. Verify that the sub-account has AccessKeyFullAccess permissions or a higher-level permission like AdministratorAccess. If not, use the main account to grant these permissions to the sub-account.
  5. Configure Volcengine security credentials

    main

    The SDK requires an Access Key ID (AK) and a Secret Access Key (SK) to authenticate requests. You can provide these credentials using one of three methods:

    Pass the credentials directly to the service client instance using set_ak() and set_sk().

    Method 2: Use environment variables

    Set the following environment variables in your shell or system:

    • VOLC_ACCESSKEY
    • VOLC_SECRETKEY

    Method 3: Use a configuration file

    Create a JSON file at ~/.volc/config (under your HOME directory) with the following structure:

    # Method 1 Example
    iam_service = IamService()
    iam_service.set_ak('Your AK')
    iam_service.set_sk('Your SK')
  6. Initialize the TLS Client

    main

    To interact with the Volcengine Log Service (TLS), you must first initialize a TLSService client. It is recommended to use environment variables for authentication to avoid hardcoding AccessKeys.

    Important: The endpoint environment variable must NOT include the protocol prefix (e.g., use tls-cn-beijing.ivolces.com instead of https://tls-cn-beijing.ivolces.com).

    If you are using STS (Security Token Service), provide the temporary access_key_id and access_key_secret, and ensure VOLCENGINE_TOKEN is set. For non-STS usage, the token part should be empty.

    import os
    from volcengine.tls.TLSService import TLSService
    
    # Ensure endpoint does not contain http:// or https://
    endpoint = os.environ["VOLCENGINE_ENDPOINT"]
    region = os.environ["VOLCENGINE_REGION"]
    access_key_id = os.environ["VOLCENGINE_ACCESS_KEY_ID"]
    access_key_secret = os.environ["VOLCENGINE_ACCESS_KEY_SECRET"]
    
    tls_service = TLSService(endpoint, access_key_id, access_key_secret, region)
  7. Generate and view BioOS Sphinx documentation locally

    main

    To build and view the BioOS technical documentation locally using Sphinx, follow these steps:

    1. Navigate to the volcengine/bioos/doc directory and generate the HTML files:
      make html
    2. Start a local web server to host the generated files (replace ${port} with your desired port number):
      python -m http.server ${port}
    3. Open your browser and navigate to http://127.0.0.1:${port} to view the documentation.
    # Generate HTML
    make html
    
    # Start server
    python -m http.server ${port}
  8. Install the Volcengine Python SDK and dependencies

    main

    To use the text privatization features, ensure you are using Python 3.7 or higher. Install the SDK using pip, and then install the required machine learning dependencies.

    # Install the SDK
    pip install --user volcengine
    
    # Or upgrade if already installed
    pip install --upgrade volcengine
    
    # Install required dependencies
    pip install numpy typing_extensions sentencepiece torch~=1.13 transformers~=4.30