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)