AutoPercenty3/test/gpt_client.py

62 lines
3.0 KiB
Python

import logging
from openai import OpenAI
logger = logging.getLogger('GPTClient')
class GPTClient:
def __init__(self, api_key: str = "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", model="gpt-4o-mini", temperature=0.0):
self.client = OpenAI(api_key=api_key)
self.model = model
self.temperature = temperature
def ask(self, prompt: str) -> str:
"""프롬프트를 이용하여 GPT 모델로부터 응답을 받습니다."""
try:
response = self.client.chat.completions.create(
model=self.model,
temperature=self.temperature,
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
except Exception as e:
logger.error(f"GPT 통신 오류: {e}")
return ""
def is_related_product(self, original_name: str, keyword_name: str) -> bool:
"""상품 연관 여부 판단"""
prompt = (
f"Are the products '{original_name}' and '{keyword_name}' from the same category? "
"Answer strictly with 'True' or 'False'."
)
result = self.ask(prompt)
return result.lower() == "true"
def generate_product_name(self, words: list, original_name: str, top_titles: list, max_length=30) -> str:
"""
주어진 단어와 원본 상품명을 참고하여 업계 용어와 고유 단어를 포함해 최종 상품명 생성.
top_titles의 형식을 참고해 작성.
"""
prompt = (
f"Create a product name in Korean using the following elements:\n"
f"1. Keywords: {words}\n"
f"2. Original Product Name: '{original_name}'\n"
f"3. Use the style and format of the following product titles: {top_titles}\n"
"Consider the characteristics of the original product and include industry-specific terms where appropriate. "
f"The name should sound professional, relevant to the industry, and not exceed {max_length} characters, including spaces."
)
response = self.ask(prompt)
return response.strip()
def extract_proper_nouns(self, words: list) -> list:
"""고유명사만을 추출하되, 상표권으로 등록할 수 없는 단어(나라, 지역, 연도 등)는 제외"""
prompt = (
f"Analyze the following list of words: {words}. Extract only proper nouns that "
f"could potentially be trademarked. Exclude country names, city names, region names, years, and any common terms "
f"that are not likely to be eligible for trademark registration. Return only the suitable words."
)
response = self.ask(prompt)
if response:
proper_nouns = [word.strip() for word in response.split(",") if word.strip()]
return proper_nouns
return []