keyword_konan / gpt_based_function.py
mlo0ollm's picture
apply keyword's parallel processing
62ec04b
raw
history blame
1.19 kB
import ast
import openai
from text_annotator import generate_annotated_text
def gpt_keyw_extract_n_annotator(user_text):
'''
:param user_text: str
:return: annotated_text: str
'''
task_description = "You are a Python function that extract 5 keywords from {input_text}. 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",
messages=messages,
temperature=0,
max_tokens=1019,
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