File size: 1,463 Bytes
0398d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
from openai import OpenAI

MESSAGE_FORMAT = """あなたは文章を「事実(Fact))」か「意見(Opinion)」かを判定する熟練の判定者です。以下に箇条書きで与える文章がそれぞれ事実か意見かを判定してください。なお回答は以下の例の形式で行なってください。
### 回答形式例
- Fact
- Fact
- Opinion

### 文章
- {}
"""


def fact_opinion_classifer(client: OpenAI, questions, chunk_size=5, model_name='gpt-4o-mini-2024-07-18'):
    chunk_num = len(questions) // chunk_size
    if len(questions) % chunk_size != 0:
        chunk_num += 1
    chunked_questions = [[] for i in range(chunk_num)]

    for i in range(len(questions)):
        chunked_questions[i // chunk_size].append(questions[i])

    responses = []
    for chunk in chunked_questions:
        # print('chunk_size', len(chunk))
        message = MESSAGE_FORMAT.format('\n- '.join(chunk))
        response = client.chat.completions.create(
            messages=[
                {
                    "role": "user",
                    "content": message
                }
            ],
            model=model_name,
            temperature=0
        )
        responses.append(response.choices[0].message.content)

    gpt_answers = []
    for gpt_answers_text in responses:
        # print(len(gpt_answers_text.split('\n')))
        gpt_answers.extend([ans[2:] for ans in gpt_answers_text.split('\n')])

    return gpt_answers