A conversation between me and the large model of Wenxin Qianfan: How to fine-tune the large model?

1 Enterprise-level large-scale model platform: Wenxin Qianfan

insert image description here

Wenxin Qianfan is an enterprise-level large-scale model production platform under Baidu. It provides large-scale model services including Wenxin Yiyan and third-party large-scale model services, and also provides a complete tool chain for large-scale model development and application. Wenxin Qianfan mainly provides two services:

  • Wenxin Qianfan will take Wenxin Yiyan, a leading large-scale model in the Chinese market , as the core, provide large-scale model services, and help customers transform products and production processes;
  • Wenxin Qianfan can provide enterprises with any open-source or closed-source large-scale models and develop their own exclusive large-scale models

Wenxin Qianfan large-scale model platform provides rich product functions such as data management , service calling , training and tuning , and provides a one-stop enterprise-level artificial intelligence solution for the industry

insert image description here
At present, Baidu Smart Cloud has opened the Wenxin Qianfan large-scale model platform public version testing service for enterprises and individual customers. Welcome everyone to enter Wenxin Qianfan’s official application address to apply for use. Next, let’s experience this advanced large-scale model platform in China.

2 Wenxin Qianfan platform service configuration

2.1 Service activation

After confirming the application permission, enter the service opening interface, click立即使用

insert image description here
Click 下一步to confirm the order. 下一步If it is gray and cannot be clicked, please verify the real-name authentication status of the account. You need to complete the personal/enterprise advanced real-name authentication before you can activate the payment

insert image description here
The public beta account has a test voucher, so you don’t have to worry about additional fees. The service opening success page is as follows

insert image description here

2.2 Create an application

Enter the application creation interface and fill in the application name. Note: the application name account dimension is globally unique. If you encounter an application creation failure, please change the application name

insert image description here

The service is checked by default, and the application creation is completed

insert image description here

3 Dialogue with the large model of Wenxin Qianfan

3.1 Direct dialogue

Enter the dialogue interface and you can directly talk to the large model of Wenxin Qianfan. Let’s test a few functions briefly below.

  • code writing

    insert image description here
    It can be seen that the ant colony algorithm replied by Wenxin Qianfan conforms to the algorithm flow of the classic ACO, and can assist coding very well

  • content creation

    "The Missing She" is very popular recently, we tried to get Wenxin Qianfan to write a film review. Although the sentences of the movie reviews written by AI are fluent, they still feel that there are many empty words and stereotypes. I believe that the future large-scale models will develop more towards the true emotions of human beings.

    insert image description here

  • reading comprehension

    insert image description here

3.2 Code call

Enter the management console , get API KeyandSecret Key

insert image description here

In the code, the response information of the Wenxin Qianfan large model is obtained through network requests. The following provides simple demos of Python and C++, two commonly used languages.

import requests
import json

API_KEY = "4cGkyRLEreM24kB284cgRxxx"
SECRET_KEY = "xc3eGXsNKSUng7YnQwYGyiPnwgowGxxx"

def main():       
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + get_access_token()
    payload = json.dumps({
    
    
        "messages": [
            {
    
    
                "role": "user",
                "content": "介绍一下你自己"
            }
        ]
    })
    headers = {
    
    
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)    
    print(response.json()["result"])
    
