import json import csv from openai import OpenAI import random token = 0 client = OpenAI() # 读取JSONL文件 filename = 'C:/Users/94427/kashiwa/DISC-Assignment/Experiment/TriviaQA/val_triviaqa.csv' data = [] with open(filename, newline='',encoding="utf-8") as csvfile: reader = csv.DictReader(csvfile) files = 'TriviaQA_GPT3.5Turbo_answers.csv' with open(files, 'w', newline='',encoding='utf-8') as csvfile: writer = csv.writer(csvfile) # 提取内容 # 逐行读取CSV文件 for row in reader: context = row['context']+row['question'] response = client.chat.completions.create( # model="ft:gpt-3.5-turbo-0125:personal:arg-quality-0328:97kBFgug", model="gpt-3.5-turbo-0125", temperature=0, messages=[ {"role": "system", "content":"As a reading comprehension expert, you will receive context and question. Please understand the given Context first and then output the answer of the question based on the Context" }, {"role": "user", "content": str(context)} ] ) answer = response.choices[0].message.content token += response.usage.total_tokens print(f"已消耗Token:{token}") print(answer) writer.writerow([answer])