summary_with_gpt / gpt_based_function.py
mlo0ollm's picture
test
54599d6
import ast
from openai import OpenAI
from text_annotator import generate_annotated_text
client = OpenAI()
def gpt_summary_generator(user_text):
'''
:param user_text:
:return:
'''
task_description_fs = "Summarize the text in 500 characters in Korean."
user_prompt = f"{user_text}"
messages = [{"role": "system", "content": task_description_fs}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0.06,
max_tokens=8362,
top_p=0,
frequency_penalty=0,
presence_penalty=0
)
gpt_summary = response['choices'][0]['message']['content']
return gpt_summary
def gpt_keyword_highlighter(user_text):
'''
:param user_text: str
:return: annotated_text: str
'''
# get keywords from user_text
task_description = "You are a Python function that extracts 5 keywords from {input_text} considering {overall_text_contents}. The output should be formatted as ['keyword1', 'keyword2', ...]. Return only the function's output, with no additional explanations."
user_prompt = r"{input_text}=" + f"{user_text}"
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0,
max_tokens=2006,
top_p=0,
frequency_penalty=0,
presence_penalty=0
)
extracted_keywords = response['choices'][0]['message']['content']
## literal_eval ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ string์„ list๋กœ ๋ณ€ํ™˜
extracted_keywords = ast.literal_eval(extracted_keywords)
## highlighted_text ํ›„์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ์ถ”๊ฐ€
highlighted_text = generate_annotated_text(text=user_text, keyw_list=extracted_keywords)
return highlighted_text
def gpt_text_naturalizer(user_input):
paraphrasing_task = f"Rewrite the contents of '{user_input}' so that it will pass the writing test."
messages = [
{"role": "system", "content": "You are a helpful assistant. use only korean"},
{"role": "user", "content": paraphrasing_task}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0.1,
max_tokens=2500
)
paraphrased_text = response['choices'][0]['message']['content']
return paraphrased_text
def gpt_explanation_generator(user_input, text_to_consider):
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {text_to_consider}"
messages = [
{"role": "system",
"content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand. use only korean"},
{"role": "user", "content": explanation_task}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0.1,
max_tokens=200
)
explanation = response['choices'][0]['message']['content']
return explanation
def gpt_easier_text_generator(user_input):
messages = [
{"role": "system",
"content": "You have a special talent for easily rewriting text. Make it easy to paraphrase, use simple words that an elementary school student can understand, but don't shorten or summarize. Don't forget to use only korean."},
{"role": "user", "content": user_input}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0,
max_tokens=2048
)
explanation = response['choices'][0]['message']['content']
return explanation
def gpt_rater(user_input):
# ๋ฃจ๋ธŒ๋ฆญ์— ์˜ํ•œ ์ฑ„์ 
lubric = """์ฑ„์  ๊ธฐ์ค€ ์ƒ: ๋ฌธ์žฅ์˜ ์ฃผ์ œ๋ฅผ ํŒŒ์•…ํ•˜๊ณ , ์ฃผ์š” ๋‚ด์šฉ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ค‘: ๋ฌธ์žฅ์˜ ์ฃผ์ œ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜: ๋ฌธ์žฅ์˜ ์ฃผ์ œ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์—†๋‹ค."""
# ๋ฃจ๋ธŒ๋ฆญ ๊ธฐ์ค€์„ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ์นธ์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ์ฑ„์ ํ•˜๋Š” ์˜์–ด๋กœ ํ”„๋กฌํ”„ํŠธ
explanation_task = f"{lubric}์„ ๊ธฐ์ค€์œผ๋กœ {user_input}์˜ ๋‚ด์šฉ์„ ์ฑ„์ ํ•ด์ฃผ์„ธ์š”. ์ฑ„์  ๊ธฐ์ค€์€ ๊ณต๊ฐœํ•˜์ง€ ๋ง๊ณ  ์ƒ, ์ค‘,ํ•˜๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ„๋‹จํ•œ ์ด์œ ๋ฅผ ์•Œ๋ ค์ฃผ์„ธ์š”."
messages = [
{"role": "system", "content": "You are a helpful assistant. use only korean"},
{"role": "user", "content": explanation_task}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0125",
messages=messages,
temperature=0.1,
max_tokens=2500
)
feedback = response['choices'][0]['message']['content']
return feedback