FastChat

repository·main·Indexed 12 days ago

https://github.com/lm-sys/fastchat

An open platform for training, serving, and evaluating large language model (LLM) based chatbots. It features a distributed multi-model serving system with OpenAI-compatible APIs, a Web UI, and the LLM Judge for model evaluation via MT-bench. Version 0.2.36.

Tokens
24.4K
Snippets
101
Records
116
Agent score
98%

What's inside FastChat

  1. Understand the classification test behavior

    main

    When running test_classification.py, the script performs a comparative evaluation of different embedding models. It trains classifiers based on the following models and reports their respective accuracies:

    • vicuna-7b (Local model)
    • text-similarity-ada-001 (OpenAI model)
    • text-embedding-ada-002 (OpenAI model)
  2. Use FastChat as an OpenAI-compatible API

    main
    FastChat provides RESTful APIs that are compatible with the OpenAI API specification. This allows you to use FastChat as a drop-in replacement for OpenAI in your applications. It is compatible with the openai-python library and standard cURL commands.
  3. Purpose of the fastchat Nginx Gateway

    main

    The Nginx gateway acts as a reverse proxy and security layer for Gradio servers. Its primary functions include:

    • Security: Protects Gradio servers by acting as a firewall and reducing the attack surface by requiring only a single public port to be exposed.
    • Management: Facilitates dynamic mounting/unmounting of Gradio servers and provides load balancing.
    • Traffic Control: Offers additional security features like total connection limits.
  4. Prompt templates for Vicuna weights

    main

    The prompt template format depends on the version of the weights you are using. Using the wrong template will degrade model performance.

    For weights v1.1, v1.3, and v1.5

    These versions use USER: and ASSISTANT: identifiers and the </s separator.

    For weights v0

    This version uses ### Human: and ### Assistant: identifiers.

    Note for v1.1+: The separator was changed from ### to the EOS token </s to improve generation stop criteria and library compatibility.

    ### Example prompt (weights v1.1, v1.3, v1.5)

    A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.

    USER: Hello! ASSISTANT: Hello!</s> USER: How are you? ASSISTANT: I am good.</s>

    
    ### Example prompt (weights v0)

    A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.

    Human: Hello!

    Assistant: Hello!

    Human: How are you?

    Assistant: I am good.

  5. Use different MT-bench grading modes

    main

    MT-bench supports three grading modes via the --mode flag in gen_judgment.py:

    1. Single-answer grading (Default): GPT-4 grades each answer directly on a scale of 10.
    2. pairwise-baseline: Compares models against a baseline model (defaults to gpt-3.5-turbo). Results are saved to data/mt_bench/model_judgment/gpt-4_pair.jsonl.
    3. pairwise-all: Performs pairwise comparisons between all possible pairs of models in the --model-list.
    # Example: Pairwise comparison against baseline
    python gen_judgment.py --mode pairwise-baseline --model-list vicuna-13b-v1.3 alpaca-13b --parallel 2
    python show_result.py --mode pairwise-baseline
    
    # Example: Pairwise comparison between all pairs
    python gen_judgment.py --mode pairwise-all --model-list vicuna-13b-v1.3 alpaca-13b --parallel 2
    python show_result.py --mode pairwise-all
  6. Review pre-generated MT-bench answers and judgments

    main

    FastChat provides pre-generated model answers and judgments for certain models. You can download them using download_mt_bench_pregenerated.py and view them locally using the qa_browser.py tool.

    # Download pre-generated data
    python3 download_mt_bench_pregenerated.py
    
    # View data locally
    python3 qa_browser.py --share
  7. Install AWQ for 4-bit inference

    main

    To enable efficient 4-bit LLM inference in FastChat, you must install the llm-awq package and its CUDA kernels. Follow these steps to set up a dedicated environment:

    1. Create and activate a new Conda environment.
    2. Install FastChat in editable mode.
    3. Clone the llm-awq repository and install it.
    4. Compile and install the AWQ CUDA kernels.

    Note: Ensure you have the necessary build tools and CUDA environment configured as per the llm-awq requirements.

    conda create -n fastchat-awq python=3.10 -y
    conda activate fastchat-awq
    # cd /path/to/FastChat
    pip install --upgrade pip
    pip install -e .
    
    git clone https://github.com/mit-han-lab/llm-awq repositories/llm-awq
    cd repositories/llm-awq
    pip install -e .
    
    cd awq/kernels
    python setup.py install
  8. Install requirements for data cleaning

    main

    To perform data cleaning tasks such as HTML conversion, language filtering, and conversation splitting, you must install the following Python dependencies:

    For HTML to Markdown conversion:

    • bs4
    • markdownify

    For language detection and filtering:

    • polyglot
    • pyicu
    • pycld2
    pip3 install bs4 markdownify
    pip3 install polyglot pyicu pycld2
  9. Launch Chatbot Arena locally

    main

    You can run a local instance of the Chatbot Arena (side-by-side battle UI) by configuring API-based models (like OpenAI, Anthropic, or Gemini) in a JSON file and launching the Gradio multi-server.

    1. Create an api_endpoint.json file defining your models. For OpenAI, use "api_type": "openai". For Anthropic, use "api_type": "anthropic_message". For Gemini, use "api_type": "gemini".
    2. Launch the server using the gradio_web_server_multi module pointing to your config file.
    {
        "gpt-4o-2024-05-13": {
            "model_name": "gpt-4o-2024-05-13",
            "api_base": "https://api.openai.com/v1",
            "api_type": "openai",
            "api_key": "[Insert API Key]",
            "anony_only": false
        }
    }
    python3 -m fastchat.serve.gradio_web_server_multi --register-api-endpoint-file api_endpoint.json
  10. Fine-tune using (Q)LoRA

    main

    You can perform parameter-efficient fine-tuning using LoRA or QLoRA.

    Requirements for QLoRA:

    • bitsandbytes >= 0.39.0
    • transformers >= 4.30.0

    Compatibility Notes:

    • QLoRA: Use DeepSpeed ZeRO2 (playground/deepspeed_config_s2.json). ZeRO3 is not currently supported with QLoRA.
    • LoRA: ZeRO3 is supported. A reference configuration is available at playground/deepspeed_config_s3.json.

    Use fastchat/train/train_lora.py for Llama-based models (like Vicuna-7B) and fastchat/train/train_lora_t5.py for T5-based models.

    # Example: Fine-tuning Vicuna-7B with QLoRA
    deepspeed fastchat/train/train_lora.py \
        --model_name_or_path ~/model_weights/llama-7b \
        --lora_r 8 \
        --lora_alpha 16 \
        --lora_dropout 0.05 \
        --data_path ./data/dummy_conversation.json \
        --bf16 True \
        --output_dir ./checkpoints \
        --num_train_epochs 3 \
        --per_device_train_batch_size 1 \
        --per_device_eval_batch_size 1 \
        --gradient_accumulation_steps 1 \
        --evaluation_strategy "no" \
        --save_strategy "steps" \
        --save_steps 1200 \
        --save_total_limit 100 \
        --learning_rate 2e-5 \
        --weight_decay 0. \
        --warmup_ratio 0.03 \
        --lr_scheduler_type "cosine" \
        --logging_steps 1 \
        --tf32 True \
        --model_max_length 2048 \
        --q_lora True \
        --deepspeed playground/deepspeed_config_s2.json
  11. Run embedding-based machine learning tests

    main

    The playground/test_embedding directory contains scripts for evaluating various machine learning tasks using text embeddings. You can use these scripts to:

    • Evaluate text similarity: See test_sentence_similarity.py.
    • Build a classifier: See test_classification.py.
    • Perform semantic search: See test_semantic_search.py.

    Prerequisites

    1. Dataset: Download the Amazon Fine Food Reviews dataset from Kaggle.
    2. OpenAI API Key: Required for comparing results against OpenAI models.

    Execution

    To run the classification test, navigate to the directory and execute the script using Python 3:

    cd playground/test_embedding
    python3 test_classification.py