InformationLiteracySupportTool / lib /opinion_reason_classifer.py
fukufuk
Update application file
318adba
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