Spaces:
Running
Running
| """ | |
| 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"] | |
| ) | |
| 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)) | |