上篇分享了一個(gè)法律的大模型,lawGPt,目前看回答一些基本的法律問題還是可以的,昨天又發(fā)現(xiàn),北京大學(xué)也開源了一個(gè)法律大模型,lawyer-llama,通過在大規(guī)模法律語(yǔ)料中進(jìn)行訓(xùn)練,系統(tǒng)學(xué)習(xí)中國(guó)的法律知識(shí)體系使得模型可以掌握中國(guó)的法律知識(shí)并應(yīng)用于中國(guó)的法律實(shí)務(wù)。
看看論文的例子
對(duì)比上圖左側(cè)的BELLE(Be Everyone's Large Language model Engine)模型,如果提問“中國(guó)的法定結(jié)婚年齡”,可以看到 Lawyer LLaMA 給出了一個(gè)正確的,并且更像是 Lawyer 的回答。并且,即使是提供了必要的法律條文,如上圖問題B,BELLE 也無(wú)法給出一個(gè)正確的回答,而 Lawyer LLaMA 則有理有據(jù)的頗具專業(yè)性的很好的回答了這個(gè)問題。
其實(shí)從 BELLE 的回答中也可以看出,直接將這樣一個(gè)大模型套在專業(yè)的垂直領(lǐng)域下往往會(huì)出現(xiàn)許多問題,作者團(tuán)隊(duì)認(rèn)為,要使得大模型可以很好地適應(yīng)法律領(lǐng)域的特殊要求,必須要滿足以下三個(gè)條件,分別是:
-
精確的表意,避免歧義:在法律領(lǐng)域中常常會(huì)有僅僅更換一個(gè)字詞,就會(huì)導(dǎo)致法律關(guān)系構(gòu)建出截然相反的結(jié)果,譬如中文中定金與訂金僅相差一個(gè)字,但是其含義與法律效力在合同法中卻完全不同;
-
理解與區(qū)分法律術(shù)語(yǔ):在法律中,有許多特有的特定詞匯,許多術(shù)語(yǔ)僅僅出現(xiàn)在法律領(lǐng)域中,如法人這個(gè)概念,而還有更多術(shù)語(yǔ)可能在法律領(lǐng)域與日常生活領(lǐng)域擁有著不盡相同的含義,也需要模型加以區(qū)分;
-
能夠理解實(shí)際情況:除了對(duì)法律術(shù)語(yǔ)與法律分析要有基本的了解與系統(tǒng)的掌握以外,模型還應(yīng)當(dāng)具有精確理解現(xiàn)實(shí)生活問題的能力,即模型需要擁有一個(gè)應(yīng)用法律理論來解決特定問題的核心能力。
基于上述理論,作者團(tuán)隊(duì)便基于開源的 LLaMA 模型期望通過以下幾步解決法律領(lǐng)域大模型的適用問題:
-
法律相關(guān)知識(shí)注入:通過收集大量法律領(lǐng)域的原始文本,如法律條文、司法解釋與國(guó)家法律文件,對(duì)原始模型使用新數(shù)據(jù)進(jìn)行繼續(xù)訓(xùn)練;
-
特定領(lǐng)域技能習(xí)得:一個(gè)良好的法律大模型應(yīng)該能夠解決法律領(lǐng)域的常見問題,如概念解釋、案例分析與法律咨詢,因此作者收集了一組實(shí)際的任務(wù)案例,使用 ChatGPT 生成相應(yīng)答案從而進(jìn)行監(jiān)督微調(diào),以使得模型具有解決法律領(lǐng)域特定任務(wù)的能力;
-
信息檢索減輕幻覺:為了減輕大模型的機(jī)器幻覺問題,作者同時(shí)引入了一個(gè)信息檢索模塊,在生成每個(gè)回復(fù)之前,都首先利用用戶的查詢與上下文檢索相關(guān)法律條文,基于這些法律條文再去生成相應(yīng)回復(fù)。
通過以上三步,作者團(tuán)隊(duì)便成功完成了 Lawyer LLaMA 的構(gòu)建,Lawyer LLaMA 的整體運(yùn)作流程如下圖所示:
我們直接看看論文給出的效果吧
同等對(duì)比,在各個(gè)不同?角度看,lawyer-llama確實(shí)要好很多
論文中還有具體的對(duì)比數(shù)據(jù),感興趣可以看看
https://arxiv.org/pdf/2305.15062.pdf
合并使用流程
????到了實(shí)操階段了,哈哈
????首先是獲取權(quán)重,分兩部分,(雖然官方git寫了三步)
????????1、下載7B中的?consolidated.00.pth
https://huggingface.co/nyanko7/LLaMA-7B/tree/main
????????2、下載lawyer-llama權(quán)重
https://huggingface.co/pkupie/lawyer-llama-13b-beta1.0/tree/main
合并使用官方給的decrypt.py腳本
for f in "/path/to/model/pytorch_model"*".enc"; \
do if [ -f "$f" ]; then \
python3 decrypt.py "$f" "/path/to_original_llama/7B/consolidated.00.pth" "/path/to/model"; \
fi; \
done
腳本在此
import os
import sys
import hashlib
import multiprocessing
import os
def xor_bytes(data, key):
return bytes(a ^ b for a, b in zip(data, (key * (len(data) // len(key) + 1))[:len(data)]))
def xor_worker(task_queue, result_queue):
while True:
chunk_idx, data, key = task_queue.get()
result_queue.put((chunk_idx, xor_bytes(data, key)))
task_queue.task_done()
def write_result_chunk(fp, w_chunk_idx, pending, hasher):
if not pending:
return w_chunk_idx, pending
pending.sort()
for pending_idx, (chunk_idx, chunk) in enumerate(pending):
if chunk_idx != w_chunk_idx:
return w_chunk_idx, pending[pending_idx:]
fp.write(chunk)
hasher.update(chunk)
w_chunk_idx += 1
return w_chunk_idx, []
def main(input_file, key_file, output_dir):
worker_count = max(1, os.cpu_count() - 1)
print(f"Decrypting file {input_file} with {worker_count} workers")
task_queue = multiprocessing.JoinableQueue(worker_count * 3)
result_queue = multiprocessing.Queue()
processes = [
multiprocessing.Process(target=xor_worker, args=(task_queue, result_queue))
for _ in range(worker_count)
]
for p in processes:
p.daemon = True
p.start()
chunk_size = 10 * 1024 * 1024
key_chunk_size = 10 * 1024 * 1024
hasher = hashlib.sha256()
# Get the checksum from the input file name
input_file_basename = os.path.basename(input_file)
checksum_hex = input_file_basename.split(".")[-2]
with open(input_file, "rb") as in_file, open(key_file, "rb") as key_file:
# Get the size of the input file
file_size = os.path.getsize(input_file)
# Minus the checksum size
file_size -= hasher.digest_size
# Read the checksum from the beginning of the input file
expected_hash = in_file.read(hasher.digest_size)
# Create the output file path without the checksum in the filename
# remove .<checksum>.enc
input_file_basename = input_file_basename[:-len(checksum_hex) - 5]
output_file = os.path.join(output_dir, input_file_basename)
with open(output_file, "wb") as out_file:
r_chunk_idx = 0 # how many chunks we have read
w_chunk_idx = 0 # how many chunks have been written
write_pending = [] # have xor results, awaiting to be written to file
bytes_read = 0
while True:
chunk = in_file.read(chunk_size)
if not chunk:
break
key_chunk = key_file.read(key_chunk_size)
if not key_chunk:
key_file.seek(0)
key_chunk = key_file.read(key_chunk_size)
task_queue.put((r_chunk_idx, chunk, key_chunk))
# read available results
while not result_queue.empty():
write_pending.append(result_queue.get())
w_chunk_idx_new, write_pending = write_result_chunk(out_file, w_chunk_idx, write_pending, hasher)
bytes_read += (w_chunk_idx_new - w_chunk_idx) * chunk_size
progress = bytes_read / file_size * 100
sys.stdout.write(f"\rProgress: {progress:.2f}%")
sys.stdout.flush()
w_chunk_idx = w_chunk_idx_new
r_chunk_idx += 1
# wait for xor workers
sys.stdout.write('\rWaiting for workers...')
sys.stdout.flush()
task_queue.join()
while not result_queue.empty():
write_pending.append(result_queue.get())
sys.stdout.write('\rWriting final chunks...')
sys.stdout.flush()
write_result_chunk(out_file, w_chunk_idx, write_pending, hasher)
computed_hash = hasher.digest()
if computed_hash != expected_hash:
print("\nError: Checksums do not match. The file may be corrupted.")
sys.exit(1)
print ("\nDecryption completed.")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: decrypt.py input_file key_file output_dir")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
git上還給了一個(gè) 法條檢索模塊
3、從百度網(wǎng)盤(提取碼:r0vx)下載法條檢索模塊,并運(yùn)行其中的python server.py
啟動(dòng)法條檢索服務(wù),默認(rèn)掛在9098端口。
模型解密好后可以使用了
使用交互界面運(yùn)行
運(yùn)行以下命令啟動(dòng)交互網(wǎng)頁(yè),訪問http://127.0.0.1:7863
。
python demo_web.py \
--port 7863 \
--checkpoint /path/to/model \
--classifier_url "http://127.0.0.1:9098/check_hunyin"
執(zhí)行這個(gè)就可以開啟使用旅程了
git:https://github.com/AndrewZhe/lawyer-llama
下期預(yù)告(幫作者點(diǎn)一下廣告,企鵝將為大模型測(cè)試注入一筆贊助費(fèi)O(∩_∩)O哈哈~):
????????下期分享【基于大模型sd,lora微調(diào)自己的圖片的訓(xùn)練】,歡迎關(guān)注 pythonLLM智能
?
往期回顧:
176B竟然可以辣么快,效果直逼chatgpt-4直接hf在線體驗(yàn),還可以商用
數(shù)據(jù)、180B模型、訓(xùn)練方法一并開源,這個(gè)世界級(jí)多模態(tài)大模型可媲美chatgpt 96%的效果
達(dá)到chatgpt 90%效果的llama,Chinese-Alpaca-Plus-13B合并使用全過程分享
chatglm+langchain+互聯(lián)網(wǎng),你可以將大模型接入網(wǎng)絡(luò)了
什么情況用Bert模型,什么情況用LLaMA、ChatGLM類大模型,咋選?文章來源:http://www.zghlxwxcb.cn/news/detail-515392.html
基于chatglm、moss+知識(shí)庫(kù)+langchain的問系統(tǒng)的搭建文章來源地址http://www.zghlxwxcb.cn/news/detail-515392.html
到了這里,關(guān)于基于chinese-llama-plus北大團(tuán)隊(duì)推出法律大模型,數(shù)據(jù)與模型全部開源,模型合并使用全流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!