29.04.2024 • 7 min read

Comprehensive Training and Fine-Tuning of the Chinese LLama3

Cover Image

Introduction to Llama3

Meta’s recent release of Llama3 has generated significant attention within the open-source community, quickly establishing itself as a top-tier model with performance rivaling proprietary systems like GPT-4. Currently, Meta has released models in 8B and 70B parameter sizes, with a much larger 400B model still in training. Thanks to substantial improvements in both pre-training and post-training methodologies, the released instruct-tuned models represent the state-of-the-art for their respective scales. These enhancements have also led to a lower error rate, improved response consistency, and greater diversity in model outputs.

One of Llama3’s most impressive features is its vast knowledge base, built upon a pre-training dataset of over 15 trillion tokens from publicly available sources. This dataset is more than seven times larger than the one used for Llama 2 and includes four times the amount of code, endowing the model with a powerful and comprehensive understanding of a wide range of topics.

The Challenge: Achieving Full-Fledged Chinese Language Support

Despite its formidable capabilities, Llama3 exhibits a notable weakness in its native support for the Chinese language. While it can often comprehend questions posed in Chinese, its responses tend to default to English. Even when explicitly prompted to reply in Chinese, the output can be unstable, sometimes resulting in a mix of Chinese and English.

A common approach to address this is to fine-tune the model on a Chinese corpus. However, fine-tuning carries the risk of “catastrophic forgetting,” potentially degrading the model’s original performance and broad knowledge base. This raises a critical question: is it possible to retain Llama3’s native power while achieving a seamless Chinese user experience?

The answer is yes. The solution lies not in altering the model itself, but in strategically processing its inputs and outputs. By creating a sequential chain where Llama3’s English output is immediately translated into Chinese by another powerful language model, we can achieve our goal. This “post-processing translation” method leverages the contextual awareness inherent in Transformer architectures, ensuring high-quality, context-aware translations.

This report will demonstrate how to implement this “translation-chain” solution using two distinct methods: a low-code visual approach and a more customizable code-based approach using the LangChain framework.


Method 1: The Low-Code Visual Workflow Approach

For rapid prototyping and development, low-code AI application builders (such as Dify, Flowise, LangFlow, etc.) are an excellent choice. These platforms allow you to construct complex AI logic by connecting functional nodes in a visual interface. Here’s a generalized guide to building our solution on such a platform.

Step-by-Step Guide:

  1. Initialize a New Project: In your chosen visual workflow builder, create a new project or “flow.”

  2. Select a Template (Optional): Many platforms offer pre-built templates. A “multi-prompt” or “sequential chain” template is a great starting point for this task.

  3. Construct the Primary LLM Chain (Llama3):

    • Create the first logical block. This typically consists of a Prompt Template node and an LLM node.
    • Configure the LLM node to use an accessible Llama3 model (e.g., llama3-70b-instruct via an API).
    • In the Prompt Template node, define the instructions for Llama3. The prompt should be in English to maximize the model’s native performance. Ensure it includes a placeholder for user input, commonly denoted as {input}.
    You are a helpful assistant. Answer all questions to the best of your ability.
    {input}
  4. Construct the Translation Chain:

    • Create a second logical block, also consisting of a Prompt Template and an LLM node.
    • Configure this LLM node to use a model with strong translation capabilities. This could be another large language model known for multilingual performance (e.g., models from the GPT or Claude series, or open-source alternatives like Qwen).
    • Design a Prompt Template specifically for translation. This prompt will take the output from the Llama3 chain as its primary input, typically denoted as {text}. Providing context from the original user query ({input}) can further improve translation quality.
    Context: The user originally asked "{input}".
    Task: Please translate the following English response into fluent, natural-sounding Chinese.
    Response to translate: {text}
  5. Connect the Chains:

    • Visually connect the output of the first (Llama3) chain to the input of the second (translation) chain. Specifically, the response from the Llama3 LLM node should feed into the {text} variable of the translation prompt.
  6. Test and Deploy:

    • Use the platform’s testing interface to send a Chinese question. For example: “老人与海这部小说中,最著名的一句话是什么?”
    • Observe the flow: the first node should generate an English answer, and the second node should translate it into a polished Chinese response.
    • Once you’re satisfied with the results, you can deploy the workflow as an API endpoint for use in your applications.

This low-code method provides a fast and intuitive way to build a high-performance, Chinese-enabled Llama3 application without writing a single line of code.


Method 2: The Code-Based Approach with LangChain

