aiqtech commited on
Commit
18786d2
ยท
verified ยท
1 Parent(s): 644e0b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -24
app.py CHANGED
@@ -277,34 +277,93 @@ class LLMCollaborativeSystem:
277
  if self.test_mode:
278
  logger.info(f"ํ…Œ์ŠคํŠธ ๋ชจ๋“œ ์ŠคํŠธ๋ฆฌ๋ฐ - Role: {role}")
279
 
280
- # ํ…Œ์ŠคํŠธ ์‘๋‹ต์„ ๋ฏธ๋ฆฌ ์ •์˜
281
- if role == "supervisor" and "์กฐ์‚ฌ์ž AI์˜ 1์ฐจ ์กฐ์‚ฌ ๊ฒฐ๊ณผ" in messages[0]["content"]:
282
- response = """1์ฐจ ์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ€ํ† ํ•œ ๊ฒฐ๊ณผ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‰๊ฐ€์™€ ์ถ”๊ฐ€ ์กฐ์‚ฌ ์ง€์‹œ๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค.
283
-
284
- **์กฐ์‚ฌ ๋‚ด์šฉ ํ‰๊ฐ€**
285
- - ๊ฐ•์ : ์ฃผ์š” ๊ธฐ์ˆ ์  ์ ‘๊ทผ๋ฒ•๊ณผ ๋Œ€ํ‘œ์ ์ธ ์„ฑ๊ณต ์‚ฌ๋ก€๊ฐ€ ์ž˜ ์ •๋ฆฌ๋จ
286
- - ๋ถ€์กฑํ•œ ์ : ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„ ๋„๊ตฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ ์ •๋ณด ๋ถ€์กฑ
287
-
288
- [์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ]: ML optimization tools 2024, model optimization failure cases"""
289
- elif role == "supervisor" and "์กฐ์‚ฌ์ž AI๊ฐ€ ์ •๋ฆฌํ•œ ์ตœ์ข… ์กฐ์‚ฌ ๋‚ด์šฉ" in messages[0]["content"]:
290
- response = """์ข…ํ•ฉ๋œ ์กฐ์‚ฌ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์‹คํ–‰์ž AI์—๊ฒŒ ๊ตฌ์ฒด์ ์œผ๋กœ ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค."""
291
- elif role == "supervisor" and messages[0]["content"].find("์‹คํ–‰์ž AI์˜ ๋‹ต๋ณ€") > -1:
292
- response = """์‹คํ–‰์ž AI์˜ ๊ณ„ํš์„ ๊ฒ€ํ† ํ•œ ๊ฒฐ๊ณผ, ๊ฐœ์„ ์‚ฌํ•ญ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค."""
293
- elif role == "supervisor":
294
- response = """์ด ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๊ฑฐ์‹œ์  ๋ถ„์„์„ ์ œ์‹œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
295
-
296
- [๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ]: best practices, implementation guide, case studies"""
297
- elif role == "researcher" and "์ถ”๊ฐ€ ์กฐ์‚ฌ" in messages[0]["content"]:
298
- response = """๊ฐ๋…์ž์˜ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜์˜ํ•˜์—ฌ ์ถ”๊ฐ€ ์กฐ์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค."""
299
  elif role == "researcher":
300
- response = """์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค."""
301
- elif role == "executor" and "์ตœ์ข… ๋ณด๊ณ ์„œ" in messages[0]["content"]:
302
- response = """๊ฐ๋…์ž AI์˜ ํ”ผ๋“œ๋ฐฑ์„ ์™„์ „ํžˆ ๋ฐ˜์˜ํ•˜์—ฌ ์ตœ์ข… ์‹คํ–‰ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค."""
 
 
 
 
 
 
303
  else:
304
- response = """๊ฐ๋…์ž์˜ ์ง€์‹œ์— ๋”ฐ๋ผ ๊ตฌ์ฒด์ ์ธ ์‹คํ–‰ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•ฉ๋‹ˆ๋‹ค."""
305
 
306
  yield from self.simulate_streaming(response, role)
307
  return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
309
  # ๐ŸŽฏ ML ๋ชจ๋ธ ์ตœ์ ํ™” ํ”„๋กœ์ ํŠธ ์ตœ์ข… ์‹คํ–‰ ๋ณด๊ณ ์„œ
310
 
@@ -1072,4 +1131,4 @@ if __name__ == "__main__":
1072
  server_port=7860,
1073
  share=True,
1074
  show_error=True
1075
- )
 
277
  if self.test_mode:
278
  logger.info(f"ํ…Œ์ŠคํŠธ ๋ชจ๋“œ ์ŠคํŠธ๋ฆฌ๋ฐ - Role: {role}")
279
 
280
+ # ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ ์‘๋‹ต
281
+ if role == "supervisor":
282
+ if "์กฐ์‚ฌ์ž AI์˜ 1์ฐจ ์กฐ์‚ฌ ๊ฒฐ๊ณผ" in messages[0]["content"]:
283
+ response = "์กฐ์‚ฌ ๋‚ด์šฉ์„ ๊ฒ€ํ† ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ์กฐ์‚ฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. [์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ]: ์ตœ์‹  ๋„๊ตฌ, ์‹คํŒจ ์‚ฌ๋ก€, ROI ๋ถ„์„"
284
+ elif "์กฐ์‚ฌ์ž AI๊ฐ€ ์ •๋ฆฌํ•œ ์ตœ์ข… ์กฐ์‚ฌ ๋‚ด์šฉ" in messages[0]["content"]:
285
+ response = "์กฐ์‚ฌ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ฒด์ ์ธ ์‹คํ–‰ ์ง€์‹œ๋ฅผ ๋‚ด๋ฆฝ๋‹ˆ๋‹ค. ๋‹จ๊ณ„๋ณ„๋กœ ์ง„ํ–‰ํ•˜์„ธ์š”."
286
+ elif "์‹คํ–‰์ž AI์˜ ๋‹ต๋ณ€" in messages[0]["content"]:
287
+ response = "์‹คํ–‰ ๊ณ„ํš์„ ๊ฒ€ํ† ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋‹ˆํ„ฐ๋ง ์ฒด๊ณ„์™€ ํŒ€ ์—ญ๋Ÿ‰ ๊ฐœ๋ฐœ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
288
+ else:
289
+ response = "๊ฑฐ์‹œ์  ๋ถ„์„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. [๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ]: ๋ชจ๋ฒ” ์‚ฌ๋ก€, ๊ตฌํ˜„ ๊ฐ€์ด๋“œ, ์‚ฌ๋ก€ ์—ฐ๊ตฌ"
 
 
 
 
 
 
 
 
 
290
  elif role == "researcher":
291
+ if "์ถ”๊ฐ€ ์กฐ์‚ฌ" in messages[0]["content"]:
292
+ response = "์ถ”๊ฐ€ ์กฐ์‚ฌ๋ฅผ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์ธ ๋„๊ตฌ์™€ ROI ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ–ˆ์Šต๋‹ˆ๋‹ค."
293
+ else:
294
+ response = "์›น ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์ตœ์‹  ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ํŠธ๋ Œ๋“œ์™€ ์‚ฌ๋ก€๋ฅผ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค."
295
+ elif role == "executor":
296
+ if "์ตœ์ข… ๋ณด๊ณ ์„œ" in messages[0]["content"]:
297
+ response = "ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜์˜ํ•œ ์ตœ์ข… ๋ณด๊ณ ์„œ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ํ”„๋กœ์ ํŠธ ๊ณ„ํš๊ณผ ์˜ˆ์ƒ ์„ฑ๊ณผ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค."
298
+ else:
299
+ response = "๊ตฌ์ฒด์ ์ธ ์‹คํ–‰ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์ฐจ๋ณ„ ์ƒ์„ธ ์ผ์ •๊ณผ ๋„๊ตฌ ์„ ํƒ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค."
300
  else:
301
+ response = "ํ…Œ์ŠคํŠธ ์‘๋‹ต์ž…๋‹ˆ๋‹ค."
302
 
303
  yield from self.simulate_streaming(response, role)
304
  return
305
+
306
+ # ์‹ค์ œ API ํ˜ธ์ถœ (๊ธฐ์กด ์ฝ”๋“œ์™€ ๋™์ผ)
307
+ try:
308
+ system_prompts = {
309
+ "supervisor": "๋‹น์‹ ์€ ๊ฑฐ์‹œ์  ๊ด€์ ์—์„œ ๋ถ„์„ํ•˜๊ณ  ์ง€๋„ํ•˜๋Š” ๊ฐ๋…์ž AI์ž…๋‹ˆ๋‹ค.",
310
+ "researcher": "๋‹น์‹ ์€ ์ •๋ณด๋ฅผ ์กฐ์‚ฌํ•˜๊ณ  ์ฒด๊ณ„์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ์กฐ์‚ฌ์ž AI์ž…๋‹ˆ๋‹ค.",
311
+ "executor": "๋‹น์‹ ์€ ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์„ ๊ตฌํ˜„ํ•˜๋Š” ์‹คํ–‰์ž AI์ž…๋‹ˆ๋‹ค."
312
+ }
313
+
314
+ full_messages = [
315
+ {"role": "system", "content": system_prompts.get(role, "")},
316
+ *messages
317
+ ]
318
+
319
+ payload = {
320
+ "model": self.model_id,
321
+ "messages": full_messages,
322
+ "max_tokens": 2048,
323
+ "temperature": 0.7,
324
+ "top_p": 0.8,
325
+ "stream": True,
326
+ "stream_options": {"include_usage": True}
327
+ }
328
+
329
+ logger.info(f"API ์ŠคํŠธ๋ฆฌ๋ฐ ํ˜ธ์ถœ ์‹œ์ž‘ - Role: {role}")
330
+
331
+ response = requests.post(
332
+ self.api_url,
333
+ headers=self.create_headers(),
334
+ json=payload,
335
+ stream=True,
336
+ timeout=10
337
+ )
338
+
339
+ if response.status_code != 200:
340
+ logger.error(f"API ์˜ค๋ฅ˜: {response.status_code}")
341
+ yield f"โŒ API ์˜ค๋ฅ˜ ({response.status_code}): {response.text[:200]}"
342
+ return
343
+
344
+ for line in response.iter_lines():
345
+ if line:
346
+ line = line.decode('utf-8')
347
+ if line.startswith("data: "):
348
+ data = line[6:]
349
+ if data == "[DONE]":
350
+ break
351
+ try:
352
+ chunk = json.loads(data)
353
+ if "choices" in chunk and chunk["choices"]:
354
+ content = chunk["choices"][0].get("delta", {}).get("content", "")
355
+ if content:
356
+ yield content
357
+ except json.JSONDecodeError:
358
+ continue
359
+
360
+ except requests.exceptions.Timeout:
361
+ yield "โฑ๏ธ API ํ˜ธ์ถœ ์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”."
362
+ except requests.exceptions.ConnectionError:
363
+ yield "๐Ÿ”Œ API ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
364
+ except Exception as e:
365
+ logger.error(f"์ŠคํŠธ๋ฆฌ๋ฐ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
366
+ yield f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
367
 
368
  # ๐ŸŽฏ ML ๋ชจ๋ธ ์ตœ์ ํ™” ํ”„๋กœ์ ํŠธ ์ตœ์ข… ์‹คํ–‰ ๋ณด๊ณ ์„œ
369
 
 
1131
  server_port=7860,
1132
  share=True,
1133
  show_error=True
1134
+ )