Yi Documentation

repository·main·Indexed 27 days ago

https://github.com/01-ai/yi

Documentation for Yi, a series of high-performance, bilingual (English/Chinese) large language models developed by 01.AI. Includes guides for inference using Transformers, vLLM, and LMDeploy; quantization via AutoAWQ and AutoGPTQ; fine-tuning with Swift and Llama-Factory; and local deployment via Ollama, llama.cpp, and MLX-LM. Also covers building RAG systems with LlamaIndex and LangChain, implementing function calling, and hardware requirements for Yi-6B and Yi-34B.

Tokens
50K
Snippets
175
Records
251
Agent score
92%

What's inside Yi

  1. Overview of Yi series models

    main

    The Yi series are bilingual (English/Chinese) large language models developed by 01.AI. The series includes base models in two primary parameter sizes:

    • 6B: Available as Yi-6B (4K sequence length, extendable to 32K during inference) and Yi-6B-200K (200K context length).
    • 34B: Available as Yi-34B (4K sequence length, extendable to 32K during inference) and Yi-34B-200K (200K context length).
  2. Overview of Yi Models

    main

    The Yi series consists of next-generation open-source bilingual large language models trained from scratch by 01.AI. These models are trained on a 3T multilingual corpus and excel in language cognition, common sense reasoning, and reading comprehension.

    Key characteristics:

    • Bilingual Capability: Strong performance in both Chinese and English.
    • Architecture: Uses the Transformer architecture, specifically following the Llama architecture style for compatibility with existing tools and libraries, though they are not derivatives of Llama and do not use Llama weights.
    • Performance: Models like Yi-34B-Chat have demonstrated performance comparable to GPT-4 Turbo on benchmarks like AlpacaEval.
  3. Overview of Yi LLM series

    main

    The Yi series consists of next-generation, open-source, bilingual (English/Chinese) large language models developed by 01.AI. These models are trained from scratch on a 3T multilingual corpus and are designed for high performance in language understanding, commonsense reasoning, and reading comprehension.

    Key characteristics:

    • Architecture: Based on the Transformer structure and adopts the Llama model architecture, ensuring compatibility with the existing AI ecosystem tools.
    • Independence: While using the Llama architecture, Yi models are not derivatives of Llama; they use entirely independent training datasets, pipelines, and infrastructure.
    • Performance: Models like Yi-34B-Chat have demonstrated competitive performance against models like GPT-4 Turbo on benchmarks such as AlpacaEval.
  4. Install and build llama.cpp

    main

    To run Yi models using 4-bit integer quantization, you need to build the llama.cpp library. If you haven't built it yet, clone the repository and use make to compile it.

    git clone https://github.com/ggerganov/llama.cpp
    cd llama.cpp
    make
  5. Quantize models using AWQ via SWIFT

    main

    To perform AWQ quantization, use the swift export command. AWQ requires a dataset for the quantization process. You can specify multiple datasets (e.g., alpaca-zh, alpaca-en, sharegpt-gpt4:default).

    After quantization, you can perform inference using the swift infer command by pointing to the newly created quantized model path.

    # Quantize using AWQ
    CUDA_VISIBLE_DEVICES=0 swift export \
        --model_type yi-1_5-6b-chat --quant_bits 4 \
        --dataset alpaca-zh alpaca-en sharegpt-gpt4:default --quant_method awq
    
    # Infer using the quantized model
    CUDA_VISIBLE_DEVICES=0 swift infer \
        --model_type yi-1_5-6b-chat \
        --model_id_or_path yi-1_5-6b-chat-awq-int4
  6. Run inference with a fine-tuned model

    main

    Use the swift infer command to interact with your fine-tuned model.

    Arguments:

    • --ckpt_dir: The directory of the merged model checkpoint.
    • --eval_human: Set to true to enable interactive human-machine mode.
    • --stop_words: A string used to control output length.
    • --infer_backend: The inference engine to use (e.g., pt for PyTorch).
    swift infer \
        --ckpt_dir /content/yi-1-5-6b-chat/v0-20240717-024536/checkpoint-682-merged \
        --eval_human true \
        --stop_words "Observation:" \
        --infer_backend pt
  7. Deploy Yi locally with Docker

    main

    To run Yi-34B-Chat locally using Docker (requires NVIDIA A800 GPU or 4*4090), ensure docker and nvidia-container-toolkit are installed. Mount your model path to /models inside the container.

    docker run -it --gpus all \
    -v <your-model-path>: /models \
    ghcr.io/01-ai/yi:latest
  8. Quantize Yi models using GPT-Q

    main

    GPT-Q is a Post-Training Quantization (PTQ) method. Use the quant_autogptq.py script to perform quantization and eval_quantized_model.py to verify the results.

    # Perform quantization
    python quantization/gptq/quant_autogptq.py \
      --model /base_model \
      --output_dir /quantized_model \
      --bits 4 --group_size 128 --trust_remote_code
    
    # Evaluate the quantized model
    python quantization/gptq/eval_quantized_model.py \
      --model /quantized_model \
      --trust_remote_code
  9. Inference after fine-tuning

    main

    After fine-tuning, you can perform direct inference using the checkpoint directory. Alternatively, you can merge the LoRA weights into the base model using swift export with --merge_lora true and then run inference on the resulting merged checkpoint.

    # Direct inference
    CUDA_VISIBLE_DEVICES=0 swift infer \
        --ckpt_dir output/yi-vl-6b-chat/vx-xxx/checkpoint-xxx \
        --load_dataset_config true
    
    # Merge LoRA and then inference
    CUDA_VISIBLE_DEVICES=0 swift export \
        --ckpt_dir output/yi-vl-6b-chat/vx-xxx/checkpoint-xxx \
        --merge_lora true
    
    CUDA_VISIBLE_DEVICES=0 swift infer \
        --ckpt_dir output/yi-vl-6b-chat/vx-xxx/checkpoint-xxx-merged \
        --load_dataset_config true
  10. Install and run Yi models locally with LM Studio

    main

    LM Studio is a desktop application used to experiment with and deploy local open-source large language models (LLMs).

    To run Yi models locally:

    1. Download and install LM Studio from the official website based on your operating system.
    2. Open LM Studio and use the search bar to find models such as yi1.5-6b-chat.
    3. LM Studio will provide an assessment of whether your local hardware can run the selected model to prevent memory issues.
    4. Select the desired model and click "download" to begin using it locally.
  11. Create the Frontend for the Mind Map Generator

    main

    To build the user interface for the PDF upload and mind map application, create an index.html file inside a templates folder. The frontend requires Tailwind CSS for styling and D3.js for rendering the mind map.

    Key components to include:

    1. File Upload Section: A form with id="upload-form" and an input with name="file" that accepts .pdf files.
    2. Chat Interface: A container with id="chat-container" containing a message display area (id="chat-messages") and a form (id="chat-form") with a text input (id="user-input").
    3. Mind Map Display: A div with id="mind-map" where the D3.js visualization will be rendered.
    4. JavaScript Logic: Implement logic to handle fetch requests to /upload (for PDF processing) and /chat_stream (for streaming LLM responses). The /chat_stream endpoint expects a POST request with a JSON body: {"message": "your message"}. The client must parse Server-Sent Events (SSE) formatted as data: {JSON_STRING} to extract content or mind_map data.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Yi-large PDF to Mind Map</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <script src="https://d3js.org/d3.v7.min.js"></script>
    </head>
    <body class="bg-gray-100 min-h-screen">
        <div class="container mx-auto px-4 py-8">
            <h1 class="text-3xl font-bold mb-8 text-center">Yi-large PDF to Mind Map Converter</h1>
            
            <!-- File Upload Section -->
            <div id="file-upload" class="bg-white rounded-lg shadow-md p-4 mb-8">
                <h2 class="text-xl font-semibold mb-2">Upload PDF File</h2>
                <form id="upload-form" enctype="multipart/form-data">
                    <input type="file" id="file-input" name="file" accept=".pdf" class="mb-2">
                    <button type="submit" class="bg-green-500 text-white px-4 py-1 rounded">Upload and Generate Mind Map</button>
                </form>
                <div id="upload-status" class="mt-2"></div>
            </div>
    
            <!-- Chat Interface -->
            <div id="chat-container" class="bg-white rounded-lg shadow-md p-4 mb-8">
                <h2 class="text-xl font-semibold mb-2">Chat with Yi-large</h2>
                <div id="chat-messages" class="mb-4"></div>
                <form id="chat-form">
                    <input type="text" id="user-input" class="w-full p-2 border rounded" placeholder="Type your message...">
                    <button type="submit" class="mt-2 bg-blue-500 text-white px-4 py-1 rounded">Send</button>
                </form>
            </div>
    
            <!-- Mind Map Display Area -->
            <div id="mind-map" class="bg-white rounded-lg shadow-md p-4"></div>
        </div>
    </body>
    </html>
  12. Configure the OpenAI client for Yi's yi-large model

    main

    To use the yi-large model, configure the OpenAI client with the following settings:

    • api_key: Your actual API key.
    • base_url: https://api.01.ai/v1

    When making chat completion requests, use model="yi-large".

    from openai import OpenAI
    
    API_BASE = "https://api.01.ai/v1"
    API_KEY = "Your API Key"  # Replace with your actual API key
    client = OpenAI(
        api_key=API_KEY,
        base_url=API_BASE
    )
    
    # Example usage in a completion call
    completion = client.chat.completions.create(
        model="yi-large",
        messages=[...] 
    )