openai api를 이용한 참고자료 변환

This commit is contained in:
hm-desk
2024-10-02 08:32:47 +09:00
parent a205c300c0
commit f8865fcfed
4 changed files with 53 additions and 1 deletions

2
.gitignore vendored
View File

@ -162,3 +162,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# custom
test.py

50
open_ai.py Normal file
View File

@ -0,0 +1,50 @@
import os
from openai import OpenAI
from dotenv import load_dotenv
import translate_article as ta
# .env 파일에서 API 키 로드
load_dotenv()
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)
def generate_blog_post(article_text, style_reference):
# ChatCompletion API를 사용하여 텍스트 변환
prompt = (
"""
너는 대한민국에 거주하는 블로그 전문가이다.
네가 작성한 블로그 글은 지난 3년간 높은 주목성, 관여도, 전환율을 만들었다.
이 전문성을 이용해서 제공받는 기사를 블로그 형태로 변형하여 작성해야만 한다.
"""
f"\n블로그 스타일은 아래 문서를 모방해줘. 적절한 사례들이 들어가도 좋겠어.\n---\n{style_reference}\n"
f"제공된 기사 내용:\n{article_text}"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "너는 대한민국에 거주하는 블로그 전문가이다."},
{"role": "user", "content": prompt}
],
max_tokens=2000,
temperature=0.7, # 창의성을 위한 적절한 값 조정
)
# 응답에서 텍스트 추출
blog_post = response.choices[0].message.content
return blog_post
# 예시 기사 텍스트 (text 파싱 결과로 제공된 텍스트를 사용할 수 있습니다)
article_text = ta.getContents()
# 블로그 스타일 참고 텍스트
style_reference = os.getenv("reference_style")
# 블로그 포스트 생성
blog_post = generate_blog_post(article_text, style_reference)
# 결과 출력
print(">>>>\n" * 3)
print(blog_post)

BIN
requirements.txt Normal file

Binary file not shown.

View File

@ -16,7 +16,7 @@ def getContents():
# HTML 태그를 제거 후 페이지의 모든 텍스트 가져오기 (전체 내용)
page_content = soup.get_text()
# 빈 줄을 제거하고 텍스트만 출력 (줄바꿈 문자를 기준으로 필터링)
lines = [line.strip() for line in page_content.splitlines() if line.strip()]