From 592566440ccebc146c94b23fd32c00f435646fe0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-TKLFCPR\\ython" Date: Sun, 31 May 2026 17:07:11 +0900 Subject: [PATCH] feat(messenger): natural language command parser (NL Command) - core/nl_command.py: Ollama LLM + rule-based fallback - POST /api/messenger/bot/nl: new NL endpoint - /bot/command: NL fallback when no command matches - 9/10 rule tests PASS Co-Authored-By: Claude Sonnet 4.6 --- itsm/core/nl_command.py | 393 ++++++++++++++++++++++++++++++++++++++ itsm/routers/messenger.py | 104 +++++++++- 2 files changed, 494 insertions(+), 3 deletions(-) create mode 100644 itsm/core/nl_command.py diff --git a/itsm/core/nl_command.py b/itsm/core/nl_command.py new file mode 100644 index 00000000..5aaad284 --- /dev/null +++ b/itsm/core/nl_command.py @@ -0,0 +1,393 @@ +""" +자연어 → 메신저 봇 명령어 파서 (NL Command Parser) + +Ollama(로컬 LLM) 기반으로 자연어 입력을 봇 명령어로 변환. +Ollama 미연결 시 규칙 기반 폴백. + +반환 형태: +{ + "command": "!scrap", + "args": ["https://example.com"], + "full_command": "!scrap https://example.com", + "confidence": 0.92, + "explanation": "URL 스크랩 요청으로 판단" +} +""" +from __future__ import annotations + +import json +import logging +import os +import re +from typing import Any, Dict, List, Optional, Tuple + +import httpx + +logger = logging.getLogger(__name__) + +OLLAMA_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434") + "/api/generate" +NL_MODEL = os.getenv("NL_COMMAND_MODEL", "llama3") +TIMEOUT = 20 # Ollama 호출 타임아웃 +MIN_CONFIDENCE = 0.55 # 이 이하면 "이해 못함"으로 처리 + + +# ── Few-shot 프롬프트 ───────────────────────────────────────────────────────── + +_SYSTEM_PROMPT = """\ +너는 GUARDiA ITSM 메신저 봇의 자연어 명령 해석기다. +사용자의 자연어 입력을 분석해 아래 봇 명령어 중 하나로 변환하라. + +[지원 명령어 목록] +!vibe [project_id] - SR에 대한 바이브 코딩 세션 시작 +!build - 빌드 실행 +!deploy - 배포 실행 +!status - SR 상태 조회 +!cancel - 세션 취소 +!health - 서버 헬스체크 +!log [path] - 서버 로그 분석 +!sm