FastAPI2604_HF / routers /llm_router.py
WildOjisan's picture
.
2385c76
"""
https://chatgpt.com/share/6a0668d8-1c1c-83a9-9d77-de76eb3e7716
"""
import requests
from fastapi import APIRouter, Form, File, UploadFile
from utils.common import CommonResponse
from utils.db import get_connection
import json
router = APIRouter(
prefix="/llm",
tags=["llm"]
)
@router.post("/askllm")
def test_home(userquery: str = Form("")
, selected_cards:str = Form("[]")
, chat_history:str=Form("[]") ):
try:
cards=json.loads(selected_cards)
card_text = "\n".join([
f"- {card['position']} ์œ„์น˜: {card['name']} / {'์—ญ๋ฐฉํ–ฅ' if card['reversed'] else '์ •๋ฐฉํ–ฅ'}"
for card in cards
])
history=json.loads(chat_history)
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute("""
SELECT
api_key
FROM t_api_keys
WHERE service_name='pollination'
LIMIT 1
""")
row = cur.fetchone()
pollination_api_key=row[0] if row else None
# pollination key ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ํด๋ผํ•œํ…Œ ์˜ค๋ฅ˜๋ผ๊ณ  ํ‰ค ๋ฑ‰๊ธฐ
if not pollination_api_key:
return CommonResponse(
success=False,
msg="Pollinations API Key๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
)
# 2. Pollinations Text ๋ชจ๋ธ์— ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ
url = "https://gen.pollinations.ai/v1/chat/completions"
"""
ํƒ๋ฐฐ์ƒ์ž์— ๋ญ ์ฃผ์†Œ๋‚˜ ๋‚ด ์ •๋ณด ์“ฐ์ž”์•„์š”?
์ปดํ“จํ„ฐ์˜ api ํ†ต์‹ ์— Content-Type, Authorization ์ด๊ฑฐ ์“ฐ๋ž˜์š”
Authorization ๋ถ€๋ถ„์— pollination_api_key ๋„ฃ์–ด์„œ ๋ณด๋‚ด๋ž˜์š”
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {pollination_api_key}"
}
"""
๋‚ด์šฉ๋ฌผ. body ๋ผ๊ณ ๋„ ๋ถ€๋ฆ„.
"""
# 4. LLM์—๊ฒŒ ๋ณด๋‚ผ messages ๋งŒ๋“ค๊ธฐ
messages = [
{
"role": "system",
"content": """
๋‹น์‹ ์€ ์นœ์ ˆํ•œ ํƒ€๋กœ ์ƒ๋‹ด์‚ฌ์ž…๋‹ˆ๋‹ค.
๋‹ต๋ณ€์€ 5๋ฌธ์žฅ ์ด๋‚ด๋กœ ์งง๊ฒŒ ํ•˜์„ธ์š”.
๋ณต์žกํ•œ ์„ค๋ช…์€ ํ•˜์ง€ ๋งˆ์„ธ์š”.
์‰ฝ๊ณ  ์ง๊ด€์ ์ด๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”.
์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์นด๋“œ๋Š” ๊ณผ๊ฑฐ, ํ˜„์žฌ, ๋ฏธ๋ž˜ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค.
๊ฐ ์นด๋“œ์˜ ์ •๋ฐฉํ–ฅ/์—ญ๋ฐฉํ–ฅ ์˜๋ฏธ๋ฅผ ๋ฐ˜์˜ํ•ด์„œ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
์ค‘์š”:
์‚ฌ์šฉ์ž์˜ ์ตœ๊ทผ ๋Œ€ํ™” ํ๋ฆ„์„ ๋ฐ˜๋“œ์‹œ ๊ธฐ์–ตํ•˜๊ณ  ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
๊ผญ ์ƒ๋Œ€๋ฐฉ๊ณผ ๋Œ€ํ™”ํ•˜๋Š” ๋А๋‚Œ์ด ๋“ค๋„๋ก ๋‹ต๋ณ€ํ•˜์„ธ์š”.
"""
}
]
# 5. ์ด์ „ ๋Œ€ํ™” ๊ธฐ๋ก ์ถ”๊ฐ€
for msg in history:
messages.append({
"role": msg["role"],
"content": msg["content"]
})
# 6. ์ด๋ฒˆ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ ์ถ”๊ฐ€
messages.append({
"role": "user",
"content": f"""
์‚ฌ์šฉ์ž ์งˆ๋ฌธ:
{userquery}
์„ ํƒ๋œ ํƒ€๋กœ ์นด๋“œ:
{card_text}
์œ„ ์งˆ๋ฌธ๊ณผ ์„ ํƒ๋œ ์นด๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์งง๊ฒŒ ํƒ€๋กœ ์ƒ๋‹ด์„ ํ•ด์ฃผ์„ธ์š”.
"""
})
print(f"# userquery: \n",userquery)
print(f"# messages: \n",messages)
payload = {
"model": "openai-fast",
"messages": messages,
"temperature": 0.8,
"max_tokens": 5000,
"stream": False
}
# 3. Pollinations API ํ˜ธ์ถœ
response = requests.post(url, headers=headers, json=payload)
# 4. ์‹คํŒจ ์ฒดํฌ
if response.status_code != 200:
return CommonResponse(
success=False,
msg=f"Pollinations API ์˜ค๋ฅ˜: {response.text}"
)
result = response.json()
print("========== Pollinations raw result ==========")
print(result)
print("============================================")
# 5. AI ์‘๋‹ต ํ…์ŠคํŠธ ๊บผ๋‚ด๊ธฐ
answer = result["choices"][0]["message"]["content"]
# 6. ํ”„๋ก ํŠธ๋กœ ๋ฐ˜ํ™˜
return CommonResponse(
success=True,
data={
"question": userquery,
"answer": answer
}
)
except Exception as e:
return CommonResponse(success=False, msg=str(e))