The intelligent question answering system based on the LLaMA fine-tuning model based on Chinese financial knowledge: LLaMA large model training fine-tuning reasoning and other detailed teaching

insert image description here

Project design collection (artificial intelligence direction): Help newcomers to quickly master skills in actual combat, complete project design upgrades independently, and improve their own hard power (not limited to NLP, knowledge graph, computer vision, etc.): Collect meaningful project design collections to help Newcomers quickly master skills in actual combat, helping users make better use of the CSDN platform, independently complete project design upgrades, and improve their own hard power.

insert image description here

  1. Column Subscription: Encyclopedia of Projects to Improve Your Hard Power

  2. [Detailed introduction of the column: project design collection (artificial intelligence direction): help newcomers quickly master skills in actual combat, complete project design upgrades independently, and improve their own hard power (not limited to NLP, knowledge graphs, computer vision, etc.)

The intelligent question answering system based on the LLaMA fine-tuning model based on Chinese financial knowledge: LLaMA large model training fine-tuning reasoning and other detailed teaching

A fine-tuning model based on the LLaMA-based model after Chinese financial knowledge instruction fine-tuning/instruct-tuning. The instruction data set is constructed through Chinese financial public question and answer data + crawled financial question and answer data, and on this basis, the instruction fine-tuning of the LLaMA model is carried out, which improves the effect of LLaMA in the financial field of question and answer.

Based on the existing data and the Chinese financial data that continues to be crawled, we will continue to use the GPT3.5/4.0 API to build high-quality data sets, and further expand high-quality instruction data on Chinese knowledge graph-finance , CFLEB financial data sets and other data set.

  • A model for instruction fine-tuning based on Chinese-LLaMA and Chinese financial data.

  • A model for instruction fine-tuning based on Meta-LLaMA and Chinese financial data.

1. Environment installation

First install the dependency package, the python environment recommends 3.9+


pip install -r requirements.txt

Secondly, install lfs to facilitate local download of LLaMa large model


git lfs install

# 下载7B模型到本地
bash ./base_models/load.sh

2. Model download

LoRA weights can be downloaded through Huggingface, the structure is as follows:

    Fin-Alpaca-LoRA-7B-Meta/
       - adapter_config.json   # LoRA权重配置文件
       - adapter_model.bin     # LoRA权重文件
LoRA model download Classification Refactor the model training data training sequence length Version
Fin-Alpaca-LoRA-7B-Meta Chinese financial question answering fine-tuning model decapoda-research/llama-7b-hf 12M instruction data 512 V1.0
Fin-Alpaca-LoRA-7B-Linly Chinese financial question answering fine-tuning model Linly-AI/Chinese-LLaMA-7B 14M instruction data 512 V1.1

3.Inference: single model reasoning - multi-model comparison

At present, ./instruction_data/infer.jsonsome test cases are provided in , which can also be replaced with other data sets, but pay attention to the consistent format

Run the infer script


#单模型推理
bash ./scripts/infer.sh

#多模型对比
bash ./scripts/comparison_test.sh

4. Dataset construction

The previous version used open and crawled Chinese financial Q&A data , involving insurance, wealth management, stocks, funds, loans, credit cards, social security, etc.

An example of command data is as follows:


问题:办理商业汇票应遵守哪些原则和规定?

回答: 办理商业汇票应遵守下列原则和规定:1.使用商业汇票的单位,必须是在银行开立帐户的法人;2.商业汇票在同城和异地均可使用;3.签发商业汇票必须以合法的商品交易为基础;4.经承兑的商业汇票,可向银行贴现;5.商业汇票一律记名,允许背书转让;6.商业汇票的付款期限由交易双方商定,最长不得超过6个月;7.商业汇票经承兑后,承兑人即付款人负有到期无条件交付票款的责任;8.商业汇票由银行印制和发售。

In view of the imperfections such as inaccuracy and single types of previous data, we are currently using the GPT3.5/4.0 interface to further optimize the data, expand the Chinese financial knowledge base, and set up a variety of prompt forms and multi-task forms to expand and enrich instruction data Set to realize the coverage of multiple business scenarios in the financial field.

The latest model status: (will be released soon, so stay tuned~)

5. Fine tune Finetune

If you want to fine-tune LLaMA with your own dataset, please ./instruction_data/fin_data.jsonbuild your own dataset in the format of

Run the finetune script


bash ./scripts/finetune.sh

6. Training Details

6.1 Computing resource requirements

The current training equipment is an A100-SXM-80GB graphics card, and the total number of training rounds is 10. In the case of batch_size=64, the video memory usage is about 40G, and in the case of batch_size=96, the video memory usage is about 65G. It is expected that graphics cards above 3090/4090 (24GB video memory) can better support it, and adjust the batch_size according to the size of the video memory.

6.2 Experimental records

6.3 Comparison of model effects

test input Raw Llama output Cornucopia (Fin-Alpaca-LoRA-7B-Meta) export Cornucopia (Fin-Alpaca-LoRA-7B-Linly) output Literary output Xunfei Xinghuo Cognitive Output
Is it better for the elderly to manage money or save regularly? The financial management methods of the elderly should be suitable for their own situation. If he/she has enough time and investment ability, it is recommended that he/she use the current time and investment opportunities to conduct financial activities. Because the elderly can obtain balance, insurance, real estate and other income methods, he/she can choose different types of financial management projects, including open market, non-public market, funds, trust plans, etc. Of course, the elderly should also pay attention to the financial security of life that they actually need. If the investor's risk tolerance is relatively high, then he can choose wealth management products for investment; if the investor's risk tolerance is relatively low, he can choose time deposit for investment. At present, wealth management products and time deposits are capital-guaranteed products, which will not lose the principal, and the income is determined by the investment target, so wealth management products are more stable than time deposits. It should be noted that wealth management products and time deposits do not have the same current interest rate, and the income of wealth management products is calculated daily, so the higher the income of wealth management products, the higher the probability of investors getting income. 9QDII funds refer to open-end foreign funds in the Chinese market. These funds mainly invest in: A-shares, Hong Kong stocks, US stocks, British stocks, German stocks, Japanese stocks, French stocks, Taiwan stocks, Hong Kong stocks, Singapore stocks, etc.; QFII funds Refers to the open-end foreign funds of the Chinese exchanges. This type of fund mainly invests in: A-shares, Hong Kong stocks, US stocks, British stocks, German stocks, Japanese stocks, French stocks, Taiwan stocks, Hong Kong stocks, Singapore stocks, etc. For the elderly with a relatively low risk tolerance, it is better to choose a time deposit, because its risk is relatively low and the income is relatively stable; for the elderly with a relatively high risk tolerance, it is better to choose a financial product, because The risk is relatively high, and the return is relatively high. It should be noted that the current wealth management products are not capital-guaranteed products, and there is a possibility of loss of principal, and they are divided into different risk levels according to the investment target. If the investor's risk tolerance is relatively low, then you can choose wealth management products with risk level R2 and below , if investors have a relatively high risk tolerance, they can choose wealth management products with a risk level of R3 and above. Current wealth management products include: bond funds, stock funds, hybrid funds, index funds and feeder funds, etc. Investors can choose suitable products for investment according to their actual conditions. It is better for the elderly to save regularly. The elderly have no income from work, and their total assets are gradually decreasing. Moreover, the elderly are prone to illness, and their medical expenses are relatively large. If the elderly invest in wealth management products, along with changes in the net value of their products, losses may occur. As for time deposits, the interest rate is generally fixed, and the principal and interest will be repaid upon maturity. Relatively speaking, the safety factor is high and the profit is more sustainable. As an AI language model, I have no personal opinions and suggestions. However, there are advantages and disadvantages to financial management and regular deposits for the elderly. The advantage of financial management for the elderly is that they can better plan their own financial situation, including expenditures on pensions, medical insurance, and children's education. In addition, the elderly can increase their wealth by investing in financial products such as stocks and funds. The advantage of fixed-term deposit is that the risk is small and the income is relatively stable. The elderly can choose time deposits of different terms, according to their own needs and risk tolerance. On the whole, the elderly should choose a financial management method that suits them according to their actual situation and needs. If the elderly have sufficient funds and risk tolerance, they can consider investing in financial products such as stocks and funds to increase their wealth. If the elderly pay more attention to stability and safety, they can choose to save regularly to protect their financial status.
Can stocks and funds be bought and sold on the same day? Yes, stocks and funds can be bought and sold on the same day. No, stocks and funds implement T+1 trading. Buying on the trading day will be calculated based on the net value at the close of the buying day, and the share will be confirmed on the second trading day. After the share is confirmed, the income will be calculated, so buying on the trading day will have to wait until Earnings can only be obtained on the second trading day. For example, if an investor buys a certain stock on Monday, and the net value announced on the market on Tuesday is 10 yuan, then the investor can calculate the return based on these data: 1000×10=10000; 10000/10=1000. If the investor holds the stock for more than 30 working days, it can be directly converted into cash, that is, all or part of the stock can be withdrawn in advance. It should be noted that there are currently no laws and regulations restricting investors from buying funds and stocks on trading days, so investors who buy funds and stocks on trading days can conduct open transactions on the same day. At the same time, after investors buy stocks on a trading day, their stocks will be automatically converted into cash, and after investors buy stocks on a trading day, their stocks will be automatically converted into a price limit (about 20%), which affects investment The transaction fee and income of the buyer. No, stock trading time: Monday to Friday 9:30-11:30 am, 13:00-15:00 pm, no trading on legal holidays. Fund trading hours: Monday to Friday 9:30-11:30 am, 13:00-15:00 pm, no trading on legal holidays. Stocks implement T+1 trading, which is traded according to the real-time market price. Following the principle of price priority and time priority, the price limit for stocks in Shanghai and Shenzhen is 10%, and the price limit for stocks on the Growth Enterprise Market and the Science and Technology Innovation Board is 20%. , the price limit of the North Exchange is 30%, the price limit of ST and *ST is 5% (the growth rate of the Growth Enterprise Market and the Science and Technology Innovation Board does not have this limit), the price limit of Hong Kong stocks is 20%, and the new third board The rise and fall is limited to 10% (there is no such limit on the rise and fall of the Growth Enterprise Market and the Science and Technology Innovation Board). For investors, stock trading is more flexible and can be traded according to their own circumstances, and stock transaction fees are relatively low. Funds The transaction fee is also relatively low, and at the same time, the stock volatility is relatively large, and the risk is relatively large. No, stock funds implement T+1 trading. Buying on the trading day (trading time) is calculated based on the net value at the close of the buying day, and the share is confirmed on the second trading day. The fund can only be sold after the share is confirmed. General trading Buy on the first trading day, sell on the third trading day, and sell at any time on the third trading day. The way stocks and funds are traded varies with different securities markets. In some securities markets, stocks and funds can be bought and sold on the same day, which is the so-called "T+0" transaction. However, in other securities markets, stocks and funds cannot be bought and sold on the same day, and they need to wait for a certain trading time before they can be bought or sold. In China's A-share market, stocks and funds cannot be bought and sold on the same day, and T+1 or T+2 transactions are required. This means that if you buy a stock or fund today, you won't be able to sell it until tomorrow. In some other countries, stocks and funds may be traded differently. Therefore, if you want to invest in stocks or funds, it is recommended that you understand the local trading rules and market conditions.

7. Prompt word template

This directory contains template styles for cues for LoRA fine-tuning LLaMa models.

  • Format

Templates are described via a JSON file containing the following keys:

  • prompt_input: The template to use when input is not None. Uses {instruction} and {input} placeholders.
  • prompt_no_input: The template to use when input is None. Uses {instruction} placeholders.
  • description: A short description of the template, with possible use cases.
  • response_split: The text to use as separator when cutting real response from the model output.

No {response} placeholder was used, since the response is always the last element of the template and is just to be concatenated to the rest.

7.1 Template case

The default template, used unless otherwise specified, is alpaca.json

{
    
    
    "description": "Template used by Alpaca-LoRA.",
    "prompt_input": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n",
    "prompt_no_input": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:\n",
    "response_split": "### Response:"    
}

7.2 Existing Templates

7.2.1 alpaca

Default template for generic LoRA fine-tuning so far.

7.2.2 alpaca_legacy

The old template used by the original alpaca, without "\n" after the response field. Reserved for reference and experimentation.

7.2.3 alpaca_short

A trimmed alpaca template that also seems to behave well and preserves some tokens. Models created using the default template also seem to pass short queries.

8. Prompt building blocks

  • prompter.py

Prompter class, a template manager.

from utils.prompter import Prompter
  • This project refers to the following open source projects,

    • Facebook Llama: https://github.com/facebookresearch/llama
    • Stanford Alpaca: https://github.com/tatsu-lab/stanford_alpaca
    • alpaca-lora by @tloen: https://github.com/tloen/alpaca-lora
    • Huatuo-Llama-Med-Chinese: https://github.com/SCIR-HI/Hatuo-Llama-Med-Chinese
    • Wenxin Yiyan https://yiyan.baidu.com/welcome The logo of this project is automatically generated by Wenxin Yiyan
    • Xunfei Spark Cognition https://xinghuo.xfyun.cn/desk

See the top or end of the article for the project code source and information

https://download.csdn.net/download/sinat_39620217/88009376

Guess you like

Origin blog.csdn.net/sinat_39620217/article/details/132007040