alpaca2qa.py解读,数据加模板

# -*- coding: utf-8 -*-
# @Time    : 2023/3/20 8:50

# 数据参考 https://github.com/carbonz0/alpaca-chinese-dataset
### 样例

# ``` json
# [
#   {
#       "instruction": "根据给定的坐标确定最近的机场。",
#       "input": "40.728157, -73.794853",
#       "output": "距给定坐标最近的机场是纽约市的拉瓜迪亚机场 (LGA)。"
#   },
#   {
#      "instruction": "输出不同种类水果的列表",
#       "input": "",
#       "output": "1. 苹果\n2. 香蕉\n3. 橘子\n4. 芒果\n5. 草莓\n6. 葡萄\n7. 蓝莓\n8. 樱桃\n9. 猕猴桃\n10. 甜瓜\n11.菠萝\n12. 李子\n13.桃子"
#   },
#   {
#      "instruction": "找出字串中隐藏的信息",
#      "input": "业余咖啡",
#      "output": "隐藏的消息是“咖啡因爱好者”。"
#   }
#  ]
# ```
import json


PROMPT_DICT1 = {
    "prompt_no_input": (
        "给定输入的英文文本,编写适当的输出将英文翻译为中文\n\n"
        "### 英文文本:\n{english}\n\n### 响应:"
    ),
}

PROMPT_DICT = {
    "prompt_input": (
        "下面是一个指令,描述了一个任务,搭配一个输入,提供进一步的上下文。"
        "编写适当的输出完成请求。\n\n"
        "### 指令:\n{instruction}\n\n### 请求:\n{input}\n\n### 响应:"
    ),
    "prompt_no_input": (
        "下面是一个指令,描述了一个任务。"
        "编写适当的输出完成请求。\n\n"
        "### 指令:\n{instruction}\n\n### 响应:"
    ),
}

def alaca2qa(src,dst):
    prompt_input, prompt_no_input = PROMPT_DICT["prompt_input"], PROMPT_DICT["prompt_no_input"]
    with open(src,mode='r',encoding='utf-8') as f:
        list_data_dict = json.loads(f.read())
    sources = [
        # example 是一个字典类型的变量,表示一个数据样本。example.get("input", "") 表示获取 example 字典中 "input" 键对应的值,
        # 如果该键不存在则返回空字符串 ""
        # prompt_input 是一个字符串模板,其中包含了两个占位符 {instruction} 和 {input},它们分别表示指令和输入提示。
        # format_map 是一个字符串方法,它接受一个字典作为参数,将字典中的键值对应替换到字符串模板的占位符中。
        prompt_input.format_map(example) if example.get("input", "") != "" else prompt_no_input.format_map(example)
        for example in list_data_dict
    ]
    targets = [f"{example['output']}" for example in list_data_dict]

    with open(dst, mode='w', encoding='utf-8',newline='\n') as f:

        for i,(s, t) in enumerate(zip(sources, targets)):
            paragraph = [
                {
                    'q': s,
                    'a': [t]
                }
            ]
            f.write(json.dumps({'id': i+1 ,'paragraph' : paragraph },ensure_ascii=False) +'\n')


# def alaca2qa(src,dst):
#     prompt_input, prompt_no_input = PROMPT_DICT["prompt_input"], PROMPT_DICT["prompt_no_input"]
#     with open(src,mode='r',encoding='utf-8') as f:
#         list_data_dict = json.loads(f.read())
#     sources = [
#         prompt_input.format_map(example) if example.get("input", "") != "" else prompt_no_input.format_map(example)
#         for example in list_data_dict
#     ]
#     targets = [f"{example['output']}" for example in list_data_dict]
#
#     with open(dst, mode='w', encoding='utf-8',newline='\n') as f:
#
#         for i,(s, t) in enumerate(zip(sources, targets)):
#             paragraph = [
#                 {
#                     'q': s,
#                     'a': [t]
#                 }
#             ]
#             f.write(json.dumps({'id': i+1 ,'paragraph' : paragraph },ensure_ascii=False) +'\n')



if __name__ == '__main__':
    src = r'./test/merge.json'
    dst = r'./test/Smerge.json'
    alaca2qa(src,dst)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值