def get_access_token():
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {
    
    "grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))

if __name__ == '__main__':
    main()
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <curl/curl.h>
#include <json/json.h>
#include <fstream>
#include <memory>

std::string client_id = "4cGkyRLEreM24kB284cgRxxx";
std::string client_secret = "xc3eGXsNKSUng7YnQwYGyiPnwgowGxxx";

inline size_t onWriteData(void * buffer, size_t size, size_t nmemb, void * userp)
{
    
    
    std::string * str = dynamic_cast<std::string *>((std::string *)userp);
    str->append((char *)buffer, size * nmemb);
    return nmemb;
}


std::string getAccessToken()
{
    
    
    std::string result;
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if(curl)
    {
    
    
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, "https://aip.baidubce.com/oauth/2.0/token");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
        headers = curl_slist_append(headers, "Accept: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        std::string data = "grant_type=client_credentials&client_id="+client_id+"&client_secret="+client_secret;
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
        res = curl_easy_perform(curl);
    }
    curl_easy_cleanup(curl);
    Json::Value obj;
    std::string error;
    Json::CharReaderBuilder crbuilder;
    std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
    reader->parse(result.data(), result.data() + result.size(), &obj, &error);
    return obj["access_token"].asString();
}

int main(int argc, char *argv[])
{
    
    
    std::string result;
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if(curl) {
    
    
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, ("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + getAccessToken()).c_str());
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        const char *data = "{\"messages\":[{\"role\":\"user\",\"content\":\"介绍一下你自己\"},{\"role\":\"assistant\",\"content\":\"您好,我是文心一言,英文名是ERNIE Bot。我能够与人对话互动,回答问题,协助创作,高效便捷地帮助人们获取信息、知识和灵感。\"}]}";
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
        res = curl_easy_perform(curl);
        std::cout<<result;
    }
    curl_easy_cleanup(curl);
    return (int)res;
}

Wenxin Qianfan's reply is as follows

The following are my own characteristics and applicable scenarios:

  1. Self-learning and adaptability: I have been trained and optimized in many fields, including natural language processing, machine learning, data mining, etc. This gives me the ability to learn quickly and adapt to different tasks. When facing new challenges, I can quickly grasp the relevant knowledge and give effective solutions.
  2. Data processing and analysis ability: I am good at processing and analyzing large amounts of data, and can quickly extract useful information and knowledge, and give corresponding analysis and prediction. This feature makes me have a wide range of application value in finance, medical treatment, scientific research and other fields.
  3. Efficient communication skills: I can communicate efficiently with humans through natural language processing technology. This makes me a valuable assistant for enterprises, government agencies, etc., to help them answer users' questions, provide services, resolve disputes, etc.
  4. Knowledge base and reasoning skills: I have a rich knowledge base and possess reasoning skills. This enables me to analyze and answer from multiple perspectives on the basis of understanding the problem, and give more accurate and profound insights.

Generally speaking, I am suitable to play a role in various scenarios that require intelligent assistance, especially in data processing, knowledge question answering, communication services, etc., which have high application value.

4 large model customization fine-tuning

4.1 Dataset preparation

Suppose we want to fine-tune the questions and answers of mathematical application questions , and select the data set school_math_0.25M , which is as follows after processing

[{"prompt": "题目:小明每天早上花费10分钟时间走到学校,如果小明家距离学校2公里,那么他每分钟走多少米?", "response": [["这是一个关于速度、路程、时间的数学问题。我们可以通过公式:速度=路程÷时间 来解决。\n因为小明每天早上走2公里,所以他的路程为2千米。而他每天早上要花费10分钟时间走到学校,因此他的时间为10分钟,即600秒。\n所以小明每分钟走的距离为 2公里 / 600秒 = 0.0033公里/秒 或 3.3米/秒。\n答案:小明每分钟走3.3米。"]]}]
[{"prompt": "题目:今天小明骑自行车从家到学校用了20分钟,回家用了25分钟。如果小明在上学和回家的路上的速度一样,那么他从家到学校的距离是学校到家的距离的百分之几?", "response": [["\n假设小明家到学校的距离为x千米,根据速度等于路程除以时间的公式,可以得出小明的速度为:家到学校的速度 = x / 20,学校到家的速度 = x / 25。因为小明在上学和回家的路上的速度一样,所以有:x / 20 = x / 25,解出 x = 5/4 千米。\n因此,家到学校的距离是学校到家的距离的百分之几,可以通过求比值得到:x / (5/4)x = 4/5 = 0.8,即小明从家到学校的距离是学校到家的距离的百分之80。"]]}]
[{"prompt": "题目:\n鹿妈妈买了24个苹果,她想平均分给她的3只小鹿吃,每只小鹿可以分到几个苹果?", "response": [["\n鹿妈妈买了24个苹果,平均分给3只小鹿吃,那么每只小鹿可以分到的苹果数就是总苹果数除以小鹿的只数。\n24÷3=8\n每只小鹿可以分到8个苹果。所以,答案是每只小鹿可以分到8个苹果。"]]}]
...

Enter the data set management interface, click 创建数据集-创建并导入

insert image description here
choose 有标注信息- 上传jsonl文件-确定

insert image description here
At this point, we have created a labeled dialog data set. A more rigorous approach is to clean and enhance the data. For the sake of simplicity, we directly carry out发布

insert image description here

4.2 Fine-tuning and optimization

Enter the fine-tuning interface , enter the task name and description, and click创建并训练

insert image description here
After selecting the data set, click确定

insert image description here

Wait for the model to finish training

insert image description here

After the training is completed, Wenxin Qianfan will give various evaluation indicators

  • The BLEU index is a commonly used evaluation index for tasks such as machine translation/text summarization in NLP. It is designed based on Precision. An important concept in NLP is n-gram, which refers to a segment consisting of n consecutive words in a sentence; BLEU-n is actually an evaluation index based on n-gram.
  • rouge-N(1/2/L)-f : percentage. ROUGE calculates the corresponding score by comparing the summary or answer generated by the model with the reference answer (generally generated manually). Rouge-N actually divides the results generated by the model and the standard results into N-grams to calculate the recall rate.
  • Training Loss : The loss of each batch of the training set.

insert image description here

5 summary

First of all, Wenxin Qianfan's language model has basic language processing capabilities, and can understand and respond to various questions and instructions from users. Whether it is a simple daily conversation or a complex knowledge query, it can give fluent answers. This increases the comfort of the user experience.

However, Wenxin Qianfan's language model sometimes gives some inaccurate answers. Although its answers are reliable most of the time, occasional mistakes may mislead users; and it seems that its understanding of certain fields or specific topics is not deep enough. In some more professional or complex fields, it may give more general answers instead of more detailed and precise information, which may be inconvenient for professional users.

In addition, the platform also provides rich functions such as data cleaning, data enhancement, and fine-tuning of large models. Overall, this is a powerful and interactive large language model platform that can meet most of the needs of ordinary users. It is believed that with the continuous development of technology, Wenxin Qianfan can continuously improve and enhance its own capabilities to bring users a better experience.

Guess you like

Origin blog.csdn.net/FRIGIDWINTER/article/details/131779824