前言
近期以chatgpt等文生成LLMS爆火,國(guó)內(nèi)也逐漸開(kāi)源了中文版的chatgpt,本文以清華大學(xué)開(kāi)源的6b的chatglm為例,實(shí)踐one-shot微調(diào),設(shè)計(jì)prompt咒語(yǔ)在信息抽取領(lǐng)域的實(shí)驗(yàn)效果。
1、場(chǎng)景描述
給定一個(gè)JD的職位要求,需要從描述中抽取出相應(yīng)的實(shí)體。
例如:
'職位要求:1、碩士以上學(xué)歷。2、計(jì)算機(jī)相關(guān)專(zhuān)業(yè)。3、3年以上工作經(jīng)驗(yàn)。4、熟練掌握python或者c++語(yǔ)言。5、有自然語(yǔ)言處理獲獎(jiǎng)經(jīng)歷優(yōu)先'
相應(yīng)的schema的實(shí)體為:
'學(xué)歷要求': ['碩士'],
'專(zhuān)業(yè)要求': ['計(jì)算機(jī)'],
'工作年限要求': ['3年以上'],
'編程語(yǔ)言': ['python', 'c++'],
'加分項(xiàng)': ['自然語(yǔ)言處理獲獎(jiǎng)經(jīng)歷'],
2、prompt咒語(yǔ)設(shè)計(jì)
prompt設(shè)計(jì)主要點(diǎn):
- 告知llms什么是信息抽取任務(wù),以職位要求信息抽取為例:需要告知模型你需要抽取的實(shí)體類(lèi)型有哪些
- 告知模型輸出格式要求,例如:json格式
3、實(shí)現(xiàn)
#!/usr/bin/env python
# _*_coding:utf-8_*_
# Author : Junhui Yu
import re
import json
from transformers import AutoTokenizer, AutoModel
# 根據(jù)需求補(bǔ)充
schema = {
'JD崗位要求': ['學(xué)歷要求', '專(zhuān)業(yè)要求', '工作年限要求', '編程語(yǔ)言', '加分項(xiàng)']
}
IE_PATTERN = "{}\n\n提取上述句子中{}類(lèi)型的實(shí)體,輸出JSON格式,上述句子中不存在的信息用['該JD未要求']來(lái)表示,多個(gè)值之間用','分隔。"
ie_examples = {
'JD崗位要求':
{
'sentence': '職位要求:1、碩士以上學(xué)歷。2、計(jì)算機(jī)相關(guān)專(zhuān)業(yè)。3、3年以上工作經(jīng)驗(yàn)。4、熟練掌握python或者c++語(yǔ)言。5、有自然語(yǔ)言處理獲獎(jiǎng)經(jīng)歷優(yōu)先',
'answers': {
'學(xué)歷要求': ['碩士'],
'專(zhuān)業(yè)要求': ['計(jì)算機(jī)'],
'工作年限要求': ['3年以上'],
'編程語(yǔ)言': ['python', 'c++'],
'加分項(xiàng)': ['自然語(yǔ)言處理獲獎(jiǎng)經(jīng)歷'],
}
}
}
def init_prompts():
ie_prefix = [
(
"需要你協(xié)助完成信息抽取任務(wù),當(dāng)我給你一個(gè)JD職位要求時(shí),幫我抽取出句子中三元組,并按照J(rèn)SON的格式輸出,上述句子中沒(méi)有的信息用['該JD未要求']來(lái)表示,多個(gè)值之間用','分隔。",
'請(qǐng)輸入JD職位描述。'
)
]
properties_str = ', '.join(schema['JD崗位要求'])
schema_str_list = f'“JD崗位要求”({properties_str})'
sentence = ie_examples['JD崗位要求']['sentence']
sentence_with_prompt = IE_PATTERN.format(sentence, schema_str_list)
ie_prefix.append((
f'{sentence_with_prompt}',
f"{json.dumps(ie_examples['JD崗位要求']['answers'], ensure_ascii=False)}"
))
return {'ie_prefix': ie_prefix}
def format_output(response: str):
if '```json' in response:
res = re.findall(r'```json(.*?)```', response)
if len(res) and res[0]:
response = res[0]
response.replace('、', ',')
try:
return json.loads(response)
except:
return response
def inference(sentences: list, custom_settings: dict):
for sentence in sentences:
properties_str = ', '.join(schema['JD崗位要求'])
schema_str_list = f'“JD崗位要求”({properties_str})'
sentence_with_ie_prompt = IE_PATTERN.format(sentence, schema_str_list)
result, _ = model.chat(tokenizer, sentence_with_ie_prompt, history=custom_settings['ie_prefix'])
result = format_output(result)
print(sentence)
print(result)
if __name__ == '__main__':
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
sentences = [
'職位要求:1、本科以上學(xué)歷。2、電子信息或軟件工程專(zhuān)業(yè)。3、1-3年工作經(jīng)驗(yàn)。4、熟練掌握java或者c++語(yǔ)言。5、有相關(guān)項(xiàng)目經(jīng)驗(yàn)優(yōu)先',
]
custom_settings = init_prompts()
inference(sentences,
custom_settings
)
輸出結(jié)果:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-505748.html
{
'學(xué)歷要求': ['本科以上學(xué)歷'],
'專(zhuān)業(yè)要求': ['電子信息或軟件工程專(zhuān)業(yè)'],
'工作年限要求': ['1-3年'],
'編程語(yǔ)言': ['熟練掌握java或者c++語(yǔ)言'],
'加分項(xiàng)': ['有相關(guān)項(xiàng)目經(jīng)驗(yàn)優(yōu)先']
}
總結(jié)
本文通過(guò)one-shot微調(diào)chatglm-6b在信息抽取領(lǐng)域上的實(shí)驗(yàn),輸出效果還可以,當(dāng)然如果有資源微調(diào)更大參數(shù)量的LLMS。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-505748.html
到了這里,關(guān)于淺嘗prompt咒語(yǔ)設(shè)計(jì):one-shot微調(diào)chatglm-6b實(shí)踐信息抽取的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!