From 0c4b3989a16b4bafc74cbab250139b9c60cbb910 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 15:54:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20news=20summarizer=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../news/crud/__init__.py | 0 .../news/crud/news_summarizer.py | 34 +++++++++++++++++++ .../news/schema/__init__.py | 0 .../news/schema/message_item.py | 7 ++++ 4 files changed, 41 insertions(+) create mode 100644 backend/ai_response_processor/news/crud/__init__.py create mode 100644 backend/ai_response_processor/news/crud/news_summarizer.py create mode 100644 backend/ai_response_processor/news/schema/__init__.py create mode 100644 backend/ai_response_processor/news/schema/message_item.py diff --git a/backend/ai_response_processor/news/crud/__init__.py b/backend/ai_response_processor/news/crud/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/ai_response_processor/news/crud/news_summarizer.py b/backend/ai_response_processor/news/crud/news_summarizer.py new file mode 100644 index 00000000..e8de57d8 --- /dev/null +++ b/backend/ai_response_processor/news/crud/news_summarizer.py @@ -0,0 +1,34 @@ +import os + +from langchain.chat_models import ChatOpenAI +from langchain.prompts import ChatPromptTemplate +import logging + +from news.contentqueue.rabbitmq_producer import send_message +from news.schema.message_item import MessageItem + +logger = logging.getLogger(__name__) +ChatOpenAI.openai_api_key = os.environ['OPENAI_API_KEY'] + + +def summarize_news(news_id: int, content: str): + logger.info("summarize_news start : (id)", news_id) + chat = ChatOpenAI(temperature=0.3) + + template = ChatPromptTemplate.from_messages( + [ + ("system", "You're a news summarizer. Also, the answer must be no more than 500 characters in Korean."), + ("user", "{raw_news_content}"), + ] + ) + + prompt = template.format_messages( + raw_news_content={content} + ) + + result = chat.predict_messages(prompt) + message_item = MessageItem( + id=news_id, + content=result.content + ) + send_message(message_item) diff --git a/backend/ai_response_processor/news/schema/__init__.py b/backend/ai_response_processor/news/schema/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/ai_response_processor/news/schema/message_item.py b/backend/ai_response_processor/news/schema/message_item.py new file mode 100644 index 00000000..7def3770 --- /dev/null +++ b/backend/ai_response_processor/news/schema/message_item.py @@ -0,0 +1,7 @@ +from dataclasses import dataclass + + +@dataclass +class MessageItem: + id: int + content: str