Spaces:
Running
Running
Commit ยท
40b8c49
1
Parent(s): 72f992b
- routers/llm_router.py +64 -2
routers/llm_router.py
CHANGED
|
@@ -11,7 +11,7 @@ router = APIRouter(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
@router.post("/askllm")
|
| 14 |
-
def test_home():
|
| 15 |
try:
|
| 16 |
with get_connection() as conn:
|
| 17 |
with conn.cursor() as cur:
|
|
@@ -24,7 +24,69 @@ def test_home():
|
|
| 24 |
""")
|
| 25 |
row = cur.fetchone()
|
| 26 |
pollination_api_key=row[0] if row else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return CommonResponse(success=False, msg=str(e))
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
@router.post("/askllm")
|
| 14 |
+
def test_home(userquery: str = Form("")):
|
| 15 |
try:
|
| 16 |
with get_connection() as conn:
|
| 17 |
with conn.cursor() as cur:
|
|
|
|
| 24 |
""")
|
| 25 |
row = cur.fetchone()
|
| 26 |
pollination_api_key=row[0] if row else None
|
| 27 |
+
|
| 28 |
+
# pollination key ์์ผ๋ฉด ๊ทธ๋ฅ ํด๋ผํํ
์ค๋ฅ๋ผ๊ณ ํค ๋ฑ๊ธฐ
|
| 29 |
+
if not pollination_api_key:
|
| 30 |
+
return CommonResponse(
|
| 31 |
+
success=False,
|
| 32 |
+
msg="Pollinations API Key๊ฐ ์์ต๋๋ค."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# 2. Pollinations Text ๋ชจ๋ธ์ ๋ณด๋ผ ๋ฐ์ดํฐ
|
| 36 |
+
url = "https://gen.pollinations.ai/v1/chat/completions"
|
| 37 |
+
|
| 38 |
+
"""
|
| 39 |
+
ํ๋ฐฐ์์์ ๋ญ ์ฃผ์๋ ๋ด ์ ๋ณด ์ฐ์์์?
|
| 40 |
+
์ปดํจํฐ์ api ํต์ ์ Content-Type, Authorization ์ด๊ฑฐ ์ฐ๋์
|
| 41 |
+
Authorization ๋ถ๋ถ์ pollination_api_key ๋ฃ์ด์ ๋ณด๋ด๋์
|
| 42 |
+
"""
|
| 43 |
+
headers = {
|
| 44 |
+
"Content-Type": "application/json",
|
| 45 |
+
"Authorization": f"Bearer {pollination_api_key}"
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
"""
|
| 49 |
+
๋ด์ฉ๋ฌผ. body ๋ผ๊ณ ๋ ๋ถ๋ฆ.
|
| 50 |
+
"""
|
| 51 |
+
payload = {
|
| 52 |
+
"model": "openai-fast",
|
| 53 |
+
"messages": [
|
| 54 |
+
{
|
| 55 |
+
"role": "system",
|
| 56 |
+
"content": "๋น์ ์ ์น์ ํ ํ๋ก ์๋ด์ฌ์
๋๋ค. ์ด๋ณด์๋ ์ดํดํ๊ธฐ ์ฝ๊ฒ ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์."
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"role": "user",
|
| 60 |
+
"content": userquery
|
| 61 |
+
}
|
| 62 |
+
],
|
| 63 |
+
"temperature": 0.8,
|
| 64 |
+
"max_tokens": 800,
|
| 65 |
+
"stream": False
|
| 66 |
+
}
|
| 67 |
|
| 68 |
+
# 3. Pollinations API ํธ์ถ
|
| 69 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 70 |
+
|
| 71 |
+
# 4. ์คํจ ์ฒดํฌ
|
| 72 |
+
if response.status_code != 200:
|
| 73 |
+
return CommonResponse(
|
| 74 |
+
success=False,
|
| 75 |
+
msg=f"Pollinations API ์ค๋ฅ: {response.text}"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
result = response.json()
|
| 79 |
+
|
| 80 |
+
# 5. AI ์๋ต ํ
์คํธ ๊บผ๋ด๊ธฐ
|
| 81 |
+
answer = result["choices"][0]["message"]["content"]
|
| 82 |
+
|
| 83 |
+
# 6. ํ๋ก ํธ๋ก ๋ฐํ
|
| 84 |
+
return CommonResponse(
|
| 85 |
+
success=True,
|
| 86 |
+
data={
|
| 87 |
+
"question": userquery,
|
| 88 |
+
"answer": answer
|
| 89 |
+
}
|
| 90 |
+
)
|
| 91 |
except Exception as e:
|
| 92 |
return CommonResponse(success=False, msg=str(e))
|