WildOjisan commited on
Commit
40b8c49
ยท
1 Parent(s): 72f992b
Files changed (1) hide show
  1. 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
- return CommonResponse(success=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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))