Implementation:Hpcaitech ColossalAI PanGuLLM
| Knowledge Sources | |
|---|---|
| Domains | NLP, LLM Integration, Cloud Services |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Pangu (PanGu LLM) is a LangChain-compatible LLM wrapper that integrates with Huawei's PanGu large language models hosted on the Huawei Cloud platform via REST API.
Description
This class extends LangChain's LLM base class to provide access to Huawei's PanGu models. It authenticates through Huawei Cloud's IAM token system using username, password, and domain credentials, and communicates with the model endpoint via HTTPS. The class supports both text completion and chat model interfaces, with configurable generation parameters including max_tokens, temperature, and top-p sampling. Stop-word truncation is applied to the generated text before returning.
Usage
Use Pangu when you need to integrate a Huawei PanGu model into a LangChain-based ColossalQA pipeline. You must set the URL, USERNAME, PASSWORD, and DOMAIN_NAME environment variables or pass them explicitly through set_auth_config.
Code Reference
Source Location
- Repository: Hpcaitech_ColossalAI
- File: applications/ColossalQA/colossalqa/local/pangu_llm.py
- Lines: 1-150
Signature
class Pangu(LLM):
n: int
gen_config: dict = None
auth_config: dict = None
def __init__(self, gen_config=None, **kwargs):
...
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
...
def set_auth_config(self, **kwargs):
...
def get_latest_auth_token(self, region, username, password, domain_name):
...
def text_completion(self, text, gen_config, auth_config):
...
def chat_model(self, messages, gen_config, auth_config):
...
Import
from colossalqa.local.pangu_llm import Pangu
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n | int | Yes | Instance identifier parameter |
| gen_config | dict | No | Generation configuration dictionary (default: {"user": "User", "max_tokens": 50, "temperature": 0.95, "n": 1}) |
| prompt | str | Yes | The text prompt to send to the PanGu model (used in _call) |
| stop | Optional[List[str]] | No | List of stop words to truncate the generated response |
| messages | list | Yes (for chat_model) | List of message dictionaries for the chat model interface |
Outputs
| Name | Type | Description |
|---|---|---|
| _call return | str | The generated text from the PanGu model, optionally truncated at stop words |
| text_completion return | dict | Raw API response dictionary containing choices with generated text |
| chat_model return | dict | Raw API response dictionary from the chat endpoint |
| get_latest_auth_token return | str | IAM authentication token (X-Subject-Token) for Huawei Cloud API |
Environment Variables
| Variable | Description |
|---|---|
| URL | The full endpoint URL for the PanGu model service |
| USERNAME | IAM username for Huawei Cloud authentication |
| PASSWORD | IAM user password for Huawei Cloud authentication |
| DOMAIN_NAME | The Huawei Cloud account domain name (parent account managing IAM users) |
Usage Examples
import os
from colossalqa.local.pangu_llm import Pangu
# Set Huawei Cloud credentials
os.environ["URL"] = "https://pangu.cn-north-4.myhuaweicloud.com/v1/completions"
os.environ["USERNAME"] = "my_iam_user"
os.environ["PASSWORD"] = "my_password"
os.environ["DOMAIN_NAME"] = "my_domain"
# Create and configure the PanGu LLM
pg = Pangu(n=1)
pg.set_auth_config()
# Text completion
response = pg("What is deep learning?")
print(response)
# Chat model usage
messages = [{"role": "user", "content": "Explain transformers."}]
chat_response = pg.chat_model(
messages=messages,
gen_config=pg.gen_config,
auth_config=pg.auth_config,
)