使用GPT将文档生成问答对

 根据文档生成问题列表

url = 'https://api.openai.com/v1/chat/completions'

# 替换为您自己的API密钥
api_key = 'sk-xxxxxxxxx'

model = "gpt-3.5-turbo-16k"

prompt1 = '''
#01 你是一个问答对数据集处理专家。
#02 你的任务是根据我给出的内容,生成适合作为问答对数据集的问题。
#03 问题要尽量短,不要太长。
#04 一句话中只能有一个问题。
#05 生成的问题必须宏观、价值,不要生成特别细节的问题。
#06 生成问题示例:
"""
李世民是谁?
介绍一下李世民。
李世民有哪些成就?
"""
#07 以下是我给出的内容:
"""
"""
{{此处替换成你的内容}}
"""
'''

def generate_question(text_content, more=False):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    content = "生成适合作为问答对的问题"
    if more:
        content = "尽可能多生成适合作为问答对的问题"
    prompt = prompt1.replace("{{此处替换成你的内容}}", text_content)
    data = {
        "model": model,
        "messages": [
            {"role": "system", "content": prompt},
            {"role": "user", "content": content}
        ]
    }
    start_time = time.time()
    response = requests.post(url, headers=headers, json=data, verify=False)
    print("耗时", time.time() - start_time)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]['content']
    else:
        print(f"Error: {response.status_code}")
        print(response.content)
        return None

根据问题列表生成问答对

url = 'https://api.openai.com/v1/chat/completions'

# 替换为您自己的API密钥
api_key = 'sk-xxxxxxxxx'

model = "gpt-3.5-turbo-16k"

prompt2 = '''
#01 你是一个问答对数据集处理专家。
#02 你的任务是根据我的问题和我给出的内容,生成对应的问答对。
#03 答案要全面,多使用我的信息,内容要更丰富。
#04 你必须根据我的问答对示例格式来生成:
"""
{"content": "李世民是谁?", "summary": "李世民,唐朝第二位皇帝,庙号太宗,是中国历史上著名的政治家、战略家、军事家、书法家和诗人。"}
{"content": "李世民的庙号是什么?", "summary": "李世民的庙号是太宗。"}
#05 我的问题如下:
"""
{{此处替换成你上一步生成的问题}}

"""
#06 我的内容如下:
"""
{{此处替换成你的内容}}
"""
'''

def generate_qa(text_content, question_text):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    prompt = prompt2.replace("{{此处替换成你上一步生成的问题}}", question_text).replace("{{此处替换成你的内容}}", text_content)
    data = {
        "model": model,
        "messages": [
            {"role": "system", "content": prompt},
            {"role": "user", "content": "拼成问答对"}
        ]
    }
    start_time = time.time()
    response = requests.post(url, headers=headers, json=data, verify=False)
    print("耗时", time.time() - start_time)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]['content']
    else:
        print(f"Error: {response.status_code}")
        print(response.content)
        return None

相关推荐

  1. 使用GPT文档生成问答

    2024-03-26 07:12:08       36 阅读
  2. 使用 KerasNLP 从头开始生成 GPT 文本

    2024-03-26 07:12:08       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 07:12:08       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 07:12:08       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 07:12:08       87 阅读
  4. Python语言-面向对象

    2024-03-26 07:12:08       96 阅读

热门阅读

  1. Spring和spring Boot的区别

    2024-03-26 07:12:08       31 阅读
  2. 考研复习时间表(3-4月)(待完善)

    2024-03-26 07:12:08       38 阅读
  3. 最长公共子序列力扣题

    2024-03-26 07:12:08       34 阅读
  4. 模式实现vue事件总线

    2024-03-26 07:12:08       35 阅读
  5. 全量知识系统 灵活的模块化框架 (Q&A)

    2024-03-26 07:12:08       40 阅读
  6. 【flutter】flutter基础总结1

    2024-03-26 07:12:08       40 阅读