【个人工作记录】

import json


def create_dialogues(input_file, output_file):
    dialogues = []

    with open(input_file, 'r', encoding='utf-8') as file:
        for line in file:
            try:
                case = json.loads(line.strip())  # 逐行读取并解析JSON对象

                # 系统角色固定
                system_message = {"role": "system", "content": "你是一个法律咨询AI。"}

                # 用户提问
                fact = case.get('fact', '案件描述缺失。')
                user_message = {"role": "user", "content": f"请解释一下案件中的法律问题:{fact}"}

                # AI助手回答
                accusation = case.get('meta', {}).get('accusation', ['未知罪名'])[0]
                imprisonment = case.get('meta', {}).get('term_of_imprisonment', {}).get('imprisonment', '未知')
                assistant_message = {"role": "assistant",
                                     "content": f"根据案件描述,涉及的法律问题包括:{accusation}。判决结果为:{imprisonment}个月。"}

                dialogues.append({"messages": [system_message, user_message, assistant_message]})
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON on line: {line}")
                print(f"Error message: {e}")

    with open(output_file, 'w', encoding='utf-8') as outfile:
        for dialogue in dialogues:
            json.dump(dialogue, outfile, ensure_ascii=False)
            outfile.write('\n')  # 每条记录换行


# 使用示例
# create_dialogues('D:\\githubDownload\\CAIL2018_ALL_DATA\\final_all_data\\exercise_contest\\新建 文本文档.json',
#                  'output_dialogues.json')
create_dialogues('D:\\githubDownload\\CAIL2018_ALL_DATA\\final_all_data\\exercise_contest\\data_test.json',
                 'test_output_dialogues.json')
import requests

api_key = ''  
headers = {'Authorization': f'Bearer {api_key}'}
upload_url = 'https://open.bigmodel.cn/api/paas/v4/files'

files = {'file': ('1_output_requests.jsonl', open('D:\\githubDownload\\CAIL2018_ALL_DATA\\final_all_data\\exercise_contest\\output.jsonl', 'rb'))}

upload_response = requests.post(upload_url, files=files, headers=headers)

print(upload_response.json())

input_file_id = upload_response.json().get('id')
print("文件ID:", input_file_id)
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="")
response = client.chat.completions.create(
    model="chatglm3-6b-1001",  # 填写需要调用的模型名称
    messages=[
        {"role": "system", "content": "你是一个法律助手,你叫chatGLM"},
        {"role": "user", "content": "你好!你叫什么名字"},
    ],
    stream=True,
)
for chunk in response:
    print(chunk.choices[0].delta)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值