๐Ÿ–‹ ์˜คํ”ˆ์†Œ์Šค LLM์œผ๋กœ ๊ธˆ์œต ๋‰ด์Šค ์š”์•ฝํ•˜๊ธฐ (๋žญ์ฒด์ธ, SERP API)

0koangยท2024๋…„ 7์›” 26์ผ
1

AI

๋ชฉ๋ก ๋ณด๊ธฐ
6/7
post-thumbnail





โœ… ๋ชฉํ‘œ

SERP Finance News API์™€ ์˜คํ”ˆ์†Œ์Šค LLM์„ ํ™œ์šฉํ•ด ์ž๋™์œผ๋กœ ๊ธˆ์œต ๋‰ด์Šค๋ฅผ ๋ชจ์œผ๊ณ , JSON ํ˜•ํƒœ๋กœ ์š”์•ฝํ•ด๋ณธ๋‹ค





โœ… ์ฝ”๋“œ ์ƒ์„ธ ๋ถ„์„

ํ™˜๊ฒฝ ์„ค์ • ๋ฐ API ์š”์ฒญ

  • ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  SERP API์— ์š”์ฒญ
  • Google News ์—”์ง„์œผ๋กœ "Tesla"์— ๊ด€ํ•œ ๋‰ด์Šค๋ฅผ ๊ฒ€์ƒ‰
from dotenv import load_dotenv

# ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ๋“œ (.env)
load_dotenv()
import requests
import json
import os

# API ํ‚ค์™€ ์—”๋“œํฌ์ธํŠธ URL ์„ค์ •
api_key = os.getenv('SERP_API_KEY')
url = "https://serpapi.com/search.json"

# ๊ฒ€์ƒ‰ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
params = {
    "api_key": api_key,
    "engine": "google_news",
    "q": "Tesla",
    "lang": "ko",
    "country": "kr"
}

# API ์š”์ฒญ
response = requests.get(url, params=params)



์‘๋‹ต ์ฒ˜๋ฆฌ ๋ฐ ๊ธฐ์‚ฌ ํ•„ํ„ฐ๋ง

  • ์„ฑ๊ณต์ ์ธ ์‘๋‹ต์„ ๋ฐ›์œผ๋ฉด JSON ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ
  • API ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๊ณ  Forbes ๊ธฐ์‚ฌ๋งŒ ๊ณจ๋ผ๋ƒ„
    • Forbes ๊ธฐ์‚ฌ๋งŒ ๊ณจ๋ผ๋‚ด๋Š” ์ด์œ : ์ด ๊ธ€ ์˜ˆ์ œ์—์„œ๋Š” ํŒŒ์‹ฑ ๋กœ์ง์„ Forbes ์‚ฌ์ดํŠธ์— ๋งž๊ฒŒ๋” ํ•ด๋†จ๊ธฐ ๋•Œ๋ฌธ
if response.status_code == 200:
    data = json.loads(response.text)
    
    articles = []
    news_results = data.get("news_results", [])
    for article in news_results:
        link = article.get('link', 'N/A')
        
    	# forbes ๊ธฐ์‚ฌ๋งŒ ํ•„ํ„ฐ๋ง
        if 'forbes' in link:
            articles.append(article)
    
    for article in articles[:5]:
        print(f"์ œ๋ชฉ: {article.get('title', 'N/A')}")
        print(f"๋งํฌ: {article.get('link', 'N/A')}")
        print(f"์ถœ์ฒ˜: {article.get('source', 'N/A')}")
        print(f"๊ฒŒ์‹œ ์‹œ๊ฐ„: {article.get('date', 'N/A')}")
        print("=====================================")
else:
    print(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {response.status_code}")

์‹คํ–‰๊ฒฐ๊ณผ



์›น ์Šคํฌ๋ž˜ํ•‘ ๋ฐ ์ฝ˜ํ…์ธ  ์ถ”์ถœ

  • ๋‰ด์Šค ๋‚ด์šฉ ์Šคํฌ๋ž˜ํ•‘. ์ฃผ์š” ๊ธฐ์‚ฌ ๋‚ด์šฉ๋งŒ ์ถ”์ถœํ•œ๋‹ค
import bs4
from langchain_community.document_loaders import WebBaseLoader

loader = WebBaseLoader(
    web_paths=[articles[0].get('link', 'N/A')],
    bs_kwargs=dict(
        parse_only=bs4.SoupStrainer(
            name=["div"],
            attrs={"class": ["article-body fs-article fs-responsive-text current-article article-topline"]},
        )
    ),
)
data = loader.load()

print(data[0].page_content)

์‹คํ–‰๊ฒฐ๊ณผ



์š”์•ฝ

  • ์˜คํ”ˆ์†Œ์Šค LLM์œผ๋กœ ์ถ”์ถœํ•œ ๊ธฐ์‚ฌ ๋‚ด์šฉ์„ ์š”์•ฝ
from langchain_community.chat_models import ChatOllama

eeve = ChatOllama(model="EEVE-Korean-Instruct-10.8B-v1.0:latest")

prompt = f"""
๋‹น์‹ ์€ ๊ธˆ์œต ๋ถ„์•ผ์˜ ์š”์•ฝ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. 

์•„๋ž˜์˜ ๊ธˆ์œต ๊ด€๋ จ ํ…์ŠคํŠธ๋ฅผ ์ฝ๊ณ , ๋‹ค์Œ ์ง€์นจ์— ๋”ฐ๋ผ ์š”์•ฝํ•ด ์ฃผ์„ธ์š”:
1. ์ „์ฒด ๋‚ด์šฉ์„ 50๋‹จ์–ด ์ด๋‚ด๋กœ ์š”์•ฝํ•œ 'summary' ํ•„๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
2. ํ•ต์‹ฌ ๋‚ด์šฉ์„ 5-10๊ฐœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ •๋ฆฌํ•ด์„œ 'key_points' ํ•„๋“œ์— ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
3. ๊ฐ key_points ๋‚ด์šฉ์€ ํ•œ ๋ฌธ์žฅ์œผ๋กœ, 20๋‹จ์–ด๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์„ธ์š”.
4. ํ…์ŠคํŠธ์˜ ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ์™€ ๊ด€๋ จ๋œ ๋‹จ์–ด๋‚˜ ๊ฐœ๋…์„ 10๊ฐœ ๋‚ด์™ธ๋กœ 'tags' ํ•„๋“œ์— ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”.
5. ์ „๋ฌธ ์šฉ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฐ„๋‹จํžˆ ์„ค๋ช…์„ ๋ง๋ถ™์—ฌ ์ฃผ์„ธ์š”.
6. ์ˆซ์ž๋‚˜ ํ†ต๊ณ„๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํฌํ•จ์‹œ์ผœ ์ฃผ์„ธ์š”.
7. ์š”์•ฝ์€ ๊ฐ๊ด€์ ์ด๊ณ  ์ค‘๋ฆฝ์ ์ธ ํ†ค์„ ์œ ์ง€ํ•ด ์ฃผ์„ธ์š”.

์‘๋‹ต์€ ๋ฐ˜๋“œ์‹œ ์•„๋ž˜์˜ JSON ํ˜•์‹์„ ๋”ฐ๋ผ ์ฃผ์„ธ์š”: 
{{
    "summary": "์ „์ฒด ๋‚ด์šฉ ์š”์•ฝ (50๋‹จ์–ด ์ด๋‚ด)",
    "key_points": [
        "ํ•ต์‹ฌ ํฌ์ธํŠธ 1 (20๋‹จ์–ด ์ด๋‚ด)",
        "ํ•ต์‹ฌ ํฌ์ธํŠธ 2 (20๋‹จ์–ด ์ด๋‚ด)",
        "ํ•ต์‹ฌ ํฌ์ธํŠธ 3 (20๋‹จ์–ด ์ด๋‚ด)",
        "ํ•ต์‹ฌ ํฌ์ธํŠธ 4 (20๋‹จ์–ด ์ด๋‚ด)",
        "ํ•ต์‹ฌ ํฌ์ธํŠธ 5 (20๋‹จ์–ด ์ด๋‚ด)",
        ...
    ],
    "tags": ["ํƒœ๊ทธ1", "ํƒœ๊ทธ2", "ํƒœ๊ทธ3", ...]
}}

ํ…์ŠคํŠธ: 
{data[0].page_content}

์œ„ ์ง€์นจ์— ๋”ฐ๋ผ JSON ํ˜•์‹์œผ๋กœ ์š”์•ฝํ•ด ์ฃผ์„ธ์š”. 
"""
response = eeve.invoke(prompt)

# JSON ํŒŒ์‹ฑ
import json
parsed_response = json.loads(response.content)
parsed_response

์‹คํ–‰๊ฒฐ๊ณผ

profile
์„œ๋น„์Šค ํ•ต์‹ฌ ๊ฐ€์น˜๋ฅผ ์ดํ•ดํ•˜๊ณ , ์ง€์†์ ์ธ ๊ฐœ์„ ์„ ์ด๋„๋Š” ์—”์ง€๋‹ˆ์–ด(๋ฅผ ์ง€ํ–ฅํ•จ)

0๊ฐœ์˜ ๋Œ“๊ธ€