File size: 1,413 Bytes
7c93e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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])