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