For developers who require greater control and customization, the LangChain framework is the ideal tool. The following tutorial demonstrates how to build the same translation chain in a standard Python environment (e.g., a Jupyter Notebook or a local IDE).

Step-by-Step Guide:

  1. Environment Setup:

    • First, ensure you have Python installed. Then, install the necessary libraries. We’ll use the langchain and langchain-openai packages as a common example, but you can substitute langchain-openai with any provider that offers Llama3 and a translation model.
    pip install langchain langchain-openai
  2. Import Modules and Configure API Keys:

    • Import the required classes from LangChain and set up your API keys as environment variables.
    import os
    from langchain_openai import ChatOpenAI
    from langchain.chains import LLMChain, SequentialChain
    from langchain.prompts import PromptTemplate
    from langchain.memory import ConversationBufferMemory
    
    # It's best practice to set keys as environment variables
    # os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY" 
    # If you use a provider for Llama3, set its key as well.
  3. Instantiate the LLM for the Primary Task (Llama3):

    • Create an instance of the LLM that will run Llama3. Here, we assume access to Llama3 through an OpenAI-compatible API endpoint.
    # This is a hypothetical example. Replace with your actual Llama3 provider.
    llama3_llm = ChatOpenAI(
        # api_key="YOUR_LLAMA3_PROVIDER_KEY",
        # base_url="https://api.your-llama3-provider.com/v1",
        model_name='llama-3-70b-instruct',
        temperature=0.7
    )
  4. Create the Primary Chain:

    • Define the prompt template and create an LLMChain for the initial response generation.
    primary_template = """You are a helpful assistant. You always respond in English.
    History: {history}
    Human: {input}
    Assistant:"""
    
    primary_prompt = PromptTemplate(
        input_variables=["history", "input"], 
        template=primary_template
    )
    
    primary_chain = LLMChain(
        llm=llama3_llm,
        prompt=primary_prompt,
        verbose=True,
        output_key="english_response", # Name the output variable
        memory=ConversationBufferMemory(input_key="input", memory_key="history")
    )
  5. Instantiate the LLM for Translation:

    • Create a second LLM instance for the translation task. A model like GPT-4 or a specialized translation model is a great choice.
    translation_llm = ChatOpenAI(
        model_name='gpt-4-turbo',
        temperature=0.2
    )
  6. Create the Translation Chain:

    • Define the prompt and chain for translating the English response.
    translation_template = """You are an expert translator. Translate the following English text into Chinese.
    Maintain the original tone and meaning. Only output the translated content.
    
    English Text: {english_response}
    Chinese Translation:"""
    
    translation_prompt = PromptTemplate(
        input_variables=["english_response"],
        template=translation_template
    )
    
    translation_chain = LLMChain(
        llm=translation_llm,
        prompt=translation_prompt,
        verbose=True,
        output_key="chinese_response" # Name the final output
    )
  7. Combine the Chains into a SequentialChain:

    • Use LangChain’s SequentialChain to link the two chains together. The output of primary_chain (english_response) will automatically become the input for translation_chain.
    # The full conversational chain
    full_chain = SequentialChain(
        chains=[primary_chain, translation_chain],
        input_variables=["input"],
        output_variables=["english_response", "chinese_response"],
        verbose=True
    )
  8. Test the Implementation:

    • Now, you can run the chain with a Chinese query and observe the final translated output.
    # First question
    query1 = "老人与海这部小说中,最著名的一句话是什么?"
    result1 = full_chain.invoke({"input": query1})
    print("Final Chinese Response:", result1['chinese_response'])
    
    # Follow-up question
    query2 = "这句话表达了作者怎样的思想?"
    result2 = full_chain.invoke({"input": query2})
    print("Final Chinese Response:", result2['chinese_response'])

This code-based approach offers maximum flexibility for adding custom logic, managing memory, and integrating the solution into larger, more complex applications.


Conclusion

The “translation-chain” methodology presents an elegant and effective solution for leveraging the power of state-of-the-art models like Llama3 in non-native languages. By separating the core logic (handled by Llama3 in English) from the localization (handled by a dedicated translation step), we can preserve the model’s full performance and knowledge base while delivering a flawless user experience in Chinese.

Whether you prefer the speed and simplicity of a low-code platform or the power and flexibility of a framework like LangChain, this approach is a valuable technique in any developer’s toolkit. It demonstrates a powerful principle in applied AI: sometimes, the most effective solution is not to modify a model, but to architect a smarter workflow around it. We encourage you to experiment with this method for other models and languages to unlock their full potential across linguistic barriers.