File size: 1,469 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
from openai import OpenAI

REASON_MESSAGE_FORMAT = """あなたは意見に関する文章をどの箇所から意見と読み取れるかを判定する熟練の判定者です。以下に箇条書きで与える文章が事実ではなく意見と判断できる理由をそれぞれ簡潔に30字以内で記述してください

### 文章
- {}

### 意見と読み取れる理由"""


def opinion_reason_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 = REASON